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. 一些 JS页面的 调用方式init()

    //初始化.... var initAccManPage=function() { //初始化... var initChangeBtn = function(){ $("#btnChang ...

  2. 模板,BFS

    #include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...

  3. 剑指offer 29 多于一半的数

    1. 思路比较简单, 每次从数组中抽出两个数, 若是不同则丢弃两个数, 最后剩下的数即为所求 2. 书中给出的代码实现比较巧妙. 遍历数组中的元素, 变量 result 记录当前元素, time 记录 ...

  4. Android 网卡地址Mac Wifi文件

    1./system/etc/firmware/ti-connectivity/wl1271-nvs.bin的文件 2./data/etc/wifi/fw文件 3./data/nvram/APCFG/A ...

  5. 【BZOJ3585/3339】mex 莫队算法+分块

    [BZOJ3585]mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. ...

  6. 《从零开始学Swift》学习笔记(Day67)——Cocoa Touch设计模式及应用之MVC模式

    原创文章,欢迎转载.转载请注明:关东升的博客 MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之一,它最早出现在Smalltalk语言中.现在,很多计算 ...

  7. 动态svg效果

    import React from 'react'; import TweenOne from 'rc-tween-one'; import SvgDrawPlugin from 'rc-tween- ...

  8. Spring 之注解事务 @Transactional(转载)

    Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 事务传播行为类型 说明 P ...

  9. 170405、java版MD5工具类

    package com.rick.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  10. 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置

    之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...