洛谷P2485 [SDOI2011]计算器
咕咕咕·····
#include <map>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
using namespace std;
const int N = 1e8 + 5;
LL t, x, y, z, k, gg;
LL X1, Y1;
map <LL, LL> c;
inline int read() {
char ch = gc();
int ans = 0;
while (ch > '9' || ch < '0') ch = gc();
while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
return ans;
}
inline LL quick_pow(LL a, LL b, LL p) {
LL base = a, ans = 1;
while (b) {
if (b & 1) ans = (ans * base) % p;
base = (base * base) % p;
b >>= 1;
}
return ans;
}
inline void exgcd(int a, int b) {
if (!b) { gg = a; X1 = 1, Y1 = 0; return; }
exgcd(b, a % b);
int temp = X1;
X1 = Y1, Y1 = temp - a / b * Y1;
}
int main() {
t = read(), k = read();
if (k == 1) {
while(t--) {
int p;
y = read(), z = read(), p = read();
printf("%lld\n", quick_pow(y, z, p));
}
}
else if (k == 2) {
while (t--) {
int p;
y = read(), z = read(), p = read();
exgcd(y, p);
if (z % gg) { printf("Orz, I cannot find x!\n"); continue; }
X1 = X1 * (z / gg);
if (X1 > 0 && X1 % p) X1 = X1 - (X1 / p) * p;
else if (X1 < 0) {
if ((0 - X1) % p) X1 = X1 + ((0 - X1) / p + 1) * p;
else X1 = 0;
}
printf("%lld\n", X1);
}
}
else {
while (t--) {
int p;
y = read(), z = read(), p = read();
if (z && y % p == 0) { printf("Orz, I cannot find x!\n"); continue; }
x = ceil(sqrt(p));
c.clear();
LL cur = z % p, po = quick_pow(y, x, p);
c[cur] = 0;
rep(i, 1, x) cur = (cur * y) % p, c[cur] = i;
cur = 1;
int vis = 1;
rep(i, 1, x) {
cur = (cur * po) % p;
if (c[cur]) { X1 = (i * x - c[cur]) % p; printf("%lld\n", (X1 + p) % p); vis = 0; break; }
}
if (vis) printf("Orz, I cannot find x!\n");
}
}
return 0;
}