hdu2802 循環節思維

F(N)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5026    Accepted Submission(s): 1788


Problem Description

Giving the N, can you tell me the answer of F(N)?
 

Input
Each test case contains a single integer N(1<=N<=10^9). The input is terminated by a set starting with N = 0. This set should not be processed.
 

Output
For each test case, output on a line the value of the F(N)%2009.
 

Sample Input
1 2 3 0
 

Sample Output
1 7 20
 

Source
解題思路:
這道題目算是給我提醒了一個方向,這道題其實不難,就是由於慣性思維,一拿到這個題目就在狂推公式,也用矩陣快速冪試了,發現都不得要得要點。
總的來說,循環節就像是數學中的周期函數,他的值只有一定範圍,過了這個範圍,就是重複的。例如周期函數:F(0)=1,F(1)=2,F(2)=3,F(3)=4,所以F(n)=F(n-1%4).
具體怎麼求出這個範圍,你可以打表出來看,寫一個循環,自己看規律。以後一般方法做不出來的時候可以往這方面想一想。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long f[110000];
void  init()
{
	int i;
	f[1]=1;
	f[2]=7;
	for(i=3;i<=5000;i++)
	f[i]=(f[i-2]+3*i*i-3*i+1)%2009;
}
int main()
{
	long long  n,i;
	init();
	while(scanf("%lld",&n),n)
	{
		//for(i=3;i<=110000;i=i+2)打表法找出循環節
		//if(f[i]==1)
		//break;
		//printf("%I64d\n",i-1);
		//for(i=4;i<=110000;i=i+2)
		//if(f[i]==7)
		//break;
		//printf("%I64d\n",i-2);
	    //printf("%I64d\n",f[4018]);
		printf("%lld\n",f[n%4018]);
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章