Number Sequence

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

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
 
Author
CHEN, Shunbao
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1008 1021 1019 1003 1009 
 
 
/*-----------------------f[i-1]%7与f[i-2]%7都有0,1,2,3,4,5,6七种可能的取值,所以f[i]的取值最大有49种,也就是在49次计算
内,至少出现一次循环,当f[i-1]与f[i]等于1(f[1],f[2]等于1)的时候,即为循环出现,周期cycle = i - 2;----------------------*/
 
 
 

#include<iostream>
using namespace std;

int main()
{
int cycle,i; //周期
int f[50];
int a,b,n;
f[1] = f[2] = 1;
while(cin>>a>>b>>n,a+b+n)
{
for( i = 3;i <= 49;i++)
{
f[i] = (a*f[i-1] + b*f[i-2])%7;
if(f[i] == f[i-1] && f[i] == 1) break;
}
cycle = i - 2;
n = n%cycle;
if(n==0)
cout<<f[cycle]<<endl;
else
cout<<f[n]<<endl;
}
return 0;
}

 

HDU 1005 Number Sequence的更多相关文章

  1. HDU 1005 Number Sequence(数列)

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

  2. HDU 1005 Number Sequence(数论)

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

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

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

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

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

  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 Number Sequence (模拟)

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

  7. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

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

  8. HDU 1005 Number Sequence(矩阵)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU - 1005 Number Sequence (矩阵快速幂)

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

随机推荐

  1. 设置float之后vertical-align失效

    相关博文: 关于Vertical-Align你需要知道的事情 https://segmentfault.com/a/1190000002668492

  2. CSUOJ_1002

    /* * Title : A+B(III) * Data : 2016/11/09 * Author : Andrew */ #include <iostream> #include &l ...

  3. 转:Eclipse快捷键 10个最有用的快捷键

    Eclipse快捷键 10个最有用的快捷键 Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代 ...

  4. linux建立一个快捷方式,连接到另一个目录

    sudo ln -s 源目录 目标快捷方式比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是sudo ln -s /home/LXBC /etc/LXBC553

  5. gulp入门教程(详细注解)

    本文转载自http://www.ydcss.com/archives/18 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很 ...

  6. SPSS数据分析—因子分析

    我们知道主成分分析是一种降维方法,但是其本质上只是一种矩阵变换的过程,提取出来的主成分并不都具有实际含义,而这种含义往往是我们所需要的,接下来的因子分析可以解决这个问题 因子分析可以看做是主成分分析的 ...

  7. hihocoder-平衡树·SBT

    http://hihocoder.com/problemset/problem/1337 #1337 : 平衡树·SBT 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 ...

  8. php 二维数组排序

    usort($info ,function($a,$b){ $a1 = $a['score']; $b1 = $b['score']; if($a1 == $b1) return 0; return ...

  9. ThinkPHP 3.2.3(四)架构之多层MVC

    一.模型(Model)层 1.ThinkPHP支持多层Model,不同的模型层都继承自系统的Model类.   2.模型的多层通过目录结构和命名规范区分. 例如:在某个项目设计中需要区分数据层.逻辑层 ...

  10. uiimage 上传 数据库

    之前我所接触的上传图片都是直接与服务器交互的,即 app端要做的就是上传到服务器 现在这个项目却是app先上传到"数据库",由"数据库"传到服务端 下面说主题 ...