HDU-1005 Number Sequence

简介: HDU-1005 Number Sequence

题目:

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

解析:

1. 本题是一个找规律的题,对于%7来说保证了f(n)最多有7钟,f(n+1)也是7种,所以f(n+2)最多49种。所以49之后就是规律。下图是我打表出来的图。第一张为1 1 {1~50},循环周期为16,第二张是1,2,{1~50},循环周期是6

2. 总结打表的规律是,当出现两个连续的1时,说明此时进入下一个循环。

#include
int main()
{
    int a, b, n;
    int s[101] = { 0 ,1,1};
    while (~scanf("%d%d%d", &a, &b, &n) && (a + b + n))
    {
        int m = 0,i;
            for (i = 3; i < 100; i++)
        {
            s[i] = (a*s[i - 1] + b*s[i - 2])%7;
            if (s[i] == 1 && s[i - 1] == 1)
                break;
        }
            m = i - 2;//周期
            n %= m;
            if(n!=0)
            printf("%d\n", s[n]);
            else
                printf("%d\n", s[i-2]);
    }
    return 0;
}


相关文章
|
存储 缓存 关系型数据库
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
133 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
100 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...
1021 0
HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 115124    Accepted Su...
801 0
|
人工智能 Java BI
KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11606    Accepted Submission...
816 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
99 1

热门文章

最新文章