[題解]bzoj2142 禮物

Description

一年一度的聖誕節快要來到了。每年的聖誕節小E都會收到許多禮物,當然他也會送出許多禮物。不同的人物在小E
心目中的重要性不同,在小E心中分量越重的人,收到的禮物會越多。小E從商店中購買了n件禮物,打算送給m個人
,其中送給第i個人禮物數量爲wi。請你幫忙計算出送禮物的方案數(兩個方案被認爲是不同的,當且僅當存在某
個人在這兩種方案中收到的禮物不同)。由於方案數可能會很大,你只需要輸出模P後的結果。

Input

輸入的第一行包含一個正整數P,表示模;
第二行包含兩個整整數n和m,分別表示小E從商店購買的禮物數和接受禮物的人數;
以下m行每行僅包含一個正整數wi,表示小E要送給第i個人的禮物數量。

Output

若不存在可行方案,則輸出“Impossible”,否則輸出一個整數,表示模P後的方案數。

Sample Input

100
4 2
1
2

Sample Output

12
【樣例說明】
下面是對樣例1的說明。
以“/”分割,“/”前後分別表示送給第一個人和第二個人的禮物編號。12種方案詳情如下:
1/23 1/24 1/34
2/13 2/14 2/34
3/12 3/14 3/24
4/12 4/13 4/23
【數據規模和約定】
設P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi爲質數。
對於100%的數據,1≤n≤109,1≤m≤5,1≤pi^ci≤10^5。

Solution

%%%PoPoQQQ大爺

代碼:

#include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;

typedef long long LL;
const int maxn=100010;
int n,m,T,w[maxn],prime[maxn];
int p[maxn],a[maxn];
LL P,ans,tot;
bool ok[maxn];

void Make(int mx){
    for(int i=2;i<=mx;i++){
        if(!ok[i])prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&i*prime[j]<=mx;j++){
            ok[i*prime[j]]=true;
            if(!(i%prime[j]))break;
        }
    }
}
LL Mul(LL x,LL y,LL mod=LONG_LONG_MAX){
    LL res=0;
    for(;y;y>>=1,x=(x<<1)%mod)
        if(y&1)res=(res+x)%mod;
    return res;
}
LL Pow(LL x,LL y,LL mod=LONG_LONG_MAX){
    LL res=1;
    for(;y;y>>=1,x=x*x%mod)
        if(y&1)res=res*x%mod;
    return res;
}
LL Calc(LL x,int pri){
    LL temp=pri,ans=0;
    while(temp<=x)ans+=x/temp,temp*=pri;
    return ans;
}
LL Work(LL x,LL y,int pri){
    LL res=1,temp=1;
    for(LL i=x-x%y+1;i<=x;i++)
        if(i%pri)temp=Mul(temp,i,y);
    if(x<pri)return temp;
    for(LL i=1;i<y;i++)
        if(i%pri)res=Mul(res,i,y);
    return Mul(Mul(Pow(res,x/y,y),temp,y),Work(x/pri,y,pri),y);
}

int main(){
    scanf("%lld%d%d",&P,&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&w[i]),tot+=w[i];
    if(tot>n)return puts("Impossible"),0;
    w[++m]=n-tot;Make(maxn-10);
    LL lst=P;
    for(int i=1;i<=prime[0];i++){
        if(!(P%prime[i])){
            p[++T]=prime[i];
            while(!(P%prime[i]))
                a[T]++,P/=prime[i];
        }
    }
    P=lst;
    for(int t=1;t<=T;t++){
        int cnt=Calc(n,p[t]);
        for(int i=1;i<=m;i++)cnt-=Calc(w[i],p[t]);
        if(cnt>=a[t])continue;
        LL sum=Pow(p[t],a[t]);
        LL phi=sum-Pow(p[t],a[t]-1);
        LL temp=Work(n,sum,p[t]);
        for(int i=1;i<=m;i++){
            LL x=Work(w[i],sum,p[t]);
            x=Pow(x,phi-1,sum);
            temp=temp*x%sum;
        }
        temp=temp*Pow(p[t],cnt,sum)%sum;
        ans=(ans+Mul(Mul(temp,(P/sum),P),Pow(P/sum,phi-1,sum),P))%P;
    }
    printf("%lld\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章