#include <iostream>
#include <fstream>
#include <cmath>
#include <unordered_map>
using namespace std;
int _base = 0;
int _congruence = 0;
int _mod = 0;
int _res = 0;
long long QuickPow (long long base, int index, int mod) {
long long res = 1;
while (index) {
if (index & 1) {
res = res * base % mod;
}
base = base * base % mod;
index >>= 1;
}
return res;
}
int Gcd (int numA, int numB) {
if (!numB) {
return numA;
}
return Gcd(numB, numA % numB);
}
int BSGS (long long base, long long congruence, int mod) {
if (mod == 1) {
return 0;
}
base %= mod, congruence %= mod;
if (congruence == 1) {
return 0;
}
int count = 0;
int gcd = 0;
long long findB = 1;
while (true) {
gcd = Gcd(base, mod);
if (gcd == 1) {
break;
}
if (congruence % gcd) {
return -1;
}
count++;
congruence /= gcd;
mod /= gcd;
findB = findB * (base / gcd) % mod;
if (findB == congruence) {
return count;
}
}
int num = ceil(sqrt(mod));
unordered_map<long long, int> dictMap;
dictMap[congruence] = 0;
for (int i = 1; i < num; i++) {
congruence = congruence * base % mod;
dictMap[congruence] = i;
}
long long power = QuickPow(base, num, mod);
long long findJ = findB;
for (int i = 1; i < num + 1; i++) {
findJ = findJ * power % mod;
if (dictMap.find(findJ) != dictMap.end()) {
return i * num - dictMap[findJ] + count;
}
}
return -1;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
while (cin >> _base >> _mod >> _congruence) {
if (!_base && !_mod && !_congruence) {
return 0;
}
_res = BSGS(_base, _congruence, _mod);
if (_res == -1) {
cout << "No Solution" << '\n';
continue;
}
cout << _res << '\n';
}
return 0;
}剪贴板
剪贴板 utiv1x10
剪贴板信息
剪贴板信息
- 收录
- 更新