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. 【Android Studio】没有先安装JDK

    如果没有先安装JDK,安装Android Studio的时候回出现下面这个界面: 请参考我整理的博客文章<JDK的下载.安装和配置>,链接:http://www.cnblogs.com/d ...

  2. 重载(Overloading)以及模板(Template)

    继续<C++ premier plus>的学习 (1)函数重载,通俗来说,就是相同的函数名字名下,存在多个函数,要使得这成立,各个同名函数必须形参列表(也称为"签名", ...

  3. Appium移动自动化测试(三)--安装Android模拟器(转)

    Appium移动自动化测试(三)--安装Android模拟器 2015-06-08 10:33 by 虫师, 30828 阅读, 9 评论, 收藏, 编辑 当Android SDK安装完成之后,并不意 ...

  4. POJ 2431 Expedition (STL 优先权队列)

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8053   Accepted: 2359 Descri ...

  5. Nginx+keepalived做双机热备加tomcat负载均衡

    Nginx+keepalived做双机热备加tomcat负载均衡 环境说明: nginx1:192.168.2.47 nginx2:192.168.2.48 tomcat1:192.168.2.49 ...

  6. migrate from weblogic to tomcat: directory mapping--reference

    Question: I am trying to migrate from weblogic to tomcat. in weblogic I have <virtual-directory-m ...

  7. 在Mac OS 中显示和隐藏系统文件

    使用终端 输入以下代码 defaults write com.apple.finder AppleShowAllFiles -bool YES 来显示隐藏了的文件 如usr/bin 把YES改成NO ...

  8. Java设计模式04:常用设计模式之建造者模式(创建型模式)

    1. Java之建造者模式(Builder Pattern) (1)概念:       将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. [ 构建与表示分离, 同构建不同表示 ] ...

  9. Linux开发工具之Makefile(上)

    二.makefile(上) 01.make工具   利用make工具可以自动完成编译工作.这些工作包括:如果修改了某几 个源文件,则只重装新编译这几个源文件:如果某个头文件被修改了,则 重新编译所有包 ...

  10. Day1 - Python基础1 介绍、基本语法、流程控制

    Python之路,Day1 - Python基础1   本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼 ...