Winter-1-F Number Sequence 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB
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
题解:
因为n过大,f(n-1)和f(n-2)只有0,1,2,3,4,5,6七种情况,又A和B不变,f(n)由f(n-1)和f(n-2)决定,所以至多计算49次后,必将发生循环,所以计算50次即可。
以下是代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <string>
int c[1000];
using namespace std;
int main(){
int a,b,n;
int t1,t2,t;
c[1]=c[2]=1;
while(scanf("%d%d%d",&a,&b,&n)!=EOF && (a || b || n)){
t1 =t2=1;
int tag=0,i;
for(i=3;i<=100;i++){
t = t2;
t2 = (a*t + b*t1)%7;
t1 = t ;
c[i]=t2;
if(t1==1 && t2==1)break;//发生循环,就终止。
}
c[0]=c[i-2];//将循环的最后一个值赋值给c[0],避免取余数后判断
printf("%d\n",c[n%(i-2)]);//i-2即周期
}
}
Winter-1-F Number Sequence 解题报告及测试数据的更多相关文章
- hdu 1711 Number Sequence 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目意思:给出一条有n个数的序列a[1],a[2],......,a[n],和一条有m 个数的序 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
Number Sequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Pro ...
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
- USACO Section2.1 Sorting a Three-Valued Sequence 解题报告
sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Ducci Sequence解题报告
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- v8随心记
因为node原因,研究v8已经有段时间了,但是一直也没有抽空写点什么,现在想想有好多当时清晰的东西又模糊了.哎,伤心的很啊.但是一时又想不起什么章法,所以只能随手胡乱写了. 下载.编译: https: ...
- PowerShell----Automatic_Variables(预定义变量)
以下这些变量是由powershell创建和维护的.ls Variable: 可以获取到所有默认的变量, 每个版本的Powershell可能有差异 $$包含会话所收到的最后一行中的最后一个令牌. $? ...
- lumen 常用辅助函数
optional 函数接收任意参数并允许你访问对象上的属性或调用其方法.如果给定的对象为空,属性或方法调用返回 null return optional($user->address)-> ...
- Java中关于枚举的7种用法
1.定义常量: public enum Color { RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE } 2.用于switch: enum Color { RE ...
- GitHub Pages站点官方宣布开始使用HTTPS
导读 数百万人依靠GitHub Pages,将其作为他们的网站主机,除此之外,还有数百万人每天访问这些网站.为了更好地保护到GitHub Pages站点的通讯,也为了鼓励在因特网上更广泛地采用HTTP ...
- PHP 开发环境的搭建和使用03-- 安装mySql
1/ 安装的MySQL版本是5.6.10版本的,直接点击Install 2/ 选择 Execute 3/ 更新最新版本成功后,选择 "next" 4/ 自定义安装方式,选择C ...
- Opengl编程指南第二章:状态管理、几何绘图
//http://blog.csdn.net/longhuihu/article/details/7701874 1.绘图基础 清除窗口 glClearColor(0.0, 0.0, 0.0, 0.0 ...
- R语言中的Apriori关联规则的使用
1.下载Matrix和arules包 install.packages(c("Matrix","arules")) 2.载入引入Matrix和arules包 # ...
- Cookies Client Identification
HTTP The Definitive Guide Cookies are the best current way to identify users and allow persistent se ...
- WSGI的理解 perfect
https://blog.csdn.net/hzrandd/article/details/10099871 https://blog.csdn.net/cloudxli/article/detail ...