欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

bzoj4403-序列统计【Lucas,组合数学】

发布时间:2023/12/3 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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=RL+1,因为序列的要求起始起始不会影响结果
然后我们开始考虑,我们在长度为nnn的序列全中1序列中我们可以每次选择一个位置让后面的数都加1,然后选择的个数不能超过m−1m-1m1个。

我们枚举进行了多少次操作,对于操作的先后顺序不会影响该结果,然后这时我们可以将模型转换为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=1m1Cn+i1iCn+mm1

然后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,组合数学】的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。