题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005

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上进行测试http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=2603

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

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

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define swap(a,b) {(a)=(a)^(b); (b)=(a)^(b); (a)=(a)^(b);}
#define MAXN 65535
#define INF 1e9 int 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<i; j++){
if(f[i]==f[j]&&f[i-1]==f[j-1]){
term = i-j;
begin = j-2;
flag = 1;
break;
}
}
}
if(flag)
printf("%d\n", f[begin+(n-1-begin)%term+1]);
else
printf("%d\n", f[n]);
}
return 0;
}

1005 Number Sequence(HDU)的更多相关文章

  1. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  2. HDU 1005 Number Sequence (模拟)

    题目链接 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f( ...

  3. HDU 1005 Number Sequence(矩阵)

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

  4. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  5. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. 1005:Number Sequence(hdu,数学规律题)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  7. HDU - 1005 Number Sequence (矩阵快速幂)

    A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. Number Sequence (HDU 1711)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 善待Redis里的数据--Unable to validate object

    又是一篇关于姿势的文章,为什么是”又”呢?因为上个星期刚写完一篇关于Apache Commons Pool的正确使用姿势的文章,点击此处阅读. Redis为我们提供便利的同时,我们也要善待里面的数据 ...

  2. Mysql explain 查看分区表

    mysql> explain select * from ClientActionTrack where startTime>'2016-08-25 00:00:00' and start ...

  3. haproxy redirect prefix

    acl short_domain hdr(Host) -i etiantian.org redirect prefix http://www.etiantian.org code 301 if sho ...

  4. js算法

    最近面试可能会问这些 1,插入排序 function sort(elements){ var res =[elements[0]]; for (var i = 0; i < elements.l ...

  5. web api 上传

    using System.Net; using System.Net.Http; using System.Web; using System.Web.Http;   namespace FileUp ...

  6. 微信内移动前端开发抓包调试工具fiddler使用教程

    在朋友圈看到一款疯转的H5小游戏,想要copy,什么?只能在微信里打开?小样,图样图森破,限制了oauth.微信浏览器内打开,照样能看你源码~ 使用fiddler来抓包 需要先做一些简单的准备工作: ...

  7. Java基础笔记-String类2

    StringBuffer 特点: 是字符串缓冲区. 是一个容器,其长度可变,可以操作添加多个数据类型. 最后通过toString方法变成字符串. 被final锁修饰,因此不能被继承. 存储: 方法1: ...

  8. servlet+ajax+json字符串后台传入,前端解析并把数据循环填入表格实例

    写在前面:1.源代码来源于博客http://blog.sina.com.cn/u/2904067371 ,在此基础上对于前端代码稍作更改,把传过来的数据解析并传入表格2.json解析,用eval()3 ...

  9. Silverlight学习(五)图形标绘

    Silverlight中进行图形标绘的方法太多了,这里的标绘和Arcgis中的标绘不同,这里大多是静态的标绘.标绘的方法主要有Path(路径标记法).直接标绘(Line等).几何标记(LineGeom ...

  10. Ucenter整合Thinkphp 双向同步登录退出

    1.整合初步工作: 1,安装Ucenter,完成后添加应用,填写要对接的网站地址 2,api , uc_client目录放置对接项目的根目录 3,通信对接,新建Ucenter组,confi文件填写在u ...