Number Sequence  HDU-1005

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

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
 
 
发这道题主要是祭奠一下我第一次遇到的超内存MLE。。。。。第一想法这么简单,暴力递归。。然后MLE狠狠打脸。。

后来再认真读题,发现还是有规律的。

如果f(n - 1)和f(n)都为1的话,就是一个循环。

#include <iostream>
#include <cstdio>
using namespace std;
int f[];
int main()
{
int a, b;
long long n;
while (scanf("%d %d %lld", &a, &b, &n) == )
{
if (a == && b == && n == )
break;
f[] = f[] = ;
int i;
for (i = ; i < ; i++)
{
f[i] = (a * f[i - ] + b * f[i - ]) % ;
if (f[i] == && f[i - ] == ) //找到循环因子 i
{
break;
}
}
n = n % (i - );
if (n == ) //刚好经过一个循环
printf("%d\n", f[i - ]);
else
printf("%d\n", f[n]);
}
return ;
}

这题真的是好烦。。。。。。。

 

HDU1005的更多相关文章

  1. hdu1005 矩阵

    //Accepted hdu1005 0MS 248K #include <cstdio> #include <cstring> #include <iostream&g ...

  2. 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757

    矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...

  3. HDU1005 找规律 or 循环点 or 矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=1005 1.一开始就注意到了n的数据范围 <=100 000 000,但是还是用普通的循环做的,自然TLE了 ...

  4. HDU1005(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 #include<cstdio> using namespace std; int ...

  5. HDU1005&&NEFU67 没有循环节

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

  6. hdu1005 Number Sequence(数论)

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

  7. HDU1005 数列找规律

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

  8. HDU1005(周期问题)

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

  9. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

随机推荐

  1. 如何用css3实现一个图片的抖动或者弹跳

    <li onmouseout="this.className='off'"><a href=""><img src=". ...

  2. 用crontab跑定时任务[转]

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  3. contiki学习笔记---process结构体

    process,字面意义,进程,看看它的结构 struct process { struct process *next; #if PROCESS_CONF_NO_PROCESS_NAMES #def ...

  4. canvas关于getImageData跨域问题解决方法

    一.问题:在使用html5的canvas是,当用到getImageData方法获取图片信息时,会碰到跨域无法获取的情况,代码如下: document.getElementById("pic& ...

  5. 转载: 查看HADOOP中一个文件有多少块组成及所在机器ip

    看文件信息 hadoop fsck /user/filename 更详细的 hadoop fsck /user/filename -files  -blocks -locations -racks   ...

  6. 用word制作电子书最简捷模式 支持epub和mobi目录

    因为制作一本OCR的电子书,转到word编辑排版后,用calibre转成mobi发现没有目录,在网上查了资料研究了一下,终于解决了目录问题,根本不用将word文档转换为什么htm或txt,尤其是转换t ...

  7. 用PHP+MySQL来做分页的演示

    用php做分页弄懂逻辑关系其实不难,不过我在听课的时候估计是被老师讲的那些变量里的英文单词给听懵了,因为有几个变量的名字都很像,只是换了两三个英文字母而已,有的就少几个这样的,听到一半已经不知道老师讲 ...

  8. Redis应用配置项说明

    近期整理了下项目中Redis配置参数,以便学习备用~ #指定内存大小,格式为1k 1GB 1M,单位不区分大小写 # 1k  => 1000 bytes # 1kb => 1024 byt ...

  9. mysql replication

    change master to master_host='192.168.0.3',master_user='myrepl',master_password='5rNUnOHxut3lkP4wXds ...

  10. iOS UIButton单双击处理响应不同的方法

    //显示目标 双击显示当前用户坐标位置 UIButton * btnShowDistination = [[UIButton alloc]initWithFrame:CGRectMake(, SCRE ...