HDU 1021 Fibonacci Again

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

题意就是判断a[n]是否为3 的倍数。是,输出yes,否则,输出no。

若直接相加数据就会很大,每个数都直接对3取余再运算,这样a[n]的值只在0 1 2之间徘徊

#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<string> using namespace std; __int64 a[1000001]= {1,2}; int main() {     for(int i=2; i<=1000000; i++)     {         a[i]=a[i-1]+a[i-2];         a[i]=a[i]%3;     }     int n;     while(cin>>n)     {         if(a[n]==0)             cout<<"yes"<<endl;         else             cout<<"no"<<endl;     } }

你可能感兴趣的:(ACM,HDU,找规律)