A. Vasya and Socks
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, ...) mom buys a pair of socks to Vasya. She does it late in the evening, so that Vasya cannot put on a new pair of socks before the next day. How many consecutive days pass until Vasya runs out of socks?
The single line contains two integers n and m (1 ≤ n ≤ 100; 2 ≤ m ≤ 100), separated by a space.
Print a single integer — the answer to the problem.
2 2
3
9 3
13
In the first sample Vasya spends the first two days wearing the socks that he had initially. Then on day three he puts on the socks that were bought on day two.
In the second sample Vasya spends the first nine days wearing the socks that he had initially. Then he spends three days wearing the socks that were bought on the third, sixth and ninth days. Than he spends another day wearing the socks that were bought on the twelfth day.
题意 : 初始有n双袜子,妈妈每m天给他买一双袜子,他每天早上穿新袜子,晚上扔掉,问多少天之后他没有袜子穿。
思路 : 我的思路是循环模拟。看了官方解题报告最终答案是n+(n-1)/(m-1).
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 5 using namespace std ; 6 7 int main() 8 { 9 int n,m ; 10 while(~scanf("%d %d",&n,&m)) 11 { 12 int cnt = 1; 13 while(1) 14 { 15 n -- ; 16 if(cnt % m == 0) 17 n ++ ; 18 if(n == 0) break ; 19 cnt ++ ; 20 } 21 printf("%d ",cnt) ; 22 } 23 return 0 ; 24 }
B. Little Dima and Equation
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.
Find all integer solutions x (0 < x < 109) of the equation:
where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x.
The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.
The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000; - 10000 ≤ c ≤ 10000).
Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109.
3 2 8
3
10 2008 13726
1 2 -18
0
2 2 -1
4
1 31 337 967
题意 : 给你abc,找出所有满足这个式子的数x = b·s(x)a + c,其中s(x)代表的是把x的每一位加起来。
思路 :枚举s(x),因为x最大是10^9,所以s(x)最大是81,所以枚举s(x)的时候求出x,判断x的每一位相加是否等于s(x)。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <string.h> 5 #include <algorithm> 6 #define LL __int64 7 8 using namespace std ; 9 10 LL sh[101010] ; 11 12 LL poww(int a,int n) 13 { 14 LL s = 1 ; 15 for(int i = 1 ; i <= n ; i++) 16 s *= (LL)a ; 17 return s; 18 } 19 int main() 20 { 21 int a,b,c ,cnt; 22 while(~scanf("%d %d %d",&a,&b,&c)) 23 { 24 cnt = 0 ; 25 memset(sh,0,sizeof(sh)) ; 26 for(int i = 1 ; i <= 81 ; i++) 27 { 28 LL sum = 0 ,sum1 = 0; 29 sum1 = b*poww(i,a)+c ; 30 LL sum2 = sum1 ; 31 while(sum2) 32 { 33 sum += sum2%10 ; 34 sum2 /= 10 ; 35 } 36 if(sum == i && sum1 < 1e9) 37 sh[cnt ++] = sum1 ; 38 } 39 printf("%d ",cnt) ; 40 if(cnt) 41 { 42 for(int i = 0 ; i < cnt-1 ; i++) 43 printf("%I64d ",sh[i]) ; 44 printf("%I64d ",sh[cnt-1]) ; 45 } 46 } 47 return 0 ; 48 }