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 解题报告及测试数据的更多相关文章

  1. hdu 1711 Number Sequence 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目意思:给出一条有n个数的序列a[1],a[2],......,a[n],和一条有m 个数的序 ...

  2. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  3. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  4. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  5. USACO Section2.1 Sorting a Three-Valued Sequence 解题报告

    sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  6. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  7. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  9. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. hdu 4003(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 思路:dp[i][j]表示以i为根选择j个机器人的最小花费,然后就是背包了:dp[u][i]=m ...

  2. Python Scrapy 自动爬虫注意细节(3)

    一.对指定页面爬取 yield Request(url, meta={'cookiejar': response.meta['cookiejar']}, callback=self.parse_url ...

  3. HDU1080(DP)

    我用的dp是n^3的, dp[i][j] 表示在s串的i个前和t串的j个前,s[i],t[j]为最末端的两个串得到的最大值. 状态转移方程为: 之前将s和t串最尾端添加'-' ;i<=n;i++ ...

  4. ORA-01102的解决办法

    启动数据库时报错了! SQL> startup mount ORACLE instance started. Total System Global Area  608174080 bytes ...

  5. angular中的动画效果

    用angular来形成动画效果的代码如下 <!DOCTYPE html> <html lang="en" ng-app="app"> & ...

  6. 浏览器加载不上css,样式走丢

    来自:http://www.cnblogs.com/crizygo/p/5466444.html 问题描述:使用eclipse修改样式文件,浏览器的页面一时显示一时不显示,最后直接没有加载最新的css ...

  7. 使用Ansible自动配置Nginx服务

    1.首先安装好Ansible环境,具体步骤请见Ansible安装 2.先创建hosts文件(为后面编写脚本安装JDK做铺垫) [root@localhost /]# vi hosts [jdktest ...

  8. 数据结构(java语言描述)

    概念性描述与<数据结构实例教程>大同小异,具体参考:http://www.cnblogs.com/bookwed/p/6763300.html. 概述 基本概念及术语 数据 信息的载体,是 ...

  9. 常见到的runtime exception

    ClassCastException    类转换异常 IllegalArgumentException   非法参数异常 IndexOutOfBoundsException   数组越界异常 Nul ...

  10. Random/Stochastic

    ---恢复内容开始--- ===================================================== A random variable's possible valu ...