HDOJ 1021:Fibonacci Again 找出循环周期

来源:互联网 发布:sqlserver创建用户语句 编辑:程序博客网 时间:2024/04/27 16:17

     http://acm.hdu.edu.cn/showproblem.php?pid=1021

     F(n-1) 与 F(n-2)的值的组合只有9种,因此F(n)的循环周期至多是9.判定循环周期的条件是F(n)=F(1)&&F(n-1)=F(0),那么必有F(n+1)=F(2),于是循环也就开始出现了。

     我的AC代码。

    

#include<iostream>using namespace std;const int Max = 9;int d[Max] = {1, 2};int main(){int loop;for(int i=2; ; i++){d[i] = (d[i-1] + d[i-2]) % 3;if(d[i] == d[1] && d[i-1] == d[0]){loop = i - 1;break;} }int n;while(scanf("%d", &n) != EOF){if(!d[n%loop]) printf("yes\n");else printf("no\n");}system("pause");return 0;}