【BZOJ-2142】禮物 拓展Lucas定理

2142: 禮物

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 1313  Solved: 541
[Submit][Status][Discuss]

Description

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

Input

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

Output

若不存在可行方案,則輸出「Impossible」,不然輸出一個整數,表示模P後的方案數。ide

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。

HINT

Source

Solution

扔下模板走人。spa

Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
#define MAXN 100010
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
	while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}

#define Pa pair<LL,LL>
#define MP make_pair
#define X first
#define C second

int N,M,P,w[MAXN];

inline LL Pow(LL x,LL y) {LL re=1; for (LL i=y; i; i>>=1,x=x*x) if (i&1) re=re*x; return re;}
inline LL Pow(LL x,LL y,LL p) {LL re=1; for (LL i=y; i; i>>=1,x=x*x%p) if (i&1) re=re*x%p; return re;}

inline void Exgcd(LL a,LL b,LL &x,LL &y) {if (!b) {x=1,y=0; return;} else Exgcd(b,a%b,y,x),y-=a/b*x;}

inline LL Inv(LL x,LL p) {LL a,b; Exgcd(x,p,a,b); return (a%p+p)%p;}

inline Pa Fac(LL x,LL p,LL c,LL pc)
{
	if (x==1 || !x) return MP(1,0);
	
	int las=x%pc; LL re2=1,re1;
	
	for (int i=1; i<=las; i++) {
		if (!(i%p)) continue;
		re2=re2*i%pc;
	}
	
	if (x>=pc) {
		re1=re2;
		for (int i=las+1; i<pc; i++) {
			if (!(i%p)) continue;
			re1=re1*i%pc;
		}
		re1=Pow(re1,x/pc,pc);
	} else re1=1;
	
	int t=x/p;
	Pa re=Fac(x/p,p,c,pc);
	return MP(re1*re2%pc*re.X%pc,t+re.C);
}

inline LL Lucas(LL n,LL m,LL p,LL c,LL pc)
{	
	Pa n1=Fac(n,p,c,pc),m1=Fac(m,p,c,pc),nm1=Fac(n-m,p,c,pc);
	
	int rc=n1.C-m1.C-nm1.C;
	
	LL re=1;
	re=n1.X*Inv(m1.X,pc)%pc*Inv(nm1.X,pc)%pc;
	
	for (int i=1; i<=rc; i++) re=(re*p)%pc;
	
	return re;
}

int p[MAXN],cnt,ex[MAXN],pex[MAXN];
inline void Divide(int x)
{
	int sx=x;
	for (int i=2; i*i<=sx; i++) {
		if (!(x%i)) {
			p[++cnt]=i;
			while (!(x%i)) ex[cnt]++,x/=i;
			pex[cnt]=Pow(i,ex[cnt]);
		}
	}
	if (x>1) p[++cnt]=x,pex[cnt]=x,ex[cnt]=1;
}

LL an[MAXN];
inline LL CRT(int n,int m)
{
	LL re=0;
	for (int i=1; i<=cnt; i++)
		an[i]=Lucas(n,m,p[i],ex[i],pex[i]);
	
	for (int i=1; i<=cnt; i++)
		(re+=P/pex[i]*Inv((P/pex[i])%pex[i],pex[i])%P*an[i]%P)%=P;
	
	return re;
}

int main()
{
	P=read(); Divide(P);
	
	N=read(),M=read();
	
	LL tot=0;
	for (int i=1; i<=M; i++) w[i]=read(),tot+=w[i];
	
	if (tot>N) return puts("Impossible"),0;
	
	LL ans=CRT(N,tot);
	for (int i=1; i<=M; i++) {
		ans=ans*CRT(tot,w[i])%P;
		tot-=w[i];
	}
	
	printf("%lld\n",ans);
	
	return 0;
}
相關文章
相關標籤/搜索