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 ...
随机推荐
- hdu 2686(状压dp)
题目链接:http://poj.org/problem?id=2686 思路:典型的状压dp题,dp[s][v]表示到达剩下的车票集合为S并且现在在城市v的状态所需要的最小的花费. #include& ...
- Ubantu apt source 国内
位置 /etc/apt/sources.list apt-get update deb http://mirrors.163.com/ubuntu/ precise main restricted u ...
- 用Python获取Linux资源信息的三种方法
方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...
- Makefile foreach(转)
原文出处:http://blog.csdn.net/cjsycyl/article/details/8064871 foreach 函数和别的函数非常的不一样.因为这个函数是用来做循环用的,Makef ...
- H5学习之--前端和PHP后端的简单交互
最近在学习前端的东西,H5+CSS3+JS,又分别学习了原生JS和jQuery库,还有Bootstrap框架,因为我是做ios开发的,所以先熟悉WebApp相关的开发知识,其他的学习资料,网上有很多的 ...
- ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)
一.问题描述 1.ArcMap闪退 2.安装32位cx_oracle提示python目录不存在 二.解决方案 1.修改pythoncore的文件目录,指向C:\Python27\ArcGIS10.3\ ...
- 《C++ Primer Plus》学习笔记0
Hello,World! 本书版本:<C++ Primer Plus(第6版)中文版>C++是在C语言基础上开发的一种集面向对象编程.泛型编程和过程化编程于一体的编程语言,是C语言的超集. ...
- 170424、Mysql权限控制 - 允许用户远程连接
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- windows下安装google protocbuf
首先安装setuptools: windows:======== 1.下载 ez_setup.py,安装setuptoolshttps://bitbucket.org/pypa/setuptools/ ...
- Struts2中获取Web元素request、session、application对象的四种方式
我们在学习web编程的时候,一般都是通过requet.session.application(servletcontext)进行一系列相关的操作,request.session.和applicatio ...