HDU 5451 Best Solver



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
  
  
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
矩阵乘法+循环节判断
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
int T,t=0,m,f[50000];
unsigned int n;

struct martix
{
    unsigned int a[2][2];
    martix(){memset(a,0,sizeof(a));}
    martix operator*(const martix &b)
    {
        martix c;
        for (int i=0;i<2;i++)
            for (int j=0;j<2;j++)
                for (int k=0;k<2;k++)
                    (c.a[i][k]+=a[i][j]*b.a[j][k])%=m;
        return c;
    }
};

bool operator!=(const martix &a,const martix &b)
{
    if (a.a[0][0]!=b.a[0][0]) return true;
    
    if (a.a[0][1]!=b.a[0][1]) return true;
    
    if (a.a[1][0]!=b.a[1][0]) return true;
    return false;
}

int get(unsigned int n,int m)
{
    int ans=1;
    for (int i=2;n;n>>=1)
    {
        if (n&1) (ans*=i)%=m;
        i=(i*i)%m;
    }
    return (ans+1)%m;
}

martix mget(martix a,int x)
{
    martix c=a;
    if (x<=0) return c;
    for (x-=1;x;x>>=1)
    {
        if (x&1) c=c*a;
        a=a*a;
    }
    return c;
}

int query()
{
    if (f[m]) return f[m];
        martix b,c;
        int loop=1;
        b.a[0][0]=10%m;
        b.a[0][1]=1%m;
        b.a[1][0]=(m-1)%m;
        c=b*b;
        while (b!=c) {c=c*b; loop++; }
        f[m]=loop;
        return loop;
}

int main()
{
    memset(f,0,sizeof(f));
    scanf("%d",&T);
    while (T--)
    {
        scanf("%u%d",&n,&m);
        martix a,b,c;
        int loop=1;
        a.a[0][0]=98%m;
        a.a[0][1]=10%m;
        b.a[0][0]=10%m;
        b.a[0][1]=1%m;
        b.a[1][0]=(m-1)%m;
        n=get(n,query());
        c=mget(b,n-1);
        a=a*c; 
        printf("Case #%d: %d\n",++t,(a.a[0][1]+m-1)%m);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值