zoukankan      html  css  js  c++  java
  • Codechef August Challenge 2018 : Modular GCD

    传送门

    一开始还手动拓欧找规律,发现好像玩不了。

    然后想了想,A-B这个数比较小,枚举它的因子判断合不合法就行了。

    需要特判A=B的情况。

    #include<cstdio>
    #include<algorithm>
    #define ll long long
    #define ld long double
    using namespace std;
    
    ll a,b,n,c,t,d,A,B;
    int i;
    const int MOD=1e9+7;
    inline void MM(ll &a,ll M){while(a>=M)a-=M;}
    inline ll cc(ll x,ll y,ll M){
        x=x*y-(ll)(((ld)x*y+0.01)/M)*M;
        return x<0?x+M:x;
    }
    inline ll mi(ll x,ll y,ll M){
        ll ans=1;
        x%=M;
        while(y){
            if (y&1) ans=cc(ans,x,M);
            y>>=1;x=cc(x,x,M);
        }
        return ans;
    }
    inline bool ju(ll x){
        return (mi(a,n,x)+mi(b,n,x))%x==0;
    }
    inline void work(){
        scanf("%lld%lld%lld",&a,&b,&n);
        c=a-b;
        if (c==0){
            printf("%d
    ",(mi(a,n,MOD)+mi(b,n,MOD))%MOD);
            return;
        }
        for (i=1;1LL*i*i<=c;i++)
        if (c%i==0&&ju(c/i)){
            printf("%lld
    ",c/i%MOD);
            return;
        }
        for (i--;i;i--)
        if (c%i==0&&ju(i)){
            printf("%lld
    ",i%MOD);
            return;
        }
    }
    int main(){
        scanf("%lld",&t);
        while(t--) work();
    }
    View Code
  • 相关阅读:
    读书笔记
    JavaScript
    Vue
    读书笔记
    Python
    Python
    概率论07 联合分布
    概率论06 连续分布
    概率论05 离散分布
    概率论04 随机变量
  • 原文地址:https://www.cnblogs.com/Enceladus/p/9493895.html
Copyright © 2011-2022 走看看