HDU

来源:互联网 发布:异或符号 java 编辑:程序博客网 时间:2024/06/08 01:49

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1705    Accepted Submission(s): 775


Problem Description
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right. 
 

Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
 

Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
 

Sample Input
23 1 24 1 10
 

Sample Output
85369
Hint
In the first case, the third number is 85 = 2*1十2十3^4. In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.
 

Source
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
 

Recommend
jiangzijing2015


题意:给出 F1 , F2 以及递推式 Fn  = Fn-1 + 2 * Fn-2 + n^4 求第n项,其中 n^4 到 (n+1)^4 的转移可以用二项式定理展开,展开得到的 n^3 , n^2 同理。
最终的中间矩阵应该是
1 1 0 0 0 0 0
2 0 0 0 0 0 0 
1 0 1 0 0 0 0
0 0 4 1 0 0 0
0 0 6 3 1 0 0
0 0 4 3 2 1 0
0 0 1 1 1 1 1

#include <bits/stdc++.h>using namespace std;const long long N = 7, Mod = 2147493647;int t;long long f1, f2, n;struct xx{    long long a[N][N];} ori,res;xx mul(xx x,xx y){    xx temp;    memset(temp.a, 0, sizeof(temp.a));    for(int i = 0; i < N; i++){        for(int j = 0; j < N; j++){            for(int k = 0; k < N; k++){                temp.a[i][j] += x.a[i][k]*y.a[k][j];                temp.a[i][j] %= Mod;            }        }    }    return temp;}void init(){    memset(res.a, 0, sizeof(res.a));    memset(ori.a, 0, sizeof(ori.a));    ori.a[0][0] = 1, ori.a[1][0] = 2, ori.a[2][0] = 1;    ori.a[0][1] = 1, ori.a[2][2] = 1, ori.a[3][2] = 4;    ori.a[4][2] = 6, ori.a[5][2] = 4, ori.a[6][2] = 1;    ori.a[3][3] = 1, ori.a[4][3] = 3, ori.a[5][3] = 3;    ori.a[6][3] = 1, ori.a[4][4] = 1, ori.a[5][4] = 2;    ori.a[6][4] = 1, ori.a[5][5] = 1, ori.a[6][5] = 1, ori.a[6][6] = 1;    res.a[0][0] = f2, res.a[0][1] = f1, res.a[0][2] = 81;    res.a[0][3] = 27, res.a[0][4] = 9, res.a[0][5] = 3, res.a[0][6] = 1;}long long calc(long long k){    while(k){        if(k & 1) res = mul(res, ori);        ori = mul(ori, ori);        k >>= 1;    }    return res.a[0][0];}int main(){    scanf("%d", &t);    while(t--){        scanf("%lld%lld%lld", &n, &f1, &f2);        if(n == 1){            printf("%lld\n", f1);            continue;        }        if(n == 2){            printf("%lld\n", f2);            continue;        }        init();        printf("%lld\n", calc(n-2));    }}


原创粉丝点击