CF2147G Modular Tetration
EuphoricStar · · 题解
首先观察一下一个
同时我们也可以得出一个必要条件是
此时我们就可以列出答案的式子了。枚举
注意这里
那么我们来看一个正确的推法。
枚举
考虑容斥,计算
引理:对于奇素数幂
证明:模
设
由
所以我们现在知道
所以答案的式子即为:
对
时间复杂度
// Problem: G. Modular Tetration
// Contest: Codeforces - Codeforces Global Round 29 (Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2147/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 1000100;
const int N = 1000000;
const ll mod = 998244353;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
int pr[maxn], tot, mpr[maxn];
bool vis[maxn];
inline void init() {
for (int i = 2; i <= N; ++i) {
if (!vis[i]) {
pr[++tot] = i;
mpr[i] = i;
}
for (int j = 1; j <= tot && i * pr[j] <= N; ++j) {
vis[i * pr[j]] = 1;
mpr[i * pr[j]] = pr[j];
if (i % pr[j] == 0) {
break;
}
}
}
}
void solve() {
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
ll ans = 1;
map<ll, ll> mp, pm;
auto work = [&](ll x, map<ll, ll> &mp) -> void {
while (x > 1) {
ll t = mpr[x];
++mp[t];
x /= t;
}
};
work(x, mp);
work(y, mp);
work(z, mp);
for (pii p : mp) {
pm[p.fst] += p.scd - 1;
work(p.fst - 1, pm);
}
for (pii p : pm) {
if (mp.find(p.fst) != mp.end()) {
continue;
}
ans = ans * (1 + (qpow(p.fst, p.scd) + mod - 1) * qpow(p.fst, mod - 2) % mod) % mod;
}
ans = ans * qpow(x * y * z % mod, mod - 2) % mod;
printf("%lld\n", ans);
}
int main() {
init();
int T = 1;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}