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. TXT四则运算计算器

    基本思想:使用getline函数从TXT文件中依次读出中缀表达式,将其转为后缀表达式后计算结果,并与用户结果比对. 整数.分数.小数的处理:将小数和整数都视为默认分母为1的分数.建立分数类,在中缀转换 ...

  2. selenium grid java 资料

    Grid TestNG: 使用Selenium Grid改进Web应用程序的测试: http://www.ithov.com/server/117464.shtml

  3. 提取HTML的正文类

    本文转载:http://blog.csdn.net/cjh200102/article/details/6824895 //2.提取html的正文 类 using System; using Syst ...

  4. C primer plus 读书笔记第十一章

    本章标题是字符串和字符串函数.主要是了解和字符串有关的函数. 1.字符串表示和字符串I/O 主要内容:字符串常量和字符串数组的初始化,对比了指针和字符串. 其中要注意的是,数组初始化是从静态存储区把一 ...

  5. [AngularJS] 5 simple ways to speed up your AngularJS application

    Nowdays, Single page apps are becoming increasingly popular among the fornt-end developers. It is th ...

  6. 开源实时视频码流分析软件:VideoEye

    本文介绍一个自己做的码流分析软件:VideoEye.为什么要起这个名字呢?感觉这个软件的主要功能就是对"视频"进行"分析".而分析是要用眼睛来看的,因此取了&q ...

  7. android studio c++ 自动补全

    这两天弄起来了Android ndk,可这东西的配置实在是个问题.对于Eclipse可以通过makefile进行编译,也比较成熟.但是对Android studio来说就蛋疼了,官方是想通过gradl ...

  8. [转] JavaScript中的字符串操作

    一.概述    字符串在JavaScript中几乎无处不在,在你处理用户的输入数据的时候,在读取或设置DOM对象的属性时,在操作cookie时,当然还有更 多....JavaScript的核心部分提供 ...

  9. Python开发【第一篇】:目录

    本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! Python开发[第一篇]:目录 Python开发[第二篇]:初识Python ...

  10. Android 连接 SQL Server (jtds方式)——下

    本文主要补充介绍jtds的查询方法,将以博主的一个实际开发程序进行说明 下图是项目的文件列表与界面效果:          运行效果: 1.三个EditText对应的是单个计划的序号.品种名.数量 2 ...