9329854 2013-10-13 14:36:41 Accepted 1005 171MS 5072K 654 B Java zhangyi

http://acm.hdu.edu.cn/showproblem.php?pid=1005

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

Total Submission(s): 86162    Accepted Submission(s): 20434

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

*/

import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(true){
int a=input.nextInt();
int b=input.nextInt();
long n=input.nextInt();
if(a==0&&b==0&&n==0)
break;
int f[]=new int[1000];
f[1]=f[2]=1;
int s[][]=new int[7][7];
s[1][1]=2;
int i=0;
for(i=3;i<60;i++){
f[i]=(a*f[i-1]+b*f[i-2])%7;
if(s[f[i-1]][f[i]]!=0){
break;
}
s[f[i-1]][f[i]]=i;
}
int d=i-s[f[i-1]][f[i]];
n-=s[f[i-1]][f[i]];
n%=d;
if(n==0)n+=d;
n+=s[f[i-1]][f[i]];
System.out.println(f[(int)n]);
}
}
}

Number Sequence_hdu_1005(规律)的更多相关文章

  1. LightOJ1245 Harmonic Number (II) —— 规律

    题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II)    PDF (English) Statistics ...

  2. 沈阳网络赛K-Supreme Number【规律】

    26.89% 1000ms 131072K A prime number (or a prime) is a natural number greater than 11 that cannot be ...

  3. HDU - 6198 number number number(规律+矩阵快速幂)

    题意:已知F0 = 0,F1 = 1,Fn = Fn - 1 + Fn - 2(n >= 2), 且若n=Fa1+Fa2+...+Fak where 0≤a1≤a2≤⋯≤a,n为正数,则n为mj ...

  4. [GodLove]Wine93 Tarining Round #3

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44857#overview 题目来源: ZOJ Monthly, July 2 ...

  5. 跳台阶(python)

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). # -*- coding:utf-8 -*- class Soluti ...

  6. is_NaN的使用

    原生js中使用判断某个值是否是数值,有且只有一个方法就是is_NaN. 原理:这个函数使用了Number() 去转换需要判断的值.Number() 去转换值,如果有任意非数值字符存在则就不是一个数值. ...

  7. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  8. uva 10706 Number Sequence(数学规律)

    题目连接:10706 - Number Sequence 题目大意:有一个有0 ~ 9组成的序列,1   1 2    1 2 3   1 2 3 4   1 2 3 4 5 ....就是第一位为1. ...

  9. ZOJ 3622 Magic Number 打表找规律

    A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

随机推荐

  1. set集合玩法、三目运算

    set是无序的,无法用下标获取值 创建set二种方式 1.第一种 s1=set()   #创建一个空的set,看下面就知道为什么要这么创建一个空的集合 2.第二种 s2={11,22,33,44} # ...

  2. 【BZOJ 2669】 2669: [cqoi2012]局部极小值 (状压DP+容斥原理)

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 667  Solved: 350 Description 有一 ...

  3. [BZOJ5093]图的价值(NTT+第二类Stirling数)

    5093: [Lydsy1711月赛]图的价值 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 250  Solved: 130[Submit][Sta ...

  4. BZOJ 2612 [Poi2003]Sums(最短路)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2612 [题目大意] 给定a数组,问num能否被表示为a[1]*x[1]+a[2]*x[ ...

  5. Unity UGUI之Image

    Image组件在Inspector Source Image--需要一个Sprite(精灵). Color--图片的颜色 Material--可以添加材质球 RayCast Target--选中可以传 ...

  6. LINUX 高可用群集之 ~Corosync~

    Corosync:它属于OpenAIS(开放式应用接口规范)中的一个项目corosync一版本中本身不具 备投票功能,到了corosync 2.0之后引入了votequorum子系统也具备了投票功能了 ...

  7. POJ 1755 Triathlon (半平面交)

    Triathlon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4733   Accepted: 1166 Descrip ...

  8. jquery-问题解答

    1.var v = $('.summer-input:input').val(); //依据class获取input 2.var v = $('input[name=user\\.name]').va ...

  9. hibernate将connection放进threadlocal里实现数据库连接池

    Why ThreadLocal? 无论如何,要编写一个多线程安全(Thread-safe)的程序是困难的,为了让线程共享资源,必须小心地对共享资源进行同步,同步带来一定的效能延迟,而另一方面,在处理同 ...

  10. VMware 11.0 简体中文版|附永久密钥

    20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...