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 ...
随机推荐
- HDU 5033 (单调栈维护凸包) Building
题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...
- POJ 2236 (简单并查集) Wireless Network
题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第 ...
- 使用C++读写Excel
1.导入Excel类型库 使用Visual C++的扩展指令#import导入Excel类型库: 1 2 3 4 5 6 7 8 9 10 11 12 #import "C:\\Progra ...
- WEBUS2.0 In Action - 索引操作指南(2)
上一篇:WEBUS2.0 In Action - 索引操作指南(1) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(1) 3. 添加.删除.撤销删除和修改文档 在WEBUS中要将 ...
- 如何快速delete数据
苦于qa账号,木有drop,truncate权限,同步数据要挨个delete表里边的数据,就写了个脚本,循环删除某个目标库的所有表里边的数据. 先在information_schema的库里边,通过T ...
- Linux diff patch
/***************************************************************************** * Linux diff patch * ...
- SQL Server 2008 序列号
SQL Server 2008 序列号:Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYBEnterprise: JD8Y6-HQG69-P9H84-XDTPG-34 ...
- 安装Oracle 11g RAC R2 之Linux DNS 配置
Oracle 11g RAC 集群中引入了SCAN(Single Client Access Name)的概念,也就是指集群的单客户端访问名称.SCAN 这个特性为客户端提供了单一的主机名,用于访问集 ...
- 使用JQuery Mobile实现手机新闻浏览器
jQuery Mobile项目是jQuery项目下的在移动开发方面的又一力作,在本文中,笔者将带你一步步更深入学习使用jQuery Mobile框架去实现一个能在android手机上运行的新闻浏览器, ...
- 嵌入式 hi3518x平台h264+g711a封装mp4代码demo
先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为: http://download.csdn.net/detail/skdkjxy/8071 ...