bzoj4403-序列统计【Lucas,组合数学】
生活随笔
收集整理的這篇文章主要介紹了
bzoj4403-序列统计【Lucas,组合数学】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=4403
題目大意
求有多少個長度為nnn的單調不升序列,且對于每個元素都∈[L,R]\in[L,R]∈[L,R]
解題思路
我們讓m=R?L+1m=R-L+1m=R?L+1,因為序列的要求起始起始不會影響結果
然后我們開始考慮,我們在長度為nnn的序列全中1序列中我們可以每次選擇一個位置讓后面的數都加1,然后選擇的個數不能超過m?1m-1m?1個。
我們枚舉進行了多少次操作,對于操作的先后順序不會影響該結果,然后這時我們可以將模型轉換為nnn個格子,iii個球,然后求方案數
∑i=1m?1Cn+i?1i?Cn+mm?1\sum_{i=1}^{m-1} C_{n+i-1}^i\Rightarrow C_{n+m}^{m}-1i=1∑m?1?Cn+i?1i??Cn+mm??1
然后LucasLucasLucas定理搞搞就好了時間復雜度O(Tlog?n+p)O(T\log n+p)O(Tlogn+p)
codecodecode
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using namespace std; const ll p=1e6+3; ll T,n,m,a[p+1]; ll power(ll x,ll b,ll p) {ll ans=1;while(b){if(b&1) ans=ans*x%p;x=x*x%p;b>>=1;}return ans; } ll C(ll n,ll m,ll p) {if(m>n) return 0ll;return a[n]*power(a[m],p-2,p)%p*power(a[n-m],p-2,p)%p; } ll lucas(ll n,ll m,ll p) {if(!m) return 1ll;return lucas(n/p,m/p,p)*C(n%p,m%p,p)%p; } int main() {scanf("%lld",&T);a[0]=1;for(ll i=1;i<=p;i++)a[i]=a[i-1]*i%p;while(T--){int r,l;scanf("%lld%lld%lld",&n,&l,&r);m=r-l+1;printf("%lld\n",(lucas(n+m,m,p)-1+p)%p);}return 0; }總結
以上是生活随笔為你收集整理的bzoj4403-序列统计【Lucas,组合数学】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 五阴炽盛是什么意思 怎么理解五阴炽盛的意
- 下一篇: CF451E-Devu and Flow