题目链接

F(N)

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

Total Submission(s): 4579 Accepted Submission(s): 1610

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

/*
这类题一般有规律,比如是循环,可以化简成等差、等比数列。
此题用程序跑出循环节,在对n取run的模就可以了。
跑循环节的时候也要取模%2009.
*/
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
LL f[1000000];
int run;//循环节
int n;
void init()//找循环
{
f[1]=1;
f[2]=7;
for(int i=3;;i++)
{
f[i]=(f[i-2]+3*i*i-3*i+1)%2009;
if(f[i]==f[1])
{
bool fg=true;
int k=(i-1)*2;
int pos=1;
for(;i<=k;i++)
{
f[i]=(f[i-2]+3*i*i-3*i+1)%2009;
if(f[i]!=f[pos])
{
fg=false;
break;
}
pos++;
}
if(fg)
{
run=k/2;
break;
}
}
}
}
int main ()
{
init();
while(scanf("%d",&n),n)
{
printf("%lld\n",f[n%run]); }
return 0;
}

HDU 2802 F(N)(简单题,找循环解)的更多相关文章

  1. HDU 6463.超级无敌简单题-卡边界的暴力 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    超级无敌简单题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. HDU - 2802 F(N) (周期)

    题目链接:HDU 2009-4 Programming Contest 分析:具有一定的周期性——4018处理下就可以A了 Sample Input Sample Output AC代码: #incl ...

  3. 数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂

    Giving the N, can you tell me the answer of F(N)? Input Each test case contains a single integer N(1 ...

  4. 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- ...

  5. hdu 1005 简单题

    今早水出的第一道题,带着情绪做的,竟然1Y了,确实惊奇.这道简单的线性递推取模,直接递推是不行的,因为n的规模达到了100,000,000,要么超时要么超内存.可以用矩阵快速幂来搞,根据题意构建出对应 ...

  6. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  7. HDU 1753 大明A+B(字符串模拟,简单题)

    简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...

  8. (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)

    题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. 【HDOJ】2802 F(N)

    找循环节水题.注意余数大于0. /* 2802 */ #include <cstdio> #include <cstring> #include <cstdlib> ...

随机推荐

  1. 四、WCF的配置文件

    注:本文为学习摘抄,原文地址:http://www.cnblogs.com/iamlilinfeng/archive/2012/10/02/2710224.html 一.概述 配置也是WCF编程中的主 ...

  2. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...

  4. 移动端touch事件获取clientX, clientY

    目有个交互需要实现手指滑动的交互,pc端使用mousedown,mousemove,mouseup监听实现. 但在ios设备上mousemove是不好监听的,同类的方法是touchstart,touc ...

  5. tomcat安装完设定用户名和密码

    vi conf/tomcat-user.xml<tomcat-users> <role rolename="manager"/> <role role ...

  6. 笨方法学python--读写文件

    1 文件相关的函数 close read readline  读取文本文件中的一行 truncate  清空文件 write('adb') 写入 2 写文件,首先要在open时,写入权限w targe ...

  7. mongoDB4--mongoDB的增删改查

    MongoDb基本操作之增删改查我们知道传统关系型数据库的最常用操作就是"增加/删除/修改/查询",也就是传说中的CRUD(create/remove/updte/delete). ...

  8. 常见的jquery一些效果

    1.CSS渐变:background: linear-gradient(to bottom right, #999 , #eee);

  9. Linux Shell 小脚本经典收藏

    原文:http://www.cnblogs.com/Javame/p/3867686.html 1.在两个文件中找出相同的号码 diff -y xx.txt oo.txt | egrep -v &qu ...

  10. akka 入门

    http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...