The 14th UESTC Programming Contest Final B - Banana Watch 预处理、前缀和
B - Banana Watch
Time Limit: 1000/1000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others)
As a famous technology company, Banana Inc. invents Banana Watch, redefining the watch.
While a normal watch has
The moving hand is at
in
How many seconds in the first minute?
Input
One integer
Sample input and outputOutput
Print the number of seconds in the first minute.
Sample Input | Sample Output |
---|---|
3 |
2 |
5 |
4 |
Hint
If
the hand moves to
My Solution
用sum[i]表示1~i的和,然后,从1 ~ maxn 查找。第一次出现if((sum[i] %= n) == 0) {printf("%d", i); break;}
然后考虑到数据范围。所以第一发有maxn = 2000000 + 1000,然后就过了。
假设用 1000000 + 8可能过也可能过不了。
#include <iostream> #include <cstdio> using namespace std; const int maxn = 2000000 + 1000; long long sum[maxn]; int main() { int n; scanf("%d", &n); sum[0] = 0; for(int i = 1; i < maxn; i++){ sum[i] += sum[i-1]+i; if((sum[i] %= n) == 0) {printf("%d", i); break;} } return 0; }
Thank you!