Codeforces Round #262 (Div. 2)

题目链接:http://codeforces.com/contest/460

A. Vasya and Socks

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?

Input

The single line contains two integers n and m (1 ≤ n ≤ 100; 2 ≤ m ≤ 100), separated by a space.

Output

Print a single integer — the answer to the problem.

题意:给出一个n,m,每天n减1,当天数达到m天的倍数的时候,n加1,每天的n的值不能为0和负,问最后最多能达到多少天。

解法:直接模拟

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 int main()
11 {
12     int n,m;
13     while (scanf("%d%d",&n,&m)!=EOF)
14     {
15         int count=0;
16         int sum=n;
17         int ans=0;
18         while (sum)
19         {
20             sum--;ans++;
21             count++;
22             if (count==m) {sum++;count=0; }
23         }
24         cout<<ans<<endl;
25     }
26     return 0;
27 }
B. Little Dima and Equation
time limit per test
1 second
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:

x = b·s(x)a + c, 

where abc 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: abc. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.

Input

The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000).

Output

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.

解法:枚举x显然不行的,我们可以枚举s(x),(1<=s(x)<=81),然后根据s(x)的值和a,b,c可以求得x ,然后判断x的所有位上的数的和是否等于此时枚举s(x)的值.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const int maxn=100;
11 LL an[maxn];
12 int main()
13 {
14     LL a,b,c;
15     LL num=1e9;
16     //cout<<num<<endl;
17     while (scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
18     {
19         LL ans=0;
20         for (LL i=1 ;i<=81 ;i++)
21         {
22             LL j=a;
23             LL k=1;
24             while (j--) k*=i;
25             k=k*b+c;
26             int flag=0;
27             LL k1=k,cnt=0;
28             //cout<<k<<" "<<i<<endl;
29             while (k1) {cnt+=k1%10;k1/=10; }
30             if (k<num && k>0 && cnt==i) {an[ans++]=k; }
31         }
32         cout<<ans<<endl;
33         for (LL i=0 ;i<ans ;i++)
34         {
35             if (!i) printf("%I64d",an[i]);
36             else printf(" %I64d",an[i]);
37         }
38         printf("
");
39     }
40     return 0;
41 }
C. Present
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.

There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?

Input

The first line contains space-separated integers nm and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the maximum final height of the smallest flower.

题意比较好懂。

解法:二分最终答案+贪心

问:我一直不知道在贪心的时候为什么要判断浇花的次数大于m的时候就必须要退出了,循环完了后直接判断浇花次数ans是否大于m不可以吗?如果有知道的朋友,感谢您的留言

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const int maxn=100000+100;
11 int an[maxn],bn[2*maxn];
12 int n,m,w;
13 int search(int mid)
14 {
15     memset(bn,0,sizeof(bn));
16     int ans=0;
17     int now=0;
18     for (int i=0 ;i<n ;i++)
19     {
20         now -= bn[i];
21         int k=mid-an[i]-now;
22         if (k>0)
23         {
24             //int k=mid-an[i]-now;
25             bn[i+w]=k;
26             now += k;
27             ans += k;
28         }
29         if (ans>m) break;
30     }
31     if (ans<=m) return 1;
32     return 0;
33 }
34 int s[2*maxn];
35 int possible(int k){
36     int p = 0, d = 0;
37     int mm = m;
38     memset(s, 0, sizeof s);
39     for(int i = 0; i < n && mm>=0; i++){
40         p -= s[i];
41         d = (k - an[i]) - p;
42         if(d>0){
43         //d=k-an[i]-p;
44         mm -= d;
45         s[i + w] = d;
46         p += d;
47         }
48     }
49     if (mm>=0) return 1;
50     return 0;
51 }
52 int main()
53 {
54 
55     while (scanf("%d%d%d",&n,&m,&w)!=EOF)
56     {
57         int l=inf,r=0;
58         for (int i=0 ;i<n ;i++) {
59             scanf("%d",&an[i]);
60             l=min(l,an[i]);
61             r=max(r,an[i]);
62         }
63         r += m;
64         //cout<<l<<" "<<r<<endl;
65         int k=l;
66         //l=0;r=1e9+500000;
67         while (l<=r)
68         {
69             int mid=(l+r)/2;
70             if (search(mid)) {l=mid+1;k=mid;}
71             else r=mid-1;
72             //cout<<l<<" "<<mid<<" "<<r<<endl;
73         }
74         printf("%d
",k);
75     }
76     return 0;
77 }
原文地址:https://www.cnblogs.com/huangxf/p/3926741.html