Bzoj 2142 gift combination mathematics + Number Theory

Source: Internet
Author: User

题目大意:给定n个物品,分给m个人,每个人拿到wi个礼物,问方案数mod P P不一定为质数

首先我们把剩下的礼物也分给一个人 答案明显不变 w[++m]=n-w1-w2-...-wm

然后就会很方便地得到公式:

ans=C(n,w1)*C(n-w1,w2)*C(n-w1-w2,w3)*...*C(n-w1-w2-...-w_(m-1),wm) mod P

       =n!/w1!/w2!/.../wm! mod P

然后p不是质数 我们把P分解 令P=∏pi^ai

我们分别处理,可以得到一次同余方程组ans%pi^ai=lefti,用中国剩余定理合并一下即可。

然后对于每个pi^ai,我们进行以下处理:

将分子和分母化为x*pi^y的形式

然后分母的x部分与pi互质,可以求逆元,分子分母的y部分直接相减即可

然后我们处理阶乘

以19为例,将19!化为x*pi^y的形式,其中pi=3,ai=2 则有

19!%9=(1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19) %9

           =(1*2*4*5*7*8*10*11*13*14*16*17*19)*3^6*(1*2*3*4*5*6) %9

式子的左半部分是不为3的倍数的数,存在长度为p^a的循环节 求出一个循环节 快速幂处理 然后处理剩余部分

右半部分是6!%9 递归处理即可

我这题解写的真是冷静。。。这题还真TM让人冷静不下来啊-0-

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef pair<ll,ll> abcd;ll P,n,m,a[20],ans;struct prime_factor{ll p,a,p_a,left;}prime[50];int tot;void Decomposition(ll x){ll i;for(i=2;i*i<=x;i++)if(x%i==0){prime[++tot].p=i;prime[tot].p_a=1;while(x%i==0)x/=i,prime[tot].a++,prime[tot].p_a*=i;}if(x!=1)prime[++tot].p=x,prime[tot].a=1,prime[tot].p_a=x;}ll Quick_Power(ll x,ll y,ll mo){ll re=1;while(y){if(y&1)re*=x,re%=mo;x*=x,x%=mo;y>>=1;}return re;}abcd Deal(ll x,int pos){ll i;abcd re=make_pair( 1ll , x/prime[pos].p );for(i=1;i<prime[pos].p_a;i++)if(i%prime[pos].p)re.first*=i,re.first%=prime[pos].p_a;re.first=Quick_Power(re.first,x/prime[pos].p_a,prime[pos].p_a);for(i=x-x%prime[pos].p_a+1;i<=x;i++)if(i%prime[pos].p)re.first*=i,re.first%=prime[pos].p_a;if(re.second){abcd temp=Deal(re.second,pos);re.first*=temp.first;re.first%=prime[pos].p_a;re.second+=temp.second;}return re;}abcd EXGCD(ll x,ll y){if(!y)return make_pair(1,0);abcd temp=EXGCD(y,x%y);return make_pair(temp.second,temp.first-x/y*temp.second);}ll Reverse(ll x,ll p){ll re=EXGCD(x,p).first;re=(re%p+p)%p;return re;}void Chinese_Remainder_Theorem(){int i;for(i=1;i<=tot;i++){abcd temp=EXGCD(P/prime[i].p_a,prime[i].p_a);ll x=temp.first;x=(x%P+P)%P;ans+=x*P/prime[i].p_a*prime[i].left;ans%=P;}}int main(){int i,j;ll sum=0;cin>>P>>n>>m;for(i=1;i<=m;i++)cin>>a[i],sum+=a[i];if(sum>n){puts("Impossible");return 0;}if(sum<n)a[++m]=n-sum;Decomposition(P);for(i=1;i<=tot;i++){abcd temp=Deal(n,i);for(j=1;j<=m;j++){abcd _temp=Deal(a[j],i);temp.second-=_temp.second;temp.first*=Reverse(_temp.first,prime[i].p_a);temp.first%=prime[i].p_a;}temp.first*=Quick_Power(prime[i].p,temp.second,prime[i].p_a);prime[i].left=temp.first%prime[i].p_a;}Chinese_Remainder_Theorem();cout<<(ans%P+P)%P<<endl;}


BZOJ 2142 礼物 组合数学+数论

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.