1. 程式人生 > >hdu 5950 2016ACM/ICPC瀋陽賽區現場賽C題【矩陣快速冪】

hdu 5950 2016ACM/ICPC瀋陽賽區現場賽C題【矩陣快速冪】

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
2
3 1 2
4 1 10

Sample Output
85
369
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.

題意:f(1)=a,f(2)=b,f(n)=2f(n2)+f(n1)+n4,a,b,n,f(n),結果對2147493647取模。

分析:看到遞推關係式和資料範圍,想到矩陣快速冪。前兩項好構造,n

4可能比較棘手。顯然,我們構造的矩陣乘以n4應該等於(n+1)4,把(n+1)4展開,展開式含n4,n3,n2,n,1,那麼初始矩陣就應該是f(n1)f(n2)n4n3n2n1
那麼我們構造的7*7的矩陣就是

b100000a00000010100000041000006310000432100011111,注意n從3開始,1和2特判。注意如果用的(n1)4構造可能出現負值,取模要先加mod再膜;膜用ll存,輸入資料也要ll。

程式碼:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<ctype.h>
#include<time.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#include<map>
#include<vector>
#include<sstream>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
void fre(){freopen("in.txt","r",stdin);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define zero(a) fabs(a)<=eps
#define equal(a,b) zero(a-b)
const ld pi=acos(-1);
double e=2.718281828;
const double eps=1e-6;
const ll MOD=2147493647;
const int N=7;
//ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll a,b;
struct Matrix{
    ll mat[N][N];
    Matrix operator*(const Matrix& m)const{
        Matrix tmp;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                tmp.mat[i][j]=0;
                for(int k=0;k<N;k++)
                {
                    tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
                    tmp.mat[i][j]%=MOD;
                }
            }
        }
        return tmp;
    }
};

ll Pow(Matrix &m,int k)
{
    if(k==2)
        return b;
    if(k==1)
        return a;
    k-=2;
    Matrix ans;
    memset(ans.mat,0,sizeof(ans.mat));
    for(int i=0;i<N;i++)
        ans.mat[i][i]=1;
    while(k)
    {
        if(k&1)
            ans=ans*m;
        k>>=1;
        m=m*m;
    }
    ll x=0;
    x=(x+b*ans.mat[0][0])%MOD;
    x=(x+a*ans.mat[0][1])%MOD;
    x=(x+81*ans.mat[0][2])%MOD;
    x=(x+27*ans.mat[0][3])%MOD;
    x=(x+9*ans.mat[0][4])%MOD;
    x=(x+3*ans.mat[0][5])%MOD;
    x=(x+ans.mat[0][6])%MOD;
    return (x+MOD)%MOD;
}

int main()
{
    //fre();
    int cas;
    ll n;
    Matrix m;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%I64d%I64d%I64d",&n,&a,&b);
        memset(m.mat,0,sizeof(m.mat));
        for(int i=0;i<N;i++)
            m.mat[i][i]=1;
        m.mat[0][1]=2;m.mat[0][2]=1;
        m.mat[1][0]=1;m.mat[1][1]=0;
        m.mat[2][3]=4;m.mat[2][4]=6;
        m.mat[2][5]=4;m.mat[2][6]=1;
        m.mat[3][4]=3;m.mat[3][5]=3;
        m.mat[3][6]=1;m.mat[5][6]=1;
        m.mat[4][5]=2;m.mat[4][6]=1;
        printf("%I64d\n",Pow(m,n));
    }
    return 0;
}