HDU 1005

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
题解:

一开始看到这题我就直接跳过了。。

但是后来发现还是有些规律的,对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。由该关系式得知每一项只与前两项发生关系,所以当连续的两项在前面出现过循环节出现了,注意循环节并不一定会是开始的 1,1 。 又因为一组测试数据中f[n]只有49中可能的答案,最坏的情况是所有的情况都遇到了,那么那也会在50次运算中产生循环节。找到循环节后,就可以解决此题

注意:周期的大小,有可能是大周期,有可能是小周期!
#include<iostream>
using namespace std;
int f[]={,,};
int main()
{
int A,B,n,q=;
while(cin>>A>>B>>n&&A&&B&&n)
{
for(int i=;i<;++i)
{
f[i]=(A*f[i-]+B*f[i-])% ;
if(i>)
{if(f[i-]==f[]&&f[i]==f[])
{
q=i-; //要特别注意,可以想一下为什么? }
}
}
cout<<f[n%q]<<endl; } return ;
}
 

HDU 1005(周期问题)的更多相关文章

  1. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  2. HDU 1005 Number Sequence(数列)

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

  3. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  4. 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...

  5. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

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

  6. hdu 1005

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 思路:找规律题 #include<stdio.h> main() { ]; int ...

  7. hdu 5037 周期优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5037 有只青蛙踩石子过河,河宽m,有n个石子坐标已知.青蛙每次最多跳L.现在可以在河中再放一些石子,使得青蛙过河 ...

  8. HDU 1005 Number Sequence:矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...

  9. hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1005 代码: #include<iostream> #include<stdio.h&g ...

随机推荐

  1. ActionFilterAttribute OnActionExecuting 获取参数

    public class AuthFilterAttribute : ActionFilterAttribute { /// <summary> /// Action执行前处理 /// & ...

  2. C++ static(施工中)

    static 变量 头文件中的static会在引用该头文件的cpp中分别生成副本 //H.h #ifndef _H_H_ #define _H_H_ ; #endif //Ex_2.c #includ ...

  3. poj 2187 凸包加旋转卡壳算法

    题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...

  4. THOMAS MASON--英国顶级衬衫面料品牌

    欧尚时光 THOMAS MASON--英国顶级衬衫面料品牌 2014-01-26 欧尚时光      今天为大家介绍英国顶级衬衫面料厂商:THOMAS MASON(托马斯·梅森).THOMAS MAS ...

  5. 考研路茫茫--单词情结 - HDU 2243(AC自动机+矩阵乘法)

    分析:与poj的2778差不多的,求出来所有的情况然后减去不包含的就行了,这次使用了一下kuangbin的那种自动机写法,确实还不错,因为尤是在建立矩阵的时候更加方便.   代码如下: ======= ...

  6. How many ways?? - hdu2157(矩阵快速幂-模板)

    分析:求Map^k,刚开始没有用快速幂,TLE了   代码如下: =================================================================== ...

  7. App抓包数据包之Paros的安装及使用

    1.在应用程序开发过程中,会遇到很多网络访问问题,自己没有能力提供后台服务,这时就可以抓取网络上的数据包,获取数据接口,应用在程序中.下面介绍使用paros抓取网络数据包得步骤. 2.要使用paros ...

  8. Shiro Quartz之Junit測试Session管理

    Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和Qu ...

  9. 完美解决Android完全退出程序(转)

    背景:假说有两个Activity, Activity1和Activity2, 1跳转到2,如果要在2退出程序,一般网上比较常见的说法是用 System.exit(0) 或是 android.os.Pr ...

  10. 初学 Canvas <第一篇-基础篇>

    本文摘自:兴趣部落大神(为你一生画眉)-讲一讲canvas究竟是个啥? HTML5 的标准已经出来好久了,但是似乎其中的 Canvas 现在并没有在太多的地方用到.一个很重要的原因是,Canvas 的 ...