zoj 2060 Fibonacci Again(fibonacci数列规律、整除3的数学特性)

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2060

题目描述:

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 /*
 2 问题 对于定义的斐波那契数列,查询第n项是否能被3整除
 3 解题思路 首先直观的解法是将前100 0000项计算出来存在表里,再查询计算,但是直接计算的话100 0000项的斐波那契数
 4 是非常大的的,基本数据类型都是不够用的
 5 利用特性“一个数的各位数字之和能被3整除,那么这个数就能被3整除”,那么对每一位先对3取余,建立一个100 0000
 6 项的表,最后查询计算就可以了
 7 另外一个投机取巧的方法是可以发现每隔三个no就会出现一个yes,那么根据结果发现规律如果n%4 == 2则是no,否则是yes
 8 */ 
 9 #include <vector>
10 #include <cstdio>
11 using namespace std;
12 
13 int main()
14 {
15     vector<int> v;
16     int n1,n2,t,i;
17     n1=7%3;
18     n2=11%3;
19     v.push_back(n1);
20     v.push_back(n2);
21     for(i=2;i<=1000000;i++){
22         t=(n1+n2) %3;
23         v.push_back(t);
24         n1=n2;
25         n2=t;
26     }
27     int n;
28     while(scanf("%d",&n) != EOF)
29     {
30         if(v[n] % 3 == 0)
31             printf("yes
");
32         else
33             printf("no
");
34     }
35     /*while(scanf("%d",&n) != EOF)
36     {
37         if(n % 4 == 2)
38             printf("yes
");
39         else
40             printf("no
");
41     }*/ 
42     return 0;
43 }
原文地址:https://www.cnblogs.com/wenzhixin/p/8543690.html