HDU 2802 F(N)(简单题,找循环解)
题目链接
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)(简单题,找循环解)的更多相关文章
- HDU 6463.超级无敌简单题-卡边界的暴力 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)
超级无敌简单题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU - 2802 F(N) (周期)
题目链接:HDU 2009-4 Programming Contest 分析:具有一定的周期性——4018处理下就可以A了 Sample Input Sample Output AC代码: #incl ...
- 数学--数论--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 ...
- 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- ...
- hdu 1005 简单题
今早水出的第一道题,带着情绪做的,竟然1Y了,确实惊奇.这道简单的线性递推取模,直接递推是不行的,因为n的规模达到了100,000,000,要么超时要么超内存.可以用矩阵快速幂来搞,根据题意构建出对应 ...
- HDU 6106 17多校6 Classes(容斥简单题)
Problem Description The school set up three elective courses, assuming that these courses are A, B, ...
- HDU 1753 大明A+B(字符串模拟,简单题)
简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...
- (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)
题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【HDOJ】2802 F(N)
找循环节水题.注意余数大于0. /* 2802 */ #include <cstdio> #include <cstring> #include <cstdlib> ...
随机推荐
- 判断一个数是否为2的n次幂
参考:http://bbs.csdn.net/topics/370058619 如题,如何判断一个整数是否是2的N次方,我能想到的方法有两个 1.一直除2,看最后是否等于1.(最笨的方法) 2.转换成 ...
- ElasticSearch(1)-入门
下一篇 Elastic Search基础(2) 相关文档: Gitbook[中文未完整]: http://learnes.net/ Gitbook[英文完整]:https://allen8807.gi ...
- linux双线ip设置(不需额外增加路由表)
linux 双线ip设置(不需额外增加路由表,只需修改下面就ok了)修改 vi /etc/iproute2/rt_tables (增加电信和网通两个路由表) 增加252 ...
- ural 1698. Square Country 5(记忆化搜索)
1698. Square Country 5 Time limit: 2.0 secondMemory limit: 64 MB The first arithmetical operation ta ...
- 杭电21题 Palindrome
Problem Description A palindrome is a symmetrical string, that is, a string read identically from le ...
- mybatis遇见的奇葩问题(返回null)
1.问题描述 select 语句没有问题,执行完毕后通过日志也可以看出 select到数据了,但是拿到的值就是null 2.原因 原来是有人将对象变量命名给改了,导致select到结果后不能映射成为对 ...
- InnoDB表要建议用自增列做主键
InnoDB引擎表是基于B+树的索引组织表(IOT): 每个表都需要有一个聚集索引(clustered index): 所有的行记录都存储在B+树的叶子节点(leaf pages of the tre ...
- Sql Sever语句 (续2)
日期时间 把日期类型的字段下默认值或绑定里的 设置成getdate() ,新建字段时候不设置会自动获取当前服务器时间 在当前时间加上xx年xx月xx天 select datedd(yy,100,g ...
- HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)
这个题目的数据应该是比较弱的,赛场上的时候我们暴力也过了,而且我的kmp居然比暴力还要慢-- 这个变形并不难,跳着选数,把漏掉的位置补上就可以了. 代码如下: #include<iostream ...
- ios简单实现如果没有开启定位,提示开启系统软件定位功能
if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == ...