日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

[NOIP模拟测试9]题(Problem) 题解 (组合数全家桶+dp)

發(fā)布時間:2023/11/27 生活经验 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [NOIP模拟测试9]题(Problem) 题解 (组合数全家桶+dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

達哥送分給我我都不要,感覺自己挺牛批。

$type=0:$

跟visit那題類似,枚舉橫向移動的步數直接推公式:

$ans=\sum C_n^i \times C_i^{\frac{i}{2}} \times C_{n-i}^{\frac{n-i}{2}},i\% 2=0$

$type=1:$

因為不能觸碰負半軸,所以可以把右移看成+1,左移看成-1,轉化為前綴和大于等于0的問題

于是直接Catalan數就好了。注意是第$\frac {n}{2}$項的Catalan。

$Catalan_n=C_{2n}^{n}-C_{2n}^{n-1}$

$type=2:$

觀察到數據范圍較小,考慮dp。

設$f[i]$為走i步回到原點的方案數,通過枚舉第一次回到原點的步數j進行轉移。

顯然j只能為偶數。

$f[i]=\sum f[i-j]*Catalan(\frac{j}{2}-1)$

$type=3:$

還是枚舉橫向走的步數,結合Catalan數求解。

$ans=\sum C_n^i*Catalan(\frac{i}{2})*Catalan(\frac{n-i}{2})$

?

?

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const int N=100005;
int n,op;
ll fac[N<<1],ans,dp[N<<1];
ll qpow(ll a,ll b)
{ll res=1;//a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;
}
ll ini()
{fac[0]=1;for(int i=1;i<=(N-5)<<1;i++)fac[i]=1LL*i*fac[i-1]%mod;
}
ll C(ll x,ll y)
{if(y>x)return 0;return fac[x]*qpow(fac[y],mod-2)%mod*qpow(fac[x-y],mod-2)%mod;
}
ll lucas(ll x,ll y)
{if(!y)return 1;return C(x%mod,y%mod)*lucas(x/mod,y/mod)%mod;
}
ll Catalan(ll x)
{return (lucas(x*2,x)-lucas(x*2,x-1)+mod)%mod;
}
void qj1()
{//cout<<2*n<<endl;//cout<<C(2*n,n)<<endl;ans=Catalan(1LL*n/2);cout<<ans<<endl;
}
void qj0()
{for(int i=0;i<=n;i++){if(i%2)continue;ans+=lucas(1LL*n,1LL*i)%mod*lucas(1LL*i,1LL*i/2)%mod*lucas(1LL*(n-i),1LL*(n-i)/2)%mod,ans%=mod;}cout<<ans<<endl;
}
void qj3()
{for(int i=0;i<=n;i++){if(i%2)continue;ans+=lucas(1LL*n,1LL*i)*Catalan(1LL*i/2)%mod*Catalan(1LL*(n-i)/2)%mod,ans%=mod;}cout<<ans<<endl;
}
void qj2()
{dp[0]=1;for(int i=2;i<=n;i+=2)for(int j=2;j<=i;j+=2)dp[i]+=dp[i-j]*4%mod*Catalan(1LL*j/2-1LL)%mod,dp[i]%=mod;cout<<dp[n]<<endl;
}
int main()
{scanf("%d%d",&n,&op);ini();if(op==1)qj1();else if(op==0)qj0();else if(op==3)qj3();else qj2();return 0;
}

?

轉載于:https://www.cnblogs.com/Rorschach-XR/p/11267549.html

總結

以上是生活随笔為你收集整理的[NOIP模拟测试9]题(Problem) 题解 (组合数全家桶+dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。