hdoj 2802 F(N)【递推 规律】

F(N)

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


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
 
一般这种让根据公式求出对应项的值得题都有规律   (有一个循环,此题的循环为4018(注意  这种有循环规律的是让你输出对应项对某个数取余后的题))可以先打表写出有限个数的结果,再观察规律
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define LL long long
#define MAX 10010
using namespace std;
LL s[MAX];
LL p[MAX];
void biao()
{
	int i,j;
	s[1]=p[1]=1;
	s[2]=p[2]=7;
	for(i=3;i<=4020;i++)
	{
		s[i]=s[i-2]+pow(i,3)-pow((i-1),3);
		p[i]=s[i]%2009;
	}
}
int main()
{
	int n,m,i,j;
	biao();
	while(scanf("%d",&n),n)
	{
		printf("%lld
",p[n%4018]); 
	}
	return 0;
}

  

 
 
原文地址:https://www.cnblogs.com/tonghao/p/4950491.html