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. HDU 5970 最大公约数

    中文题 题意: 思路: 1.观察可得 模m的同余系和m的gcd都相同(这题多了一个c也是相同的) 2.由于取证所以不能用简单的用O(m^2)的做法,涉及到多1少1的 3.打表观察,例如i为模9为7的数 ...

  2. Kindle DXG和Win10 64bits无法连接的问题

    直入主题:换根数据线. 不要觉得答案简单,我就是不负责任的在调侃. 在得出这条答案之前,我的思路是,既然插上线以后,kindle的充电指示灯会亮,那就应该不是线的问题. 所以实际的过程是我安装了驱动之 ...

  3. Javascript 中的神器——Promise

    Promise in js 回调函数真正的问题在于他剥夺了我们使用 return 和 throw 这些关键字的能力.而 Promise 很好地解决了这一切. 2015 年 6 月,ECMAScript ...

  4. Python之路,day12-Python基础

    Mysql数据库操作 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据 ...

  5. TFS下的源代码控制

    以下主要描述了: TFS源代码控制系统的基本场景 如何把一个项目添加到源代码管理中 如何与服务器同步 如何做Check-In 如何做分支与合并 什么是上架与下架 我们知道工作项是项目管理的基本元素,但 ...

  6. DB2 中文排序问题

    本地测试库中 代码集: GBK 数据库配置发行版级别 = 0x0c00 数据库发行版级别 = 0x0c00 数据库地域 = CN 数据库代码页 = 1386 数据库代码集 = GBK 数据库国家/地区 ...

  7. 支付宝支付参数MD5签名

    protected String signMD5(List<String> paramNames, String key, String charset) throws Unsupport ...

  8. jsp添加背景音乐

    在<head></head>间假如标签<embed src="文件地址" loop="11" autostar="tru ...

  9. MySql启动,提示:Plugin 'FEDERATED' is disabled....Cannot allocate memory for the buffer pool

    2016-05-27 09:25:01 31332 [Note] Plugin 'FEDERATED' is disabled. 2016-05-27 09:25:01 31332 [Note] In ...

  10. Linux查找含有某字符串的所有文件

    转自:http://151wqooo.blog.51cto.com/2610898/1162118 如果你想在当前目录下 查找"hello,world!"字符串,可以这样: gre ...