Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Do not try to compute tetration. First kill the non-units: if , every later keeps that common factor, so it can never become modulo .
For a unit , let . The condition is exactly . Since is itself a power of , this turns into a plain prime-divisibility question.
A unit works iff every prime divisor of also divides . Valuations are not the real issue: once the prime is present in , the tower eventually supplies enough exponent.
Fix a residue modulo . Its order is fixed. If shares a prime with , that residue contributes ; otherwise the density inside that residue is by CRT.
Now sum over the unit group by Sylow components. For each prime with , the -component is either identity with weight , or one of nonidentity elements with weight , giving factor .
Key characterization
The sequence is scary-looking, but the only thing that matters is the multiplicative order of modulo .
First, if , then every for also shares a common prime factor with . So can never be congruent to modulo . Non-units are dead immediately.
Now assume . Let
.
Then
iff .
So for large , we need
,
which is equivalent to
.
But is also a power of . A number eventually divides powers of exactly when every prime divisor of already divides . The exact exponents do not matter long-term, because the tower exponent gets huge. If a prime is present in , its valuation in eventually becomes large enough. If a prime is missing from , it never appears. Math is brutal like that.
So we have the core criterion:
A positive integer is -tetrative iff and every prime divisor of divides .
The case is included automatically.
Turning the criterion into a density
Fix a unit residue modulo , and let
.
Every integer has the same order modulo , namely .
We need to be divisible by every prime in .
If some prime also divides , then this is impossible: is a unit, so . This residue contributes .
Otherwise, all primes in are coprime to . By CRT, among integers with , the extra requirements for all cut the density by exactly .
So residue contributes
if , and contributes otherwise.
Therefore the full density is
,
where the sum is over unit residues , but residues whose order has a prime factor also dividing are skipped.
The group sum factorizes
Let be the unit group. Its size is .
For each prime , let be the Sylow -subgroup of . Since is finite abelian,
,
and
, where .
For an element , the prime divides iff the -component of is not the identity.
Now split primes into two types.
If , then is forbidden: if the -component is nonidentity, then , so we would need , impossible for a unit modulo . Therefore the -component must be identity. This gives factor .
If , then this prime is allowed. In :
So this prime contributes the factor
.
Multiplying independent Sylow choices gives the whole sum. Thus the answer is
.
That is the entire problem. The 3100 part is mostly noticing that the tower collapses into a Sylow-product counting problem, not doing any cursed tower simulation.
Computing it
We are given , with . So every prime factor of is at most , and we can factor everything with a smallest-prime-factor sieve.
From the factorization
,
we factor
.
So for each prime power in :
Finally compute the formula modulo :
All denominators are invertible modulo MOD here, since all relevant primes and input factors are less than MOD.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const int MAXV = 1000000;
vector<int> spf;
ll modPow(ll a, ll e) {
a %= MOD;
ll r = 1;
while (e > 0) {
if (e & 1) r = r * a % MOD;
a = a * a % MOD;
e >>= 1;
}
return r;
}
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
void buildSpf() {
spf.assign(MAXV + 1, 0);
vector<int> primes;
for (int i = 2; i <= MAXV; i++) {
if (!spf[i]) {
spf[i] = i;
primes.push_back(i);
}
for (int p : primes) {
ll v = 1LL * p * i;
if (v > MAXV || p > spf[i]) break;
spf[v] = p;
}
}
}
void addFactors(int n, map<int, ll>& f) {
while (n > 1) {
int p = spf[n];
int c = 0;
while (n % p == 0) {
n /= p;
c++;
}
f[p] += c;
}
}
int main() {
setIO();
buildSpf();
int T;
cin >> T;
while (T--) {
int x, y, z;
cin >> x >> y >> z;
map<int, ll> mf, phiF;
addFactors(x, mf);
addFactors(y, mf);
addFactors(z, mf);
for (auto [p, e] : mf) {
if (e > 1) phiF[p] += e - 1;
addFactors(p - 1, phiF);
}
ll mMod = 1LL * x * y % MOD * z % MOD;
ll ans = modPow(mMod, MOD - 2);
for (auto [q, e] : phiF) {
if (mf.count(q)) continue;
ll nonIdentity = (modPow(q, e) - 1 + MOD) % MOD;
ll term = (1 + nonIdentity * modPow(q, MOD - 2)) % MOD;
ans = ans * term % MOD;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const int MAXV = 1000000;
vector<int> spf;
ll modPow(ll a, ll e) {
a %= MOD;
ll r = 1;
while (e > 0) {
if (e & 1) r = r * a % MOD;
a = a * a % MOD;
e >>= 1;
}
return r;
}
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
void buildSpf() {
spf.assign(MAXV + 1, 0);
vector<int> primes;
for (int i = 2; i <= MAXV; i++) {
if (!spf[i]) {
spf[i] = i;
primes.push_back(i);
}
for (int p : primes) {
ll v = 1LL * p * i;
if (v > MAXV || p > spf[i]) break;
spf[v] = p;
}
}
}
void addFactors(int n, map<int, ll>& f) {
while (n > 1) {
int p = spf[n];
int c = 0;
while (n % p == 0) {
n /= p;
c++;
}
f[p] += c;
}
}
int main() {
setIO();
buildSpf();
int T;
cin >> T;
while (T--) {
int x, y, z;
cin >> x >> y >> z;
map<int, ll> mf, phiF;
addFactors(x, mf);
addFactors(y, mf);
addFactors(z, mf);
for (auto [p, e] : mf) {
if (e > 1) phiF[p] += e - 1;
addFactors(p - 1, phiF);
}
ll mMod = 1LL * x * y % MOD * z % MOD;
ll ans = modPow(mMod, MOD - 2);
for (auto [q, e] : phiF) {
if (mf.count(q)) continue;
ll nonIdentity = (modPow(q, e) - 1 + MOD) % MOD;
ll term = (1 + nonIdentity * modPow(q, MOD - 2)) % MOD;
ans = ans * term % MOD;
}
cout << ans << '\n';
}
}