ex_BSGS
Posted eterna-king
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ex_BSGS相关的知识,希望对你有一定的参考价值。
//author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #include<algorithm> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<utility> #include<cmath> #include<climits> #include<deque> #include<functional> #include<complex> #include<numeric> #include<unordered_map> #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)<(y)?(x):(y)) #define Pi acos(-1.0) #define ABS(x) ((x) >= 0 ? (x) : (-(x))) #define pb(x) push_back(x) #define lowbit(x) (x & -x) #define FRIN freopen("C:\Users\Administrator.MACHENI-KA32LTP\Desktop\in.txt", "r", stdin) #define FROUT freopen("C:\Users\Administrator.MACHENI-KA32LTP\Desktop\out.txt", "w", stdout) #define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define outd(x) printf("%d ", x) #define outld(x) printf("%lld ", x) #define il inline using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; const int maxn = 1e5; const int INF = 0x7fffffff; const int mod = 1e9 + 7; const double eps = 1e-7; inline int read_int() { char c; int ret = 0, sgn = 1; do { c = getchar(); } while ((c < ‘0‘ || c > ‘9‘) && c != ‘-‘); if (c == ‘-‘) sgn = -1; else ret = c - ‘0‘; while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) ret = ret * 10 + (c - ‘0‘); return sgn * ret; } inline ll read_ll() { char c; ll ret = 0, sgn = 1; do { c = getchar(); } while ((c < ‘0‘ || c > ‘9‘) && c != ‘-‘); if (c == ‘-‘) sgn = -1; else ret = c - ‘0‘; while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) ret = ret * 10 + (c - ‘0‘); return sgn * ret; } map <int, int> mp; il int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } il int Quick_pow(ll base, ll index, int p) { ll res = 1; while (index) { if (index & 1)res = res * base % p; base = base * base % p; index >>= 1; } return res % p; } il int EX_BSGS(int a, int b, int p) { if (b == 1) return 0; mp.clear(); int cnt = 0, t = 1, s, x, m; for (int d = gcd(a, p); d != 1; d = gcd(a, p)) { if (b % d) return -1; ++cnt, b /= d, p /= d, t = 1LL * t * a / d % p; if (b == t) return cnt; } s = b, m = sqrt(p) + 1; for (int i = 0; i < m; ++i) { mp[s] = i; s = 1LL * s * a % p; } x = Quick_pow(a, m, p), s = t; for (int i = 1; i <= m; ++i) { s = 1LL * s * x % p; if (mp.count(s)) return i * m - mp[s] + cnt; } return -1; } int a, b, p; int main() { while (scanf("%d %d %d", &a, &p, &b) && a + b + p) { a %= p, b %= p; int res = EX_BSGS(a, b, p); if (res == -1)puts("No Solution"); else outd(res); } return 0; }
以上是关于ex_BSGS的主要内容,如果未能解决你的问题,请参考以下文章