HD1005Number Sequence
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83963 Accepted Submission(s): 19894
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
和许多同志一样,看到这道题之后,都会想到用递归去做,然后就会发现去溢出,然后再会去找这个sequence里面的规律,发现会是一个循环。这应该也是做题的一个过程吧,在讨论区里看到有大神说测试数据有问题(应该是很久以前的数据),便更加觉得这到题目有意思,虽然数学有点水,但谁叫自己是一个ACMer了,那就花点时间去会一会这倒题吧。
题解思路:
一个非负数数 % 7,结果是0 ~ 6。
假设你有两个数n和m,满足f(n)=f(m);f(n-1)=f(m-1),则按照题目所给公式,f(n)f(m)和前后的数会在一定范围内一一对应相等,所以会有一个循环周期存在,因为f(n)是依赖于f(n-1)和f(n-2),因此有7*7=49种情况,即循环周期是49; 以下是ac代码:
#include<iostream>
using namespace std;
int main(){
int a,b,c, f[],temp;
while(cin>>a>>b>>c && !(a== && b== && c==)){
f[]=,f[]=;
temp = c % ;
for(int i=; i<=;i++){
f[i] = (a * f[i-] +b * f[i-]) % ;
}
cout<<f[temp]<<endl;
}
return ;
}
HD1005Number Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Android开源库--SlidingMenu左右侧滑菜单
如果说我比别人看得更远些,那是因为我站在了巨人的肩上. github地址:https://github.com/jfeinstein10/SlidingMenu 设置: 1.下载之后以依赖项的 ...
- 最全的PHP常用函数大全
PHP的一些常用函数 quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠. quoted_printable_decode() 函数对经过 quoted-printable 编码后的字 ...
- HTML5_拖放
拖放(Drag 和 drop)是 HTML5 标准的组成部分.拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 支持的浏览器:Inter ...
- R语言的向量化编程思维
1.计算缺失值比例 perNA<- mean(is.na(Data1)) 2.按值替换 #which返回值是符合条件的下标 NAIDX<- which(Data2<=3 | Data ...
- Codeforces 500A - New Year Transportation【DFS】
题意:给出n个数,终点t 从第i点能够跳到i+a[i],问能否到达终点 #include<iostream> #include<cstdio> #include<cstr ...
- BZOJ 3391 Tree Cutting网络破坏
不想写. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> ...
- PS:WINRAR制作32位安装程序和64位安装程序选项
32位 64位
- Android 系统 reboot
/*********************************************************************** * Android 系统 reboot * 说明: * ...
- 【Java】MD5加密
package sdfg; import java.math.BigInteger; import java.security.MessageDigest; import java.security. ...
- Python 删除 数组
numpy删除一列 从0开始,第三个参数是第几个维度 可以多删几个