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 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )

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

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

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

hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契

Problem DescriptionInput The first line of input gives the number of cases, T. T test cases follow,...

poj 3070 矩阵快速幂求斐波拉契数列

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12324   Acc...
  • ctrss
  • ctrss
  • 2016-05-03 13:50
  • 134

矩阵快速幂 -codevs斐波拉契数列2

1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond ...

矩阵的特征值 => 矩阵的幂 => 广义斐波拉契数列的通项公式

众所周知,计算机里动辄涉及到斐波拉契数列,本文主要是运用线性代数的方法求出广义斐波拉契数列的通项公式。 广义斐波拉契数列的定义(自定义,可能还不够严谨,欢迎指教)如下: 且a,b满足a^2 +...

HDU 5451 Best Solver (2015年沈阳赛区网络赛B题)

1.题目描述:点击打开链接 2.解题思路:第一次做这类通过矩阵求解整数部分的题目,学习了。首先是如何求(5+2sqrt(6))^n的整数部分,这里可以参考如下的博客链接: 点击打开链接 接下来,本题...

HDU 5451 Best Solver

Problem Description The so-called best problem solver can easily solve this problem, with his/her c...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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