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;
    }
  • 相关阅读:
    CentOS7 linux下yum安装redis以及使用
    nssm 在windows上部署服务
    netcore 2.2 封装 AutoMapper
    git pull/push免密输入
    缓存常见问题
    批量维护关系数据
    测试跨域html
    ODBC配置
    spring boot监控之prometheus配置
    REST Client
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2673541.html
Copyright © 2011-2022 走看看