Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 86547    Accepted Submission(s): 20560

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
 
Author
CHEN, Shunbao
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1021 1019 1003 1009 1108
 
又是一道给出了运算公式的数学,凡是没有优化的话,超时超内存等等是避免不了的了。(错了好多次就是因为这,百度了下才明白)这题很显然是一个找规律的题目,也就是该题的求解中是存在循环节的。

  对于公式 f[n] = A * f[n-1] + B * f[n-2]; 后者只有7 * 7 = 49 种可能,为什么这么说,因为对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。由该关系式得知每一项只与前两项发生关系,所以当连续的两项在前面出现过循环节出现了,注意循环节并不一定会是开始的 1,1 。 又因为一组测试数据中f[n]只有49中可能的答案,最坏的情况是所有的情况都遇到了,那么那也会在50次运算中产生循环节。找到循环节后,就可以轻松解决了。
 
代码如下:
 #include <stdio.h>
#include <string.h> int main()
{
int a,b,n;
while(scanf("%d %d %d",&a,&b,&n),a||b||n)
{
int i,xh=;
int f[];
memset(f,,sizeof(f));
f[]=f[]=;
for(i=;i<;i++)
{
f[i]=(a*f[i-]+b*f[i-])%;
if(f[i]==&&f[i-]==)
break;
}
xh=i-;
n=n%xh;
if(n==)
n=xh;
//for(i=1;i<60;i++)
//printf("%d ",f[i]);
printf("%d\n",f[n]);
}
return ;
}

网上简洁做法:

http://www.189works.com/article-19050-1.html

 

hdu_1005_Number Sequence_201310222120的更多相关文章

随机推荐

  1. shell脚本-高级变量

    shell脚本-高级变量 字符串变量切片 ${#var}: 返回字符串变量var的长度 ${var:offset}: 返回字符串变量var中从第offset个字符后(不包括第offset 个字符)的字 ...

  2. Euclid(几何)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:已知A,B,C,D,E,F的坐标, ...

  3. Knights of the Round Table(Tarjan+奇圈)

    http://poj.org/problem?id=2942 题意:n个武士,某些武士之间相互仇视,如果在一起容易发生争斗事件.所以他们只有满足一定的条件才能参加圆桌会议:(1)相互仇视的两个武士不能 ...

  4. curl 采集的时候遇到60报错怎么办?

    1.到https://curl.haxx.se/ca/cacert.pem复制下文本粘贴到文件夹cart.pem 然后把catr.pem放到PHP的bin目录下 2.在php.ini中修改下面这句话, ...

  5. 【洛谷2257/BZOJ2820】YY的GCD(数论/莫比乌斯函数)

    题目: 洛谷2257 预备知识:莫比乌斯定理(懵逼乌斯定理) \(\mu*1=\epsilon\)(证bu明hui略zheng) 其中(我校学长把\(\epsilon(x)\)叫单位函数但是为什么我没 ...

  6. 服务器上oracle的监听设置

    1.查看本机的计算机名 2.修改etc/host 3.修改oracle的listener.ora(我服务器上的路径:) 4.修改tnsnames.ora(和上边文件一个目录)

  7. Nginx一个实现负载均衡的强大web server

    <转>nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 〉直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 〉另外一个 ...

  8. mysql数据库存储的引擎和数据类型

    一.查看支持的存储引擎 SHOW ENGINES \G; 或者 SHOW VARIABLES LIKE 'have%'; 二.安装版mysql的默认引擎是InnoDB,免安装版默认引擎是MyISAM ...

  9. C# Autofac 出现 尝试创建“XXController”类型的控制器时出错。请确保控制器具有无参数公共构造函数 错误解决方案

    出现以下错误: 总结解决方案: 本项目采用构造函数方法进行依赖注入,由于个人原因在业务层相互注入了接口,导致交叉:报错

  10. asp.net mvc 最简单身份验证 [Authorize]通过的标准

    [Authorize] public ContentResult Index2() { return Content("验证通过了"); } 经常能够看到某个Controler下的 ...