现在的位置: 首页 > 综合 > 正文

HDOJ 1021:Fibonacci Again 找出循环周期

2018年02月01日 ⁄ 综合 ⁄ 共 446字 ⁄ 字号 评论关闭

     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;
}

抱歉!评论已关闭.