Codeforces Round #441 (Div. 2) (ABC)
Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house is b meters, between Owl's and Eeyore's house is c meters.
For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).
Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.
First line contains an integer n (1 ≤ n ≤ 100) — number of visits.
Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses.
Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses.
Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl's and Eeyore's houses.
Output one number — minimum distance in meters Winnie must go through to have a meal n times.
3
2
3
1
3
1
2
3
5
0
In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.
In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all.
吃n顿饭,求最少走多少路径,n = 1时不需要走 ,n > 1 时,选择a、b中最小的走,然后在与c比较一直走最小的。
1 #include <bits/stdc++.h> 2 #define ll long long 3 const int N = 110; 4 using namespace std; 5 6 int main() { 7 int n, a, b, c; 8 cin >> n >> a >> b >> c; 9 if(n == 1) return 0*printf("0\n"); 10 printf("%d\n",min(a,b)+(n-2)*(min(a,min(b,c)))); 11 return 0; 12 }
You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.
Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.
First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.
Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.
If it is not possible to select k numbers in the desired way, output «No» (without the quotes).
Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.
3 2 3
1 8 4
Yes
1 4
3 3 3
1 8 4
No
4 3 5
2 7 7 7
Yes
2 7 7
1 #include <bits/stdc++.h> 2 #define ll long long 3 const int N = 1e5+10; 4 using namespace std; 5 int a[N], b[N]; 6 vector<int> vs[N]; 7 int main() { 8 int n, k, m, x; 9 cin >> n >> k >> m; 10 for(int i = 1; i <= n; i ++) { 11 cin >> a[i]; 12 b[a[i]%m]++; 13 vs[a[i]%m].push_back(i); 14 } 15 for(int i = 0; i <= m; i ++) { 16 int len = vs[i].size(); 17 if(len >= k) { 18 printf("Yes\n"); 19 for(int j = 0; j < k-1; j ++) { 20 printf("%d ",a[vs[i][j]]); 21 } 22 return 0*printf("%d\n",a[vs[i][k-1]]); 23 } 24 } 25 printf("No\n"); 26 return 0; 27 }
Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.
Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.
The first line contains integer n (1 ≤ n ≤ 109).
In the first line print one integer k — number of different values of x satisfying the condition.
In next k lines print these values in ascending order.
21
1
15
20
0
In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.
In the second test case there are no such x.
求是否存在一个数x,使的x加上每个位上的数之和等于n,最大9位 所以从n-81就到n遍历一下就够了。
1 #include <bits/stdc++.h> 2 #define ll long long 3 const int N = 110; 4 using namespace std; 5 vector<int> vs; 6 int fun(int x) { 7 int sum = x; 8 while(x > 0) { 9 sum += x%10; 10 x /= 10; 11 } 12 return sum; 13 } 14 int main() { 15 int n; 16 cin >> n; 17 for(int i = max(1,n-81); i < n; i ++) { 18 if(fun(i) == n) vs.push_back(i); 19 } 20 if((int)vs.size() == 0) return 0*printf("0\n"); 21 int len = vs.size(); 22 printf("%d\n", len); 23 for(int i = 0; i < len-1; i ++) { 24 printf("%d ", vs[i]); 25 } 26 printf("%d\n",vs[len-1]); 27 return 0; 28 }