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 ...
随机推荐
- R语言实战读书笔记(二)创建数据集
2.2.2 矩阵 matrix(vector,nrow,ncol,byrow,dimnames,char_vector_rownames,char_vector_colnames) 其中: byrow ...
- core--主线程
windows的线程是windows可以分配给cpu的最小单元,对window应用程序的管理,最小的管理单位就是线程.那什么是线程?线程其实什么都不是,只是一个概念,没有实体,又看不见.这个概念的定义 ...
- Struts2+Uploadify文件上传使用详解
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...
- sql的 group by 分组;linq的 group by 分组
先来看看 linq的,下面的一段linq 是 ,在 学生导入数据的时候,我们根据学生的手机号码和学生名称进行分组,如果有重复的,我们就筛选出来,用到了 linq的 group by,注意这里是new出 ...
- BZOJ 1984 月下“毛景树”
我觉得我要把BZOJ上的链剖写完了吧.... #include<iostream> #include<cstdio> #include<cstring> #incl ...
- Linux diff patch
/***************************************************************************** * Linux diff patch * ...
- dede 5.7进后台卡死解决办法
注释后台文件dede/templets/index_body.htm(大概在第18行) <script type="text/javascript"> function ...
- Ensemble Learning 之 Gradient Boosting 与 GBDT
之前一篇写了关于基于权重的 Boosting 方法 Adaboost,本文主要讲述 Boosting 的另一种形式 Gradient Boosting ,在 Adaboost 中样本权重随着分类正确与 ...
- yaf框架流程三
路由的原理请看http://yaf.laruence.com/manual/yaf.routes.html这个链接 要点:路由的顺序是堆栈模式的,及最后添加的路由规则最优先.由上两篇可知,定义的第一条 ...
- 修改Chrome默认搜索引擎为Google.com
在使用Chrome的时候,Google为增强本地化搜索,或将默认的Google搜索引擎转换为本地语言,如在中国会自动转到google.com.hk,日本会会自动转到google.co.jp,如果你是一 ...