hdu2802 F(N) (循环节)

来源:互联网 发布:802.1d端口路径开销 编辑:程序博客网 时间:2024/05/09 23:09

F(N)

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


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

数学题,遇事不决先打表,找规律,于是就发现循环节。

#include<iostream>using namespace std;#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stdlib.h>#include<vector>#include<queue>#include<deque>#include<map>#include<set>#include<time.h>#define pi(x) printf("%d",(x));#define pin(x) printf("%d\n",(x));#define si(x) scanf("%d",&(x))#define sii(x,y) scanf("d",&(x),&(y))#define s3(x,y,z) scanf("d%d",&(x),&(y),&(z))#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))#define sl(x) scanf("%lld",&(x))#define read int Tcase;scanf("%d",&Tcase);while(Tcase--)#define cls(x,y) memset((x),(y),sizeof((x)));#define cls0(x) memset((x),0,sizeof((x)));#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)#define GT(x) (x)=clock();typedef long long LL;typedef unsigned long long ULL;const int maxint=((~((unsigned)(0)))>>1);const LL maxll=((~((unsigned long long)(0)))>>1);const int inf=0x3f3f3f3f;const double PI=acos(-1.0);const int maxn=1e5+5;//int top,head[maxn];//struct note {//    int to,next;//} a[maxn];////void ADD(int u,int v) {//    a[top].next=head[u];//    a[top].to=v;//    head[u]=top++;//}LL a[maxn];const LL mod=2009;int main() {#ifdef tangge    clock_t tSTART,tEND,t3;    GT(tSTART);#endif // tangge    a[1]=1LL;    a[2]=7LL;    for(LL i=3; i<5000; ++i) {        a[i]=((a[i-2]-((i-1)%mod*(i-1)%mod*(i-1)%mod)+mod+mod)%mod+(i*i%mod*i)%mod)%mod;    }    int n;    while(si(n)&&n) {        cout<<a[n%4018]<<endl;    }#ifdef tangge    GT(tEND);    printf("%.8lf\n",(tEND-tSTART)/1000.0);#endif // tangge    return 0;}
0 0
原创粉丝点击