Description has only two Sentences

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

upd : HDU is 好了, and i wa 了, grass

#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;
}

posted @ 2022-10-11 20:36  La_pluma  阅读(27)  评论(0编辑  收藏  举报