Number Sequence_hdu_1005(规律)
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(规律)的更多相关文章
- LightOJ1245 Harmonic Number (II) —— 规律
题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II) PDF (English) Statistics ...
- 沈阳网络赛K-Supreme Number【规律】
26.89% 1000ms 131072K A prime number (or a prime) is a natural number greater than 11 that cannot be ...
- 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 ...
- [GodLove]Wine93 Tarining Round #3
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44857#overview 题目来源: ZOJ Monthly, July 2 ...
- 跳台阶(python)
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). # -*- coding:utf-8 -*- class Soluti ...
- is_NaN的使用
原生js中使用判断某个值是否是数值,有且只有一个方法就是is_NaN. 原理:这个函数使用了Number() 去转换需要判断的值.Number() 去转换值,如果有任意非数值字符存在则就不是一个数值. ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- 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. ...
- ZOJ 3622 Magic Number 打表找规律
A - Magic Number Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Subm ...
随机推荐
- Vim的一些补充
查看当前路径 :pwd 切换当前路径到当前打开文件所在路径 :cd %:p:h 解释: %表示当前文件名,%:p表示包含文件名的全部路径,%:p:h表示文件所在路径(head of the full ...
- Lisp em SCU - 4490 (强大的map用法)
Time Limit: 1000 MS Memory Limit: 131072 K Description There are two lists and they may be intersect ...
- Python操作Mongo数据库
连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...
- 【51Nod 1363】最小公倍数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1363 \[ \begin{aligned} &\sum_{i=1 ...
- BZOJ4029 HEOI2015定价
贪心. 每次将最后一个非零位加一判断即可. 一开始想少了,只关心把最后一位变成5了,其实可以都变的. #include<bits/stdc++.h> using namespace std ...
- spark1.0.0 mllib机器学习库使用初探
本文机器学习库使用的部分代码来源于spark1.0.0官方文档. mllib是spark对机器学习算法和应用的实现库,包括分类.回归.聚类.协同过滤.降维等,本文的主要内容为如何使用scala语言创建 ...
- bzoj 1780
这是一道环上的问题,我们先将一个环展开,再复制一次. 这样,任何一个合法方案一定对应在转换后的序列的一些连续的区间,使得它们的并的长度大于等于圈长. 然后,我们将区间合并一下(就是将一些被其他区间包含 ...
- bzoj 1004 Cards 组合计数
这道题考察的是组合计数(用Burnside,当然也可以认为是Polya的变形,毕竟Polya是Burnside推导出来的). 这一类问题的本质是计算置换群(A,P)中不动点个数!(所谓不动点,是一个二 ...
- Android中的数据存储(一):SharedPreferences 2017-05-24 10:35 64人阅读 评论(1) 收藏
SharedPreferences 这是本人(菜鸟)学习android数据存储时接触的有关SharedPreferences的知识以及本人自己写的一个简单地demo,为初学者学习和使用SharedPr ...
- [翻译] CKShapeView 支持CAShapeLayer
CKShapeView 支持CAShapeLayer https://github.com/conradev/CKShapeView CKShapeView is a UIView subclass ...