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> ...
随机推荐
- jQuery实例2
下拉框实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 【转】ActiveMQ与虚拟通道
郑重提示,本文转载自http://shift-alt-ctrl.iteye.com/blog/2065436 ActiveMQ提供了虚拟通道的特性(Virtual Destination),它允许一个 ...
- LeetCode OJ 123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- NOIP2001-普及组复赛-第二题-最大公约数和最小公倍数问题
题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数条件: 1.P,A是正整数 ...
- linux安装配置solr
一.JDK的安装和配置 下载.解压jdk-7u79-linux-x64.gz 1.tar -zxvf jdk-7u79-linux-x64.gz -c /usr/java/ 解压到/usr/java/ ...
- SharePoint 2013 配置InfoPath 列表表单
转载来源:http://www.cnblogs.com/jianyus/p/3470113.html SharePoint列表,都是通过表单展示,有时候不太符合要求,这时候,我们可以通过定制表单,来是 ...
- ( ̄y▽ ̄)~ 智能手机II
( ̄y▽ ̄)~ 智能手机II TimeLimit: 3000/1000 MS (Java/Others) MenoryLimit: 32768/32768 K (Java/Others) 64-bi ...
- jqgird 实践
$.jgrid.defaults.styleUI="Bootstrap"; $("#table_list_2").jqGrid({ multiselect: t ...
- Meta标签中的属性及含义
一.Meta标签中的format-detection属性及含义 format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的forma ...
- mysql数据库root密码忘记的修改
注:此方法root的密码可以设置成功,但是重新开启服务时可能会出现中断的异常. 1.修改MySQL的root密码,需要先关闭MySQL的服务 2.进入命令行窗口,进入MySQL的安装路径bin目录下, ...