hdu 1021 Fibonacci Again ——同余的简单性质

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26755    Accepted Submission(s): 12950


Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
 

 

Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
 

 

Output
Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.
 

 

Sample Input
0 1 2 3 4 5
 

 

Sample Output
no no yes no no no
 
先预处理。根据同余的性质求解。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 int f[1000000];
 7 void pre(){
 8   f[0] = 7 % 3; f[1] = 11 % 3;
 9   for (int i = 2; i <= 1000000; ++i){
10     f[i] = (f[i-1] % 3 + f[i-2] % 3) % 3;
11   }
12 }
13 int main(void){
14   int n;
15 #ifndef ONLINE_JUDGE
16   freopen("hdu1021.in", "r", stdin);
17 #endif
18   pre();
19   while (~scanf("%d", &n)){
20     if (f[n]){
21       printf("no\n");
22     }
23     else printf("yes\n");
24   }
25   return 0;
26 }

水题……

posted on 2013-03-31 22:12  aries__liu  阅读(450)  评论(0编辑  收藏  举报