Number Sequence HDU-1005

原创 2018年04月16日 16:15:40

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

#include<iostream>
using namespace std;
int main()
{
    int a, b, n;
    int f[50]; //因为 f[n]是%7后得出的结果,而%7后有7种结果0,
    //1,2,3,4,5,6;f(n-1)有7种可能,
    //f(n-2)有7种可能; (f[n-1],f[n-2])出现重复的组合的最大周期为7*7=49
    while (cin >> a >> b >> n && a && b && n)
    {
        if (n == 1 || n == 2)
        {
            cout << "1" << endl; continue;
        }   
        for (int i = 1; i < 3; i ++)
            f[i] = 1;
        for (int i = 3; i <= n % 49; i ++)
            f[i] = (a * f[i-1] + b * f[i-2]) % 7; 
        cout << f[n%49] << endl;
    }
    return 0;
}

HDU-1005-Number Sequence(Java版+考虑周期+恶心水题!)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • qq_16542775
  • qq_16542775
  • 2015-05-04 15:23:02
  • 479

hdu-1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • oYinGui1
  • oYinGui1
  • 2016-03-18 13:51:48
  • 262

HDU-1005 Number Sequence

Number SequenceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To...
  • GUOQU_WEILAI
  • GUOQU_WEILAI
  • 2018-03-23 11:33:11
  • 3

ACM:HDU-1005 Number Sequence

需要注意的是如果直接用递归然后存储在大数中是可以写出来的,但是直接递归很效率很低所以会存在时间不够的问题。 然后如果不用大数存储就会因为递归次数太多而造成溢出。 观察一下,对7取模,所以对于n1...
  • qq_33545916
  • qq_33545916
  • 2016-04-05 00:12:10
  • 254

HDU-1005 Number Sequence

分析:由于n是1e8直接暴力是不可能了,观察到取模数是7,所以f[i]取模无非在0~7之间,而A和B是不变的,f[i]由f[i - 1]和f[i - 2]转移过来,最多也就有49种可能,大于它的时候找...
  • zhlbjtu2016
  • zhlbjtu2016
  • 2017-10-19 15:24:00
  • 42

HDU-1005 Number Sequence 循环结

一开始的思路是遇到11就开始循环,后来发现,发现当A=5555,B=666666时数列是1142142142142,这种情况不是以11开始循环的,依然超时就罢了,关按11判断的话就错了。因为两个结果都...
  • y201619819
  • y201619819
  • 2017-07-04 09:19:19
  • 37

Number Sequence(存在循环规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe...
  • qaz135135135
  • qaz135135135
  • 2016-07-22 19:47:39
  • 1471

Rope Sequence Number ST/HT 圈码字体

  • 2015年09月11日 20:55
  • 28KB
  • 下载

ORA-02287: sequence number not allowed here

http://devbbs.doit.com.cn/thread-11269-1-1.html http://zsjg13.iteye.com/blog/737677   当插入值需要从另外一张...
  • caolaosanahnu
  • caolaosanahnu
  • 2012-02-28 18:01:29
  • 1550

poj-1019-Number Sequence【思维】【规律】

题目链接:点击打开链接 Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Sub...
  • wang907553141
  • wang907553141
  • 2016-11-08 22:12:09
  • 301
收藏助手
不良信息举报
您举报文章:Number Sequence HDU-1005
举报原因:
原因补充:

(最多只允许输入30个字)