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次即可。

以下是代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#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即周期

    }

}

时间: 02-22

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 个数的序列b[1],b[2],......,b[m],求出b[1],b[2],...,b[m]在序列a中完全匹配时,在序列a中的位置,如果找不到输出-1. 这几天一直在学kmp,该题算是kmp的入门题吧.有个地方要稍稍注意,代码中,主串和模式串的比较初始值为-1,-1,否则如果从0开始,会默认第一个字符是相

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

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

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i

USACO Section1.5 Number Triangles 解题报告

numtri解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 有一个数字的金字塔,形状如下    7   3 8  8 1 0 2 7 4 4 4 5 2 6 5 要从顶端开始走,每次只能向

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

sort3解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N,而后给出N个数,每个数都是1~3中的一个.请问,要把这个数列升序排好,最少需要进行几次两两交换?[数据范围] 1<=N<

Ducci Sequence解题报告

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2 - a3

CF995E Number Clicker 解题报告

CF995E Number Clicker 题目描述 Allen is playing Number Clicker on his phone. He starts with an integer u u on the screen. Every second, he can press one of 3 buttons. Turn \(u \to u+1 \pmod{p}\). Turn \(u \to u+p-1 \pmod{p}\). Turn \(u \to u^{p-2} \pmod{

【LeetCode】Longest Consecutive Sequence 解题报告

[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra