hdu5451Best Solver=矩阵快速幂+广义斐波拉契

来源:互联网 发布:知乎温酒作品集 编辑:程序博客网 时间:2024/06/04 18:55

Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

It is known that y=(5+26)1+2x.
For a given integer x (0≤x<2^32) and a given prime number M (M≤46337), print [y]%M. ([y] means the integer part of y)

Input
An integer T (1< T≤1000), indicating there are T test cases.
Following are T lines, each containing two integers x and M, as introduced above.

Output
The output contains exactly T lines.
Each line contains an integer representing [y]%M.

Sample Input

7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337

Sample Output

Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947

Source
2015 ACM/ICPC Asia Regional Shenyang Online

参考:http://blog.csdn.net/xtulollipop/article/details/52382791
同样的做法:然后就可以去找矩阵的循环节,可以暴力扫,也可以用结论:
http://blog.csdn.net/xtulollipop/article/details/52373948
然后就简单了。。

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>using namespace std;#define pi acos(-1.0)#define EPS 1e-6    //log(x)#define e exp(1.0); //2.718281828//#define mod 1000000007#define INF 0x7fffffff#define inf 0x3f3f3f3f#pragma comment(linker,"/STACK:102400000,102400000")typedef long long LL;#define debug(x) cout<<x<<endl;#define debug2(x) cout<<x<<" ";//#define MOD 10000007LL MOD;struct Mat{    int n,m;    LL mat[9][9];};Mat operator *(Mat a,Mat b){    Mat c;    memset(c.mat,0,sizeof(c.mat));    c.n = a.n,c.m = b.m;    for(int i=1;i<=a.n;i++){        for(int j=1;j<=b.m;j++){            for(int k=1;k<=a.m;k++){                c.mat[i][j] += (a.mat[i][k]*b.mat[k][j])%MOD;                c.mat[i][j] %= MOD;            }        }    }    return c;}Mat operator +(Mat a,Mat b){    Mat c;    memset(c.mat,0,sizeof(c.mat));    c.n = a.n,c.m = a.m;    for(int i=1;i<=a.n;i++){        for(int j=1;j<=a.m;j++){            c.mat[i][j] = (a.mat[i][j]+b.mat[i][j])%MOD;        }    }    return c;}Mat operator ^(Mat a,LL k){    Mat c;    memset(c.mat,0,sizeof(c.mat));    c.n = a.n,c.m = a.n;    for(int i=1;i<=a.n;i++)c.mat[i][i] = 1;    while(k){        if(k&1){            c = c*a;        }        a = a*a;        k>>=1;    }    return c;}void out(Mat a){    for(int i=1;i<=a.n;i++){        for(int j=1;j<=a.m;j++){            printf(j==a.m? "%I64d\n":"%I64d ",a.mat[i][j]);        }    }}LL quickPow(LL x, LL n, LL mm){    LL a = 1;    while (n)    {        a *= n&1 ? x : 1;        a %= mm;        n >>= 1 ;        x *= x;        x %= mm;    }    return a;}int main(){    int T_T;    scanf("%d",&T_T);    LL x,m;    int cas=0;    while(T_T--){        scanf("%I64d %I64d",&x,&m);        printf("Case #%d: ",++cas);        MOD=m;        LL tempmod=m*m-1;        LL n=quickPow(2,x,tempmod)+1;        if(n==0){            printf("1\n");            continue;        }        else if(n==1){            printf("9\n");            continue;        }        Mat pp;        pp.n=pp.m=2;        pp.mat[1][1]=5%MOD;        pp.mat[1][2]=12%MOD;        pp.mat[2][1]=2;        pp.mat[2][2]=5%MOD;        Mat A0;        A0.n=2,A0.m=1;        A0.mat[1][1]=5%MOD;        A0.mat[2][1]=2;        Mat ans=pp^(n-1);        ans=ans*A0;        printf("%I64d\n",(2*ans.mat[1][1]-1+MOD)%MOD);    }    return 0;}/*                   _ooOoo_                  o8888888o                  88" . "88                  (| -_- |)                  O\  =  /O               ____/`---'\____             .'  \\|     |//  `.            /  \\|||  :  |||//  \           /  _||||| -:- |||||-  \           |   | \\\  -  /// |   |           | \_|  ''\---/''  |   |           \  .-\__  `-`  ___/-. /         ___`. .'  /--.--\  `. . __      ."" '<  `.___\_<|>_/___.'  >'"".     | | :  `- \`.;`\ _ /`;.`/ - ` : | |     \  \ `-.   \_ __\ /__ _/   .-` /  /======`-.____`-.___\_____/___.-`____.-'======                   `=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^         I have a dream!A AC deram!! orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz*/
0 0
原创粉丝点击