You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
71 lines
1.6 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef unsigned long long ULL;
|
|
|
|
int phi(int x) {
|
|
int res = x;
|
|
for (int i = 2; i * i <= x; i++) {
|
|
if (x % i == 0) {
|
|
res = res - res / i;
|
|
while (x % i == 0) x /= i;
|
|
}
|
|
}
|
|
if (x > 1) res = res - res / x;
|
|
return res;
|
|
}
|
|
|
|
ULL qmi(ULL a, ULL b, ULL p) {
|
|
ULL res = 1;
|
|
a %= p;
|
|
while (b) {
|
|
if (b & 1) {
|
|
res = res * a % p;
|
|
b--;
|
|
}
|
|
b >>= 1;
|
|
a = a * a % p;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
ULL f[100005];
|
|
|
|
int main() {
|
|
int T;
|
|
cin >> T;
|
|
|
|
for (int t = 1; t <= T; t++) {
|
|
ULL b, p, m;
|
|
cin >> b >> p >> m;
|
|
if (b == 0 && p == 1 && m == 18446744073709551615ull) {
|
|
printf("Case #%d: 18446744073709551616\n", t);
|
|
continue;
|
|
}
|
|
int ph = phi(p);
|
|
ULL ans = 0;
|
|
if (b == 0) ans++;
|
|
f[0] = 1;
|
|
bool flag = 0;
|
|
int i;
|
|
for (i = 1; i <= m; i++) {
|
|
f[i] = f[i - 1] * i;
|
|
if (f[i] >= ph) {
|
|
f[i] %= ph;
|
|
flag = 1;
|
|
if (f[i] == 0) break;
|
|
}
|
|
if (flag) {
|
|
if (qmi(i, f[i] + ph, p) == b)
|
|
ans++;
|
|
} else {
|
|
if (qmi(i, f[i], p) == b)
|
|
ans++;
|
|
}
|
|
}
|
|
for (int k = 0; i <= m && k < p; i++, k++)
|
|
if (qmi(i, ph, p) == b)
|
|
ans += 1 + (m - i) / p;
|
|
printf("Case #%d: %I64u\n", t, ans);
|
|
}
|
|
return 0;
|
|
} |