首页 > 其他分享 >Description has only two Sentences

Description has only two Sentences

时间:2022-10-11 20:46:06浏览次数:51  
标签:cnt return Description int ll ret only Sentences mod

因为 hdu is down 所以吧这个正确性存疑的 EX_BSGS 暂存在这里

#include <map>
#include <cmath>
#include <cstdio>
typedef long long ll;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
std::map<int, int> ash;
int x, y, a;
ll qpow(ll a, int b, int mod){
    ll ret = 1;
    while(b){
        if(b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

int EX_BSGS(int a, ll t, int b, int p){
    ash.clear();
    a %= p, b %= p, t %= p;
    int cnt = 0, d;
    while(d = gcd(a, p), d != 1){
        if(b % d) return -1;
        ++cnt, b /= d, p /= d, t = t * a / d % p;
        if(b == t) return cnt;
    }
    ll s = b;
    int m = sqrt(p);
    for(int i = 0; i < m; ++i){
        ash[s] = i;
        s = s * x % p;
    }
    a = qpow(a, m, p); s = t * a % p;
    for(int i = 1; i <= m; ++i){
        if(ash.find(s) != ash.end()) return m * i - ash[s] + cnt;
        s = s * a % p;
    }
    return -1;
}

int main(){
    while(scanf("%d%d%d", &x, &y, &a) != -1){
        int ret = EX_BSGS(x, y / (x - 1), y / (x - 1), a);
        if(ret == -1) puts("Impossible!");
        else printf("%d\n", ret);
    }

    return 0;
}

标签:cnt,return,Description,int,ll,ret,only,Sentences,mod
From: https://www.cnblogs.com/Silver-ash/p/16782492.html

相关文章