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

题目链接: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 (0≤x<232) 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

 

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 long
struct 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;
}
/*
7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337
*/

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值