题目链接:HDU 2009-4 Programming Contest
分析:具有一定的周期性——4018处理下就可以A了
Sample Input 1 2 3 0 Sample Output 1 7 20
AC代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<iostream> 6 #include<queue> 7 #include<map> 8 #include<stdlib.h> 9 10 using namespace std; 11 12 #define N 5000 13 #define INF 0x3f3f3f3f 14 #define Maxn 100010 15 16 int f[N]; 17 18 int main() 19 { 20 int i,n; 21 22 f[1]=1; 23 f[2]=7; 24 for(i=3; i<=N; i++) 25 f[i]=(f[i-2]+3*i*i-3*i+1)%2009; 26 27 while(scanf("%d", &n), n) 28 { 29 if(n>=4018) 30 n=n%4018; 31 32 printf("%d ", f[n]); 33 } 34 return 0; 35 }