CF1420D Rescue Nibel!(组合数+差分)
题意:
给出n盏灯的开始时间和结束时间,询问同时有K盏灯亮的方案数。
题解:
#include<bits/stdc++.h> using namespace std; const int maxn=3e5+100; const int mod=998244353; typedef long long ll; ll qpow (ll x,ll y) { x%=mod; ll ans=1; while (y) { if (y&1) ans=ans*x%mod; y>>=1; x=x*x%mod; } return ans; } ll fac[maxn],inv_fac[maxn]; void init () { fac[0]=1; for (int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod; inv_fac[maxn-1]=qpow(fac[maxn-1],mod-2); for (int i=maxn-2;i>=0;i--) inv_fac[i]=inv_fac[i+1]*(i+1)%mod; } ll cal (ll n,ll m) { if (m<0||m>n) return 0; return fac[n]*inv_fac[m]%mod*inv_fac[n-m]%mod; } void solve () { int n,k; scanf("%d%d",&n,&k); vector<pair<int,int> > v; for (int i=0;i<n;i++) { int l,r; scanf("%d%d",&l,&r); v.push_back(make_pair(l,-1)); v.push_back(make_pair(r,1)); } sort(v.begin(),v.end()); int u=0;ll ans=0; for (int i=0;i<v.size();i++) { u-=v[i].second; if (v[i].second==-1) ans+=cal(u-1,k-1),ans%=mod; } printf("%lld\n",ans); } int main () {init();solve();}