Codeforces Round #353 (Div. 2) A. Infinite Sequence

题目链接:

http://codeforces.com/contest/675/problem/A

题意:

给你一个等差数列,问你b是不是其中的一项。

即是否存在大于等于0的整数k使得a+k*c==b。只要求(b-a)%c==0&&(b-a)/c>=0就可以了。

但是,我就是写成了二分。。而且还写搓了。。

先看看哇的代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 
 7 const int maxn = 333;
 8 const int INF = 1e9;
 9 
10 int main() {
11     int a, b, c;
12     while (scanf("%d%d%d", &a, &b, &c) == 3) {
13         if (c == 0) {
14             if (b == a) puts("YES");
15             else puts("NO");
16             continue;
17         }
18         int low, hig,flag;
19         if (c < 0) {
20             flag = 0;
21             low=0, hig = (-INF - a) / c + 10;
22             if (b > a) {
23                 puts("NO"); continue;
24             }
25             while (low < hig-1) {
26                 int mid = low + (hig - low) / 2;
27                 if (a + mid*c>b) {
28                     low = mid + 1;
29                 }
30                 else if (a + mid*c < b) {
31                     hig = mid;
32                 }
33                 else {
34                     flag = 1; break;
35                 }
36             }
37             if (flag) puts("YES");
38             else puts("NO");
39         }
40         else if (c>0) {
41             flag = 0;
42             low = 0, hig = (INF - a) / c + 10;
43             if (b < a) {
44                 puts("NO"); continue;
45             }
46             while (low < hig - 1) {
47                 int mid = low + (hig - low) / 2;
48                 if (a + mid*c<b) {
49                     low = mid + 1;
50                 }
51                 else if (a + mid*c > b) {
52                     hig = mid;
53                 }
54                 else {
55                     flag = 1; break;
56                 }
57             }
58             if (flag) puts("YES");
59             else puts("NO");
60         }
61     }
62     return 0;
63 }

以后写二分以此为鉴。。

贴修改之后的:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 
 7 const int maxn = 333;
 8 typedef long long LL;
 9 const LL INF = 1e9;
10 
11 int main() {
12     int a, b, c;
13     while (scanf("%d%d%d", &a, &b, &c) == 3) {
14         if (c == 0) {
15             if (b == a) puts("YES");
16             else puts("NO");
17             continue;
18         }
19         LL low, hig;
20         if (c < 0) {
21             low = 0, hig = (-INF - a) / c + 10;
22             if (b > a) {
23                 puts("NO"); continue;
24             }
25             while (low < hig - 1) {
26                 LL mid = low + (hig - low) / 2;
27                 if (a + mid*c>=b) {
28                     low = mid;
29                 }
30                 else if (a + mid*c < b) {
31                     hig = mid;
32                 }
33             }
34             if (a + low*c == b) puts("YES");
35             else puts("NO");
36         }
37         else if (c>0) {
38             low = 0, hig = (INF - a) / c + 10;
39             if (b < a) {
40                 puts("NO"); continue;
41             }
42             while (low < hig - 1) {
43                 LL mid = low + (hig - low) / 2;
44                 if (a + mid*c<=b) {
45                     low = mid;
46                 }
47                 else if (a + mid*c > b) {
48                     hig = mid;
49                 }
50             }
51             if (a + low*c == b) puts("YES");
52             else puts("NO");
53         }
54     }
55     return 0;
56 }

 

posted @ 2016-05-24 01:01  fenicnn  阅读(150)  评论(0编辑  收藏  举报