给出 a , b , p a,b,p a,b,p,其中 g c d ( a , p ) = 1 gcd(a,p)=1 gcd(a,p)=1,求 x x x满足
a x ≡ b ( m o d p ) a^x\equiv b(mod~p)\\ ax≡b(mod p)
设 x = A p − B x=A\sqrt p-B x=Ap−B其中 A ∈ [ 1 , p ] , B ∈ [ 0 , p ] A\in[1,\sqrt p],B\in[0,\sqrt p] A∈[1,p],B∈[0,p],得到问题的变形
a A p − B ≡ b ( m o d p ) a A p ≡ b a B ( m o d p ) a^{A\sqrt p-B}\equiv b(mod~p)\\ a^{A\sqrt p}\equiv ba^B(mod~p)\\ aAp−B≡b(mod p)aAp≡baB(mod p)
我们先枚举 B B B,算出每个 b a B m o d p ba^B~mod~p baB mod p,用 u n o r d e r e d _ m a p unordered\_map unordered_map存起来,再枚举 A A A,计算出 a A p a^{A\sqrt p} aAp,在 u n o r d e r e d _ m a p unordered\_map unordered_map中找相同的值,这样的 A , B A,B A,B就能恰好凑成一对答案。复杂度 O ( p ) O(\sqrt p) O(p),如果用 m a p map map的话,就多一个 l o g log log。
unordered_map<ll, ll> mp;
ll bsgs(ll a, ll b, ll p) {
if(a % p == 0) return -1;
mp.clear();
ll k = ceil(sqrt(p));
for(int i=0; i<=k; i++) {
mp[b] = i;
b = b * a % p;
}
ll aa = qpow(a, k, p), A = aa;
for(int i=1; i<=k; i++) {
if(mp[aa]) {
return 1ll * i * k - mp[aa] + 1;
}
aa = aa * A % p;
}
return -1;
}
[TJOI2007]可爱的质数/模板BSGS
#include
using namespace std;
typedef long long ll;
unordered_map<ll, ll> mp;
ll qpow(ll x, ll y, ll mod) {
ll ans = 1;
while(y) {
if(y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
ll bsgs(ll a, ll b, ll p) {
if(a % p == 0 && b != 0) return -1;
mp.clear();
ll k = ceil(sqrt(p));
for(int i=0; i<=k; i++) {
mp[b] = i;
b = b * a % p;
}
ll aa = qpow(a, k, p), A = aa;
for(int i=1; i<=k; i++) {
if(mp[A]) {
return 1ll*i*k-mp[A];
}
A = A * aa % p;
}
return -1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ll a, b, p;
cin >> p >> a >> b;
ll ans = bsgs(a, b, p);
if(ans == -1) cout << "no solution";
else cout << ans;
return 0;
}
P2485 [SDOI2011]计算器
快速幂+扩展欧几里得+bsgs。
#include
using namespace std;
#define int long long
const int inf = 0x7fffffff;
unordered_map<int, int> mp;
int qpow(int x, int y, int mod) {
int ans = 1;
while(y) {
if(y & 1) ans = 1ll * ans * x % mod;
x = 1ll * x * x % mod;
y >>= 1;
}
return ans;
}
int exgcd(int &x, int &y, int a, int b) {
if(b == 0) {
x = 1, y = 0;
return a;
}
int d = exgcd(x, y, b, a%b);
int tmp = y;
y = x-a/b*y;
x = tmp;
return d;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
for(int i=1; i<=n; i++) {
int y, z, p;
cin >> y >> z >> p;
if(k == 1) cout << qpow(y, z, p) << endl;
else if(k == 2) {
int u, v;
int d = exgcd(u, v, y, p);
if(z % d) puts("Orz, I cannot find x!");
else {
int l = p/d, r = z / d;
cout << (1ll * (r % l) * (u % l) % l + l) % l << endl;
}
}
else {
mp.clear();
if(y % p == 0 && z % p) {
puts("Orz, I cannot find x!");
continue;
}
int now = z%p, m = ceil(sqrt(p)), f = 0;
mp[now] = 0;
for(int i=1; i<m; i++) {
now = now * y % p;
mp[now] = i;
}
now = 1;
int A = qpow(y, m, p);
for(int i=1; i<=m; i++) {
now = now * A % p;
if(mp.count(now)) {
cout << ((i*m - mp[now]) % p + p) % p << endl;
f = 1;
break;
}
}
if(!f) puts("Orz, I cannot find x!");
}
}
return 0;
}