Problem Summary
You played games and won exactly of them. Your longest winning streak has length exactly . How many win/loss sequences match this?
Approach
At first glance this looks like a plain combinatorics problem, but it turns out to be tricky to count directly. Following Solemntee’s editorial, the trick is inclusion-exclusion. Let be the number of sequences whose longest winning streak is at least . The answer we want is .
To compute : first “reserve” the wins. Placing disjoint runs each of length at least can be encoded as choosing of the candidate slots between/around the losses — that’s . After committing wins to those runs, the remaining positions can hold the leftover wins and losses freely, contributing . By inclusion-exclusion, Handle the corner case separately (the answer is iff ), and you’re done.
Code
#include<bits/stdc++.h>using namespace std;long long mod=998244353;int MAX=100005;vector<long long>fac,inv,finv;void binom_init(){ fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - mod / i * inv[mod % i] % mod; finv[i] = finv[i - 1] * inv[i] % mod; }}long long binom(long long n, long long r){ if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % mod * finv[n - r] % mod;}int main(){ binom_init(); int n,m,k; scanf("%d%d%d",&n,&m,&k); if(k==0) { printf("%d\n",m==0); return 0; } long long ans1=0,ans2=0; for(long long i=1;i*k<=m;i++) { if(i&1)ans1=(ans1+binom(n-m+1,i)%mod*binom(n-i*k,n-m)%mod+mod)%mod; else ans1=(ans1-binom(n-m+1,i)%mod*binom(n-i*k,n-m)%mod+mod)%mod; } for(long long i=1;i*(k+1)<=m;i++) { if(i&1)ans2=(ans2+binom(n-m+1,i)%mod*binom(n-i*(k+1),n-m)%mod+mod)%mod; else ans2=(ans2-binom(n-m+1,i)%mod*binom(n-i*(k+1),n-m)%mod+mod)%mod; } printf("%lld\n",(ans1-ans2+mod)%mod); return 0;}