zoukankan      html  css  js  c++  java
  • hdu 2685 I won't tell you this is about number theory

    #include<stdio.h>
    #include<string.h>
    #define LL __int64

    LL mult_mod(LL a,LL b,LL c)
    {
        a%=c;
        b%=c;
        LL ret=0;
        while(b)
        {
            if(b&1){ret+=a;ret%=c;}
            a<<=1;
            if(a>=c)a%=c;
            b>>=1;
        }
        return ret;
    }

    LL PowerMod(LL a, LL b, LL c)
    {
        
        LL ans = 1;
        LL k = a % c;
        while(b>0)    //(k*k % c)2^b %c
        {
            if(b&1)
                ans = mult_mod(ans,k,c);
            b = b>>1;
            k = mult_mod(k,k,c);
        }
        return ans;
    }

    LL gcd(LL a,LL b)
    {    
        if(a==0){return b;}
        return gcd(b%a,a);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        LL a,m,n,k,G;
        while(T--)
        {
            scanf("%I64d%I64d%I64d%I64d",&a,&m,&n,&k);
            if(m>n)
            {
                G = gcd(n,m);
            }
            else
            {
                G = gcd(m,n);
            }
            printf("%I64d ",(PowerMod(a,G,k)+k-1%k)%k);
        }
    }

    /*
        gcd(a^m - 1,a^n - 1) = a^gcd(m,n) - 1


    hint:
        计算减法的时候 为了保证结果是正数。
        a-b % k   ->  (a%k + k - b%k)%k

     
    */

  • 相关阅读:
    xml=>数组
    php的session锁
    压缩服务器中的文件夹,并下载到电脑
    通过onkeydown事件来控制只允许数字
    简单算法
    memcahe安装
    HTML div css 强制 换行 不换行
    windows charles response 乱码解决办法
    根据字节流判断内容是否使用UTF-8编码
    nginx安装过程
  • 原文地址:https://www.cnblogs.com/Milkor/p/4561363.html
Copyright © 2011-2022 走看看