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.
For a fixed permutation, forget the choices for a second and count how many turns each person gets before the process stops. If person gets fewer than turns, their personal box physically cannot be emptied. Instant death.
Let . During the first turns, everyone gets either or turns. The people in the first positions get the extra turn.
So a permutation is fair iff every person with is impossible, and every person with must be placed inside the first positions. People with do not care.
The annoying part is that depends on , and can be huge. But , so is just a number; once you know and , the condition is only about counting forced people in the prefix.
If people have , then we need . Count permutations by choosing where those forced people go in the first slots, ordering them, and arranging everyone else: .
Let
For a fixed permutation , the process lasts exactly turns, because every turn hangs exactly one decoration. After turn , everything is gone, so we stop. No one has to take a ghost decoration afterward. That final-stop detail matters.
Count turns, not choices
Suppose person appears times among the first turns.
Person is the only person who can take from box , so all decorations from box must be taken during those turns. Therefore we need
Is that also sufficient? Yep.
If for every , then on exactly of person 's turns, take from box . On all remaining turns, take from box . The number of remaining turns is
= \sum_{i=1}^n c_i - \sum_{i=1}^n a_i = S - \sum_{i=1}^n a_i = a_0.$$ So the common box is used exactly $a_0$ times. Perfect. No extra hidden scheduling monster under the bed. Thus a permutation is fair iff $$a_i\le c_i \quad \text{for all } i.$$ **What are the $c_i$ values?** Write $$S=qn+r,\quad 0\le r<n.$$ During $S$ turns, every position in the permutation appears $q$ times. The first $r$ positions appear one extra time. So if person $i$ is in the first $r$ positions, then $$c_i=q+1,$$ otherwise $$c_i=q.$$ That means: - if $a_i>q+1$, impossible for every permutation; - if $a_i=q+1$, person $i$ **must** be placed in the first $r$ positions; - if $a_i\le q$, person $i$ can go anywhere. Tiny edge case: if $r=0$, there is no prefix with extra turns, so nobody can have $a_i=q+1$. The same rule handles it because then we need $m\le 0$. **Counting permutations** Let $$m = \#\{i\in[1,n] : a_i=q+1\}.$$ These $m$ forced people must all be inside the first $r$ positions. If $m>r$, answer is $0$. Otherwise: 1. choose and order the $m$ forced people inside the first $r$ positions; 2. fill every remaining position with the other $n-m$ people in any order. The count is $$\binom{r}{m}m!(n-m)!.$$ This simplifies to $$\frac{r!}{(r-m)!}(n-m)!.$$ Since $n\le 50$, we can precompute factorials and inverse factorials modulo $998244353$, or just compute the expression directly with factorials and modular inverses. Clean and boring, which is exactly what you want at 2 seconds. **Algorithm** For each test case: 1. compute $S$; 2. compute $q=\lfloor S/n\rfloor$ and $r=S\bmod n$; 3. scan $a_1,\dots,a_n$: - if any $a_i>q+1$, answer is $0$; - count how many equal $q+1$; 4. if that count is greater than $r$, answer is $0$; 5. otherwise output $$\binom{r}{m}m!(n-m)! \pmod{998244353}.$$ Complexity: $O(n)$ per test case after factorial precomputation. Memory: $O(n)$, basically nothing.#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const int MAXN = 55;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll modpow(ll a, ll e) {
ll res = 1;
while (e) {
if (e & 1) res = res * a % MOD;
a = a * a % MOD;
e >>= 1;
}
return res;
}
int main() {
setIO();
vector<ll> fact(MAXN), invfact(MAXN);
fact[0] = 1;
for (int i = 1; i < MAXN; i++) fact[i] = fact[i - 1] * i % MOD;
invfact[MAXN - 1] = modpow(fact[MAXN - 1], MOD - 2);
for (int i = MAXN - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1) % MOD;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> a(n + 1);
ll sum = 0;
for (int i = 0; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
ll q = sum / n;
int r = sum % n;
bool ok = true;
int forced = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > q + 1) ok = false;
if (a[i] == q + 1) forced++;
}
if (!ok || forced > r) {
cout << 0 << '\n';
continue;
}
ll ans = fact[r] * invfact[r - forced] % MOD;
ans = ans * fact[n - forced] % MOD;
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const int MAXN = 55;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll modpow(ll a, ll e) {
ll res = 1;
while (e) {
if (e & 1) res = res * a % MOD;
a = a * a % MOD;
e >>= 1;
}
return res;
}
int main() {
setIO();
vector<ll> fact(MAXN), invfact(MAXN);
fact[0] = 1;
for (int i = 1; i < MAXN; i++) fact[i] = fact[i - 1] * i % MOD;
invfact[MAXN - 1] = modpow(fact[MAXN - 1], MOD - 2);
for (int i = MAXN - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1) % MOD;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> a(n + 1);
ll sum = 0;
for (int i = 0; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
ll q = sum / n;
int r = sum % n;
bool ok = true;
int forced = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > q + 1) ok = false;
if (a[i] == q + 1) forced++;
}
if (!ok || forced > r) {
cout << 0 << '\n';
continue;
}
ll ans = fact[r] * invfact[r - forced] % MOD;
ans = ans * fact[n - forced] % MOD;
cout << ans << '\n';
}
}