[观察]HDOJ1021Fibonacci Again

http://acm.hdu.edu.cn/showproblem.php?pid=1021
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

这个题最开始给出的n范围很大,然而后面并没有要求输出数字,只是要求输出yes or no。
这样就比较有意思了。
系统肯定会给出一个很大的数字用来测试,这样用int又超出了范围,用long int会出现超时。
所以我们想,可能存在输入输出的规律

通过找规律,发现
n 0 1 2 3 4 5 6 7 8 9 10
f(n) 7 11 18 29 47 76 123 199 322 521 843
余数 1 2 0 2 2 1 0 1 1 2 0
每8个数是一个循环,%8 == 2 和 %8 == 6的时候f(n)%3 == 0

#include <stdio.h> 
int main(void)  
{  
    int n;  
    while(scanf("%d", &n) != EOF)  
    {  
        if(n % 8 == 2 || n % 8 == 6)  
            printf("yes\n");  
        else  
            printf("no\n");  
    }  
    return 0;  
}  

同时每隔四个数据就出现一个yes
写的更简单一些,就是

#include <iostream> 

using namespace std;  

int main()  
{  
 int j;  
 while (cin >> j) {  
  if ((j+2)%4 == 0) puts("yes");  
  else puts("no");  
 }  
 return 0;  
}  

你可能感兴趣的:(ACM,杭电)