HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )

来源:互联网 发布:日本充气娃娃淘宝网 编辑:程序博客网 时间:2024/05/29 00:31

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451


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 (0x<232) and a given prime number M (M46337), print [y]%M. ([y] means the integer part of y)
 

Input
An integer T (1<T1000), 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
70 463371 463373 463371 4633721 46337321 463374321 46337
 

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

Source
2015 ACM/ICPC Asia Regional Shenyang Online


PS:

这题和HDU 2256  HDU 4565 类似!

推出公式后再暴力寻找一下循环节!推到见HDU 2256

或者:http://m.blog.csdn.net/blog/u011481752/26291179


代码如下:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longstruct Matrix{    LL m[4][4];} I,A,B,T;LL mod;LL n;int ssize = 2;Matrix Mul(Matrix a,Matrix b){    int i, j, k;    Matrix c;    for(i = 1; i <= ssize; i++)    {        for(j = 1; j <= ssize; j++)        {            c.m[i][j]=0;            for(k = 1; k <= ssize; k++)            {                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);                c.m[i][j]%=mod;            }        }    }    return c;}Matrix quickpagow(LL n){    Matrix m = A, b = I;    while(n > 0)    {        if(n & 1)            b = Mul(b,m);        n = n >> 1;        m = Mul(m,m);    }    return b;}LL quickpow(LL m,LL n,LL k){    LL ans = 1;    while (n > 0)    {          if (n & 1)             ans = (ans*m)%k;          n = n >> 1 ;          m = (m*m)%k;    }    return ans;}const int maxm = 46337+10;int r[maxm], f[maxm];void init(){    if(!r[mod])    {        f[0] = 2;        f[1] = 10;        for(int i = 2; ; i++)        {            f[i] = (10LL*f[i-1]-f[i-2]+mod)%mod;            if(f[i] == f[1] && f[i-1] == f[0])            {                r[mod] = i-1;                break;            }        }    }    memset(I.m,0,sizeof(I.m));    memset(A.m,0,sizeof(A.m));    for(int i = 0; i <= ssize; i++)    {        //单位矩阵        I.m[i][i] = 1;    }}int main(){    int t;    int cas = 0;    scanf("%d",&t);    while(t--)    {        scanf("%I64d%I64d",&n,&mod);        init();        A.m[1][1] = 10%mod;        A.m[1][2] = -1;        A.m[2][1] = 1;        A.m[2][2] = 0;        T = quickpagow((quickpow(2,n,r[mod])+1)%r[mod]-1);        B.m[1][1] = 10%mod;        B.m[2][1] = 2%mod;        T = Mul(T,B);        printf("Case #%d: %d\n",++cas,(T.m[1][1]+mod-1)%mod);    }    return 0;}/*70 463371 463373 463371 4633721 46337321 463374321 46337*/



0 0
原创粉丝点击