HDU5950(67/600)

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 i4i4. 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 < 231231 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.

矩陣快速冪水題

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct p
{
    ll q[7][7];
};
ll mo=2147493647;
p dd={{
        1,2,1,4,6,4,1,
        1,0,0,0,0,0,0,
        0,0,1,4,6,4,1,
        0,0,0,1,3,3,1,
        0,0,0,0,1,2,1,
        0,0,0,0,0,1,1,
        0,0,0,0,0,0,1
}};
p cheng(p q,p w)
{
    p fs;
    memset(fs.q,0,sizeof(fs.q));
    for(int a=0;a<7;a++)
    {
        for(int b=0;b<7;b++)
        {
            for(int c=0;c<7;c++)
            {
                fs.q[a][b]+=(q.q[a][c]*w.q[c][b]*1ll)%mo;
                fs.q[a][b]=(fs.q[a][b]+mo)%mo;
            }
        }
    }
    return fs;
}
p ksm(p ds,int zs)
{
    p fs={1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1};
    while(zs)
    {
        if(zs&1)fs=cheng(fs,ds);
        ds=cheng(ds,ds);
        zs>>=1;
    }
    return fs;
}
int main()
{
    int T;
    cin>>T;
//  p fs={1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1};
    while(T--)
    {
        ll n,a,b;
        cin>>n>>a>>b;
        p we=ksm(dd,n-2);
//      p we=cheng(dd,fs);
        ll dan=0;
/*      for(int a=0;a<7;a++)
        {
            for(int b=0;b<7;b++)
            {
                cout<<we.q[a][b]<<" ";
            }
            cout<<endl;
        }
        return 0;*/
        dan+=b*1ll*we.q[0][0]%mo;
        dan+=mo;
        dan%=mo;
        dan+=a*1ll*we.q[0][1]*1ll%mo;
        dan+=mo;
        dan%=mo;
        dan+=16*we.q[0][2]*1ll%mo;
        dan+=mo;
        dan%=mo;
        dan+=8*1ll*we.q[0][3]%mo;
        dan+=mo;
        dan%=mo;
        dan+=4*we.q[0][4]%mo*1ll;
        dan+=mo;
        dan%=mo;
        dan+=2*we.q[0][5]*1ll%mo;
        dan+=mo;
        dan%=mo;
        dan+=1ll*we.q[0][6]*1ll%mo*1ll;
        dan+=mo;
        dan%=mo;
        printf("%lld\n",dan);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章