首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >HDOJ 2802 F(N)

HDOJ 2802 F(N)

作者头像
谙忆
发布2021-01-21 11:00:49
发布2021-01-21 11:00:49
3080
举报
文章被收录于专栏:程序编程之旅程序编程之旅

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

一般这种让根据公式求出对应项的值得题都有规律 (有一个循环,此题的循环为4018(注意 这种有循环规律的是让你输出对应项对某个数取余后的题))可以先打表写出有限个数的结果,再观察规律,或者直接写代码判断是否进入了循环。

代码语言:javascript
复制
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    static int[] f = new int[10000];
    public static void main(String[] args) {
        f[1]=1;
        f[2]=7;
        for(int i=3;i<=4018;i++){
            f[i] = (f[i-2]-((i-1)*(i-1)*(i-1))+(i*i*i))%2009;
            //System.out.println(i+" "+f[i]);
        }
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0)
                return ;
//          for(int i=3;i<100000;i+=2){
//              if(f[i]==1&&f[i+1]==7){
//                  System.out.println(i-1);
//                  break;
//              }
//          }
            //判断多久开始循环
            System.out.println(f[n%4018]);
        }

    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/03/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档