[Fortuna OJ] Aug 13th - Group A Problem-Solving Report
A - Count
So hard.
Simply put, the problem asks: given the condition \(\{x_i \mod m \in [1, m - 1]\}\), count the \(k\)-tuples that meet it. We can first take every \(x_i\) modulo \(m\), then let \(A = \sum_{i = 1}^k (x_i \mod m)\) (note: \(A\) is not taken modulo as a whole, it is just the sum of the individual remainders). We know that \(A \mod m = n \mod m\), and the upper bound of \(A\) is \(A = k(m - 1)\) (the upper bound of each term).
After this setup, the problem can be turned into: for each distinct \(A\), with \(A \mod m = n \mod m\) and \(A = \sum_{i = 1}^k (x_i \mod m), \forall i, x_i \in [1, m - 1]\) holding, how many ways there are to form the \(k\)-tuples. First, we can enumerate each \(A\) to narrow down the problem. Based on the conditions \(A\) satisfies, we can iterate over \(i\) like this to get every \(A\):
\[ A = im + (n \mod m), i \in [0, \lfloor \frac{k(m - 1) - (n \mod m)}{m} \rfloor ] \]
The complexity of getting the \(A\) values is really just on the order of \(O(k)\). Now, for each \(A\) we do the counting, working out how many such \(k\)-tuples there are. First, we need to know:
For an indeterminate equation \(\sum_{i = 1}^n x_i = B\), the number of positive integer solutions is \({B - 1 \choose n - 1}\) (by stars and bars).
This is important; the explanations below all use this result. In \(A\), elements greater than \(m\) may very well show up among the \(x_i\), so we can enumerate how many such elements greater than \(m\) there are and compute from that. Unfortunately, we can only count the ways with at least \(i\) elements greater than \(m\), and cannot count the answer for exactly zero, but luckily we can still use this to do inclusion–exclusion. So the formula for the answer is:
\[ \sum_{A = im + n \mod m} {k + \lfloor \frac{n - A}{m} \rfloor - 1 \choose k - 1} \sum_{j = 0}^{min(k, \lfloor \frac{A}{m - 1} \rfloor)} (-1)^j {A - j(m - 1) - 1 \choose k - 1} {k \choose j} \]
Notes:
- \({A - j(m - 1) - 1 \choose k - 1}\) means the number of solutions when solving the indeterminate equation again after at least \(j\) copies of \((m - 1)\) have already been subtracted.
- \({k \choose j}\) is the number of ways to choose the set of those elements greater than \(m\).
- \({k + \lfloor \frac{n - A}{m} \rfloor - 1 \choose k - 1}\) is the number of ways to pick some of the leftover \(m\)s and put them into those elements.
The solution to this problem isn't written in much detail, and nobody has written a solution for this problem either, so understanding this stuff is a real struggle. If you have any questions, please leave a comment, and I'll deal with it as soon as I can.
// A.cpp
// More comments on this problem at https://kalorona.com
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353, MAX_N = 1e7 + 10;
ll n, m, k, inv[MAX_N];
int fac[MAX_N], fac_inv[MAX_N];
ll comb(ll n_, ll k_)
{
if (n_ < k_)
return 0;
if (n_ < MAX_N && k_ < MAX_N)
return 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod;
ll ans = 1;
for (ll i = 1; i <= k_; i++)
ans = 1LL * ans * inv[i] % mod * ((n_ - k_ + i) % mod) % mod;
return ans;
}
int main()
{
freopen("count.in", "r", stdin);
freopen("count.out", "w", stdout);
scanf("%lld%lld%lld", &n, &m, &k);
inv[1] = 1;
for (ll i = 2; i < MAX_N; i++)
inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
fac[0] = fac_inv[0] = 1;
for (int i = 1; i < MAX_N; i++)
fac[i] = 1LL * fac[i - 1] * i % mod, fac_inv[i] = 1LL * fac_inv[i - 1] * inv[i] % mod;
ll ans = 0;
for (ll A = n % m; A <= 1LL * k * (m - 1); A += m)
{
ll opt = 1, res = 0;
for (ll i = 0; i <= k && 1LL * i * (m - 1) <= A; i++)
{
res = (res + opt * comb(A - i * (m - 1) - 1, k - 1) % mod * comb(k, i) % mod + mod) % mod;
opt = -opt;
}
ans = (ans + res * comb(k + (n - A) / m - 1, k - 1) % mod + mod) % mod;
}
printf("%lld", ans);
return 0;
}