HDOJ 2802 F(N)

循环节4018

F(N)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3542    Accepted Submission(s): 1219

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

HDU 2009-4 Programming Contest

/* ***********************************************
Author        :CKboss
Created Time  :2014年12月14日 星期日 12时29分33秒
File Name     :HDOJ2802.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int mod=2009;

int f[5000];

int Get(int x)
{
	int part1=((x*x)%mod*x)%mod;
	int y=x-1;
	int part2=((y*y)%mod*y)%mod;
	return ((f[x-2]+part1)%mod+mod-part2)%mod;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int n;
	f[1]=1; f[2]=7;
	for(int i=3;i<=4018;i++) f[i]=Get(i);

	while(scanf("%d",&n)!=EOF&&n)
	{
		n=n%4018;
		if(n==0) puts("0");
		else printf("%d\n",f[n]);
	}

    return 0;
}
时间: 2024-10-25 10:04:26

HDOJ 2802 F(N)的相关文章

hdoj 2802 F(N)【递推 规律】

F(N) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4078    Accepted Submission(s): 1432 Problem Description Giving the N, can you tell me the answer of F(N)? Input Each test case contains a si

HDU 2802 F(N)

F(N) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3408    Accepted Submission(s): 1171 Problem Description Giving the N, can you tell me the answer of F(N)? Input Each test case contains a s

【HDOJ】2802 F(N)

找循环节水题.注意余数大于0. 1 /* 2802 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 4018 7 #define MOD 2009 8 9 __int64 a[MAXN+1]; 10 11 void init() { 12 __int64 tmp; 13 int i, j, k; 14 bool flag; 15 16 a[0]=0, a[

HDU 2802 F(N) 数论+打表

题目大意:f[n]-n^3=f[n-2]-(n-1)^3 (n >=3),f[1]=1,f[2]=7,求f[n]. 题目思路:将n^3移到到等式右边化简的到:f[n]=f[n-2]+3n*(n-1)+1: 因为第n项与第n-2项有关,所以知道了f[1]与f[2]的值可以分奇偶打下表,找到循环节为4018. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #incl

HDOJ 2582 f(n) (YY+找规律)

题意 Gcd(n)=gcd(C[n][1],C[n][2],--,C[n][n-1]) f(n)= Gcd(3)+Gcd(4)+-+Gcd(i)+-+Gcd(n). 求f(n). 思路 刚开始在gcd()的地方想消一下公式看看,发现Gcd(n)好像就等于n,然后python算了一下第二个样例发现不对,后来考虑可能有质因子的话会使gcd更小,就打了一个表,然后发现一共有三种情况: 1.如果n是素数,Gcd(n) = n 2.如果n是有唯一质因子,Gcd(n) = p 3.如果n有多个质因子,Gcd

F(N)---hdu2802(寻找循环节)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2802 f[1] = 1; f[2] = 7; f[n] = (f[n-2] - (n-1)*(n-1)*(n-1)+ n*n*n) % 2009, n>2; 给你一个n让你输出f[n];(n<1e9)由于n较大,所以不能直接输出,结果是对2009求余所以一定存在循环节,由于f(n)只与f(n-2)有关所以我们只需判断再次出现1或7即可: #include <stdio.h> #incl

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

HDOJ 5012 Dice--2014网络赛西安赛区F题

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5012 Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 307    Accepted Submission(s): 183 Problem Description There are 2 special dices on the