zoukankan      html  css  js  c++  java
  • HDU

    Best Solver

    The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. 

    It is known that y=(5+26–√)1+2xy=(5+26)1+2x. 
    For a given integer x (0x<232)x (0≤x<232) and a given prime number M (M46337)M (M≤46337), print [y]%M[y]%M. ([y][y] means the integer part of yy) 

    InputAn integer T (1<T1000)T (1<T≤1000), indicating there are TT test cases. 
    Following are TT lines, each containing two integers xx and MM, as introduced above.
    OutputThe output contains exactly TT lines. 
    Each line contains an integer representing [y]%M[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



    循环节题目常见的有两种情况:
    1.MOD-1
    2.MOD^2-1
    通过推导或暴力可求出。

    本题循环节MOD^2-1。


    #include<bits/stdc++.h>
    #define MAX 3
    using namespace std;
    typedef long long ll;
    
    ll n,MOD;
    struct mat{
        ll a[MAX][MAX];
    };
    
    mat operator *(mat x,mat y)
    {
        mat ans;
        memset(ans.a,0,sizeof(ans.a));
        for(int i=1;i<=2;i++){
            for(int j=1;j<=2;j++){
                for(int k=1;k<=2;k++){
                    ans.a[i][j]+=(x.a[i][k]*y.a[k][j]+MOD)%MOD;
                    ans.a[i][j]%=MOD;
                }
            }
        }
        return ans;
    }
    mat qMod(mat a,ll n)
    {
        mat t;
        t.a[1][1]=10;t.a[1][2]=-1;
        t.a[2][1]=1;t.a[2][2]=0;
        while(n){
            if(n&1) a=t*a;
            n>>=1;
            t=t*t;
        }
        return a;
    }
    ll qsortMod(ll a,ll b)
    {
        ll ans=1;
        a%=((MOD+1)*(MOD-1));
        while(b){
            if(b&1) ans=ans*a%((MOD+1)*(MOD-1));
            b>>=1;
            a=a*a%((MOD+1)*(MOD-1));
        }
        return ans;
    }
    int main()
    {
        int tt=0,t,i;
        scanf("%d",&t);
        while(t--){
            scanf("%I64d%I64d",&n,&MOD);
            mat a;
            a.a[1][1]=10;a.a[1][2]=0;
            a.a[2][1]=2;a.a[2][2]=0;
            a=qMod(a,qsortMod(2,n));
            printf("Case #%d: %I64d
    ",++tt,(a.a[1][1]-1+MOD)%MOD);
            
        }
        return 0;
    }
  • 相关阅读:
    lambda续集——1
    c++之—— lambda表达式(有个未能解决的问题等待大佬解答)——(在stack overflow找到了答案)
    交换两个变量,只使用2个变量——权当面试了解使用
    移位实现正负数原码输出
    算法导论之——插入排序
    类模板的实现与定义相分离
    类模板
    当函数模板遇到普通函数
    c++之——template模板函数
    字符转数字,数字转字符
  • 原文地址:https://www.cnblogs.com/yzm10/p/9566725.html
Copyright © 2011-2022 走看看