hdu 3037 Saving Beans Lucas定理

来源:互联网 发布:外汇行情软件哪个好 编辑:程序博客网 时间:2024/05/18 20:51

题意:将不超过m颗豆子存储在n颗不同的树上,求种类数结果模p,输入的p一定是质数。(1<=n,m<=1e9)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037

用隔板法求种类数:http://blog.sina.com.cn/s/blog_7cc6f2770100red3.html

题解:http://m.blog.csdn.net/discreeter/article/details/70244319

费马小定理求逆元:http://blog.csdn.net/chen_minghui/article/details/72801934

#include<cstdio> // Lucas:求C(n+m,m) 对p取模 #include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,ox3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;const int INF = ( 2e9 ) + 2;const int maxn = 100010;ll fact[maxn];ll n,m,p;ll qPow(ll a,ll n,int p){ll ret=1;a%=p;while(n){if(n&1)ret=(a*ret)%p;a=(a*a)%p;n>>=1;}return ret;}ll C(ll n,ll m,int p)/**/{if(m>n)return 0;  // m>=n return 1return fact[n]*qPow(fact[n-m]*fact[m]%p/**/,p-2,p); }int Lucas(ll n,ll m,int p){if(m==0)return 1;else return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p/* */;}int main(){int t;fact[0]=1;scanf("%d",&t);while(t--){scanf("%lld%lld%lld",&n,&m,&p);for(int i=1;i<=p;i++)fact[i]=1LL*fact[i-1]*i%p;printf("%d\n",Lucas(n+m,m,p));}}

原创粉丝点击