博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1005 Number Sequence(HDU)
阅读量:4699 次
发布时间:2019-06-09

本文共 1492 字,大约阅读时间需要 4 分钟。

题目链接:

 

Number Sequence

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

Total Submission(s): 85249    Accepted Submission(s): 20209

Problem Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
 

 

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
 

 

Output
For each test case, print the value of f(n) on a single line.
 

 

Sample Input
1 1 3 1 2 10 0 0 0
 

 

Sample Output
2 5

 

 

解析:

这是一道寻找循环点的问题,可能很多人在杭电上通过了这个题目,但是我建议大家将自己的代码再贴到另一个OJ上进行测试。

很多人都认为周期是49,但是给出的解题报告都不是很有说服力。

所以,我们可以寻找循环的开头以及周期,然后输出,这样能够保证正确性,当然一开始的记录数组最好能够相对大一些,不然仍然不能通过测试。

代码:

 

#include 
#include
#include
#include
#define min(a,b) (a
b?a:b)#define swap(a,b) {(a)=(a)^(b); (b)=(a)^(b); (a)=(a)^(b);}#define MAXN 65535#define INF 1e9int f[1200];int main(){ int a,b,n; int i, j; int flag, term, temp, begin; while(~scanf("%d%d%d", &a, &b, &n), (a||b||n)){ memset(f, 0, sizeof(f)); f[1]=1; f[2]=1; term = n; flag = 0; for(i=3; i<=n&&!flag; i++){ f[i] = (a*f[i-1]+b*f[i-2])%7; for(j = 2; j

 

转载于:https://www.cnblogs.com/suncoolcat/p/3331289.html

你可能感兴趣的文章
大道至简 7、8、读后感
查看>>
防盗链与token运用
查看>>
机器翻译论文
查看>>
两数据库Dblink数据抽取blob
查看>>
SpringMVC后台数据校验
查看>>
【腾讯优测干货分享】使用多张图片做帧动画的性能优化
查看>>
Timeout occurred while waiting for latch: class 'ACCESS_METHODS_DATASET_PARENT'
查看>>
笔记--Spring in action
查看>>
搭建顶级域名下的个人博客网站
查看>>
2012年7月新日本語能力試験N3、勉強している!
查看>>
图书管理系统需求分析说明书
查看>>
本地访问网站好使外网不好用 可能是防火墙端口
查看>>
LintCode: Triangle
查看>>
python核心编程:第六章。
查看>>
解决phpmailer可以在windows下面发送成功, 在linux下面失败的问题
查看>>
Sharing A Powerful Tool For Calculate Code Lines
查看>>
Java判断Class变量是什么类型
查看>>
javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片
查看>>
个人项目
查看>>
Java学习笔记——常用类
查看>>