版本比较
DIV3 7.14 斯特林数、卡特兰数 预习&做题记录
正文
增加 148 行删除 1 行
10 行未改动11121314151516171819310 行未改动全部展开33033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948010 行未改动但是有这个限制,所以减去不合法的方案数。因为越过了 $y=x$。

设 路径第一次交 $y=x+k+1$ 于 $P$,将 $O \to P$ 的路径沿 $y=x+1$ 翻折,到 $O' \to P$,其中 $O'(-1,1)$,翻折上去时向上变成向右,向右变成向上,所以就变成了起点为 $(-1,1)$,终点为 $(n,n)$ 的方案数,也就是 $C_{2 \times n}^{n-1}$。设路径第一次交 $y=x+k+1$ 于 $P$,将 $O \to P$ 的路径沿 $y=x+1$ 翻折,到 $O' \to P$,其中 $O'(-1,1)$,翻折上去时向上变成向右,向右变成向上,所以就变成了起点为 $(-1,1)$,终点为 $(n,n)$ 的方案数,也就是 $C_{2 \times n}^{n-1}$。

综上,$C_{2 \times n}^{n} - C_{2 \times n}^{n-1}$ 是合法方案数,$C_n=C_{2 \times n}^{n} - C_{2 \times n}^{n-1}$。310 行未改动全部展开
[lg](/problem/AT_agc021_e) [at](https://atcoder.jp/contests/agc021/tasks/agc021_e)
设施了 $R$ 次变红魔法,$B$ 次变蓝魔法。
显然有三种情况:
- $R<B$:显然至少一个石头是蓝的,无解;- $R \ge B + n$:显然最优方案可以全部有解,所以就是随便排,$C_{k}^{R}$ 种方案;
所以只剩一种情况:$B \le R \le B+n$。
贪心可以求出最多有 $R-B$ 个石头被施的变红魔法大于变蓝魔法(也就是只多施一次),其他红蓝相等的要最后施蓝的。
因为一个红蓝相等就是类似 `******RB` 的类型,`******` 占位,所以丢掉,变成 `RB`。
此时就是要求出有多少种组合可以找出 $n-(R-B)$ 个 `RB`,故:
> 所有前缀 `B` 比 `R` 最多多 $B-[n-(R-B)]=R-n$。
把 `B` 视为向上,`R` 视为向右,从 $(0,0)$ 走到 $(R,B)$ 不能越过 $y=x+(R-n)$。
这个是上一题用过的做法,答案是 $C_{k}^{R}-C_{k}^{2 \times R - n + 1}$,当然 $R \ge B + n$ 也可以用,因为此时 $C_{k}^{2 \times R - n + 1}=0$。
做完了吗?并没有。因为当 $R=B$ 的时候,最后一个不一定要 `RB`,只需要 `R`,所以 $k \gets k-1$,这时候才做完了。
:::success[code]```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef char ch;typedef string str;typedef double db;typedef __int128 i128;const ll inf=9e18,maxn=5e5+1,mod=998244353;const i128 Inf=1e35;ll n,k,fact[maxn],invfact[maxn],ans;ll qpow(ll a,ll b){ ll ans=1; while(b) { if(b%2) ans=ans*a%mod; a=a*a%mod,b/=2; } return ans;}ll inv(ll x) {return qpow(x,mod-2);}void init(){ fact[0]=invfact[0]=1; for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod; invfact[maxn-1]=inv(fact[maxn-1]); for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;}ll C(ll n,ll m){ if(n<m||m<0) return 0; return fact[n]*invfact[m]%mod*invfact[n-m]%mod;}int main(){ cin>>n>>k; if(k<n) return cout<<0,0; init(); for(int i=0;i<=k;i++) { if(2*i<k) continue; ans=(ans+C(k-(2*i==k),i)-C(k-(2*i==k),2*i-n+1)+mod)%mod; } cout<<ans;}```:::
# [P5014 水の三角(修改版)](/problem/P5014)
显然,如果没有斜边,就是 $C_{n+m}^{m}-C_{n+m}^{m-1}$,其中 $n,m$ 为行号减一,列号减一。
有了斜边也差不多,设走了 $x$ 条斜边,那么下和右就只剩 $N=n-x,M=m-x$ 条了,方案数 $C_{N+M}^{M}-C_{N+M}^{M-1}$,再乘上斜边的放置方案 $C_{N+M+i}^{i}$,答案就是:
$$\sum\limits_{i=0}^{m} (C_{N+M}^{M}-C_{N+M}^{M-1}) \times C_{N+M+i}^{i}\\N=n-i,M=m-i$$
:::success[code]```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef char ch;typedef string str;typedef double db;typedef __int128 i128;const ll inf=9e18,maxn=2e6+1,mod=998244353;const i128 Inf=1e35;ll t,a,fact[maxn],invfact[maxn];ll qpow(ll a,ll b){ ll ans=1; while(b) { if(b%2) ans=ans*a%mod; a=a*a%mod,b/=2; } return ans;}ll inv(ll x){ return qpow(x,mod-2);}void init(){ fact[0]=invfact[0]=1; for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod; invfact[maxn-1]=inv(fact[maxn-1]); for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;}ll C(ll n,ll m){ return fact[n]*invfact[m]%mod*invfact[n-m]%mod;}void solve(){ cin>>a; ll n=sqrt(a); while(n*(n+1)/2<a) n++; ll m=a-n*(n-1)/2,ans=0; n--,m--; for(int i=0;i<=m;i++) { ll N=n-i,M=m-i,A=C(N+M+i,i),B=(C(N+M,M)-C(N+M,M-1)+mod)%mod; ans=(ans+A*B%mod)%mod; } cout<<ans<<"\n";}int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>t; init(); while(t--) solve();}```:::