HDU1005 Number Sequence (奇技淫巧模拟)

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). 

InputThe 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. 
OutputFor 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

这道题看到100,000,000,竟然去想O(n)的算法了....考虑到数组开100,000,000可能会炸,于是我滑稽的开了滚动数组优化,于是滑稽的TLE掉了.后来想一想发现a*f[i-1]%7就只有七种情况:0~6,b*f[i-2]%7也只有七种情况,这样f[i]最多有49种情况,因为f[i-1]和f[i-2]都只有7种情况且a和b都是定值.所以只用打个49个数的表然后%%%就行了.然而我并不知道这种奇技淫巧叫啥,反正很6就是了.
下面附上代码:
#include<cstdio>
#define mod 7
using namespace std;

long long f[60],a,b,n;
int main()
{
    while(scanf("%d%d%d",&a,&b,&n)!=EOF&&a&&b&&n)
    {
        f[1]=1;
        f[2]=1;
        long long t=2;
        while(t<=49)
        {
            t++;
            f[t]=(a*f[t-1]+b*f[t-2])%mod;
        }
        printf("%d
",f[n%49]);
    }
}








每日刷题身体棒棒!
原文地址:https://www.cnblogs.com/stxy-ferryman/p/7470587.html