hdu---2802---F(N)

题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=2802

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
 
Mean:
f[n]=f[n-2]+3*n*n-3*n+1;
analyse:
本题的循环是4018.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=500009;
const int INF=0x3f3f3f3f;
const int mod=2009;
int f[maxn];
void Init()
{
    f[1]=1;
    f[2]=7;

    for(int i=3; i<4018; i++)
    {
        f[i]=f[i-2]+3*i*i-3*i+1;
        f[i]%=mod;
    }
}
int main()
{
    Init();
    int n;
    while(scanf("%d", &n), n)
    {
        printf("%d
", f[n%4018]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/w-y-1/p/5794520.html