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

原创 2016年08月31日 11:08:29

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 10000007
LL 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

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1575Tr A(最基本的矩阵快速幂)

Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub...

40.Sqrt(x) (二分查找)

mplement int sqrt(int x). Compute and return the square root of x. 分析:

hdu5451(矩阵快速幂+广义斐波拉契)

题目链接:hdu5451 题目大意:已知,给你整数x,和一个素数M,求[y]%M。 题目分析: 设 (5+2√6)n=Xn+Yn*√6 Xn+Yn*√6 =(...

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

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

hdoj 5451 Best Solver 【求循环节 + 矩阵快速幂】

Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others) Tot...

矩阵快速幂 求斐波拉切数列的第n项 poj3070

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7241 ...

Hdu 5451 Best Solver(快速幂取模+循环节) -2015 ACM-ICPC沈阳网赛1002

Hdu 5451 Best Solver(快速幂取模+循环节) -2015 ACM-ICPC沈阳网赛1002

HDU1588___矩阵快速幂and斐波拉契通项的矩阵算法

整个算法来自http://blog.sina.com.cn/s/blog_626631420100vsug.html 他本人写的非常的清楚,感谢。 题目中的公式: f(0)=0 f(1)=1 ...
  • dr5459
  • dr5459
  • 2013年04月15日 15:26
  • 792

hdu 5451 Best Solver(矩阵乘法+数论)

题目链接:hdu 5451 Best Solver解题思路因为取模的数很小,所以暴力找打循环节,然后求解。代码#include #include #include #include #incl...

hdu4565So Easy!+矩阵快速幂+广义斐波拉契

Problem Description   A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu5451Best Solver=矩阵快速幂+广义斐波拉契
举报原因:
原因补充:

(最多只允许输入30个字)