[ACMcoder] Number Sequence

简介: Problem 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).I

Problem 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

解题思路

利用数组的循环规律解题,周期为i-2。

实现代码

#include <iostream>
using namespace std;

int main()
{
    int A, B, n;
    int f[53];
    while (cin>>A>>B>>n)
    {
        if (A == 0 && B == 0 && n == 0)
        {
            break;
        }
        f[1] = 1; f[2] = 1;
        int i, j, z;
        for (i = 3; i <= 52; i++)
        {
            f[i] = (A * f[i-1] + B * f[i-2]) % 7;
            if (f[i] == 1 && f[i-1] == 1)
            {
                break;
            }
        }
        i -= 2;
        n %= i;
        if (n == 0)
        {
            cout<<f[i]<<endl;
        }
        else
        {
            cout<<f[n]<<endl;
        }
    }
};
目录
相关文章
|
存储 缓存 关系型数据库
Redo日志 (4)—log sequence number(六十二)
Redo日志 (4)—log sequence number(六十二)
|
Java C++
HDU-1005,Number Sequence(有规律的数学题)
HDU-1005,Number Sequence(有规律的数学题)
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
134 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
101 0
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
953 1
|
Java Windows
HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 175657    Accepted Submission...
869 0
|
Java
HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27028    Accepted Submission...
1025 0