zoukankan      html  css  js  c++  java
  • HDU 2802 F(N)(简单题,找循环解)

    F(N)

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


    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
     
    Recommend
    lcy
     
     
     
    以后遇到这样的题目,第一感觉就应该去找循环解。
    其实循环解也挺好找出来的。打表,程序中判断下就出来循环了。
    本题的循环是4018.
    至于为什么4018是循环,现在还没有想出来,感觉挺神奇的
    /*
    找循环周期
    周期是4018,只要模4018就可以了。f[4018]=f[0]
    */
    
    #include<stdio.h>
    const int MOD=2009;
    const int MAXN=10000;
    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()
    {
        //freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        init();
        int n;
        while(scanf("%d",&n),n)
        {
            printf("%d\n",f[n%4018]);
        }
        return 0;
    }
  • 相关阅读:
    P1478 陶陶摘苹果(升级版)
    洛谷 P1008 三连击
    1412:二进制分类
    1411:区间内的真素数
    判断素数
    关于小数
    一本通题库1159斐波那契数列
    一本通题库1161转进制
    一本通题1051
    基础算法——数据排序——冒泡
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2673541.html
Copyright © 2011-2022 走看看