您的位置:首页 > 其它

【BZOJ2142】【扩展lucas】礼物 题解

2017-11-01 21:33 411 查看
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。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cctype>
#include <algorithm>
#define digit (ch <  '0' || ch >  '9')
#define clr(x) memset(x, 0, sizeof x)
#define ms(a, x) memset(x, a, sizeof x)
#define LL long long
#define INF 2147483647
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

template <class T> inline void read(T &x) {
int flag = 1; x = 0;
register char ch = getchar();
while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }
while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
x *= flag;
}

int gcd(int a, int b) { return !b ? a : gcd(b, a%b); }

void ex_gcd(int a, int b, int &x, int &y) {
if(!b) { x = 1; y = 0; return ; }
ex_gcd(b, a%b, x, y);
int t = x; x = y; y = t-a/b*x;
}

int pow_mod(LL x, LL y, LL p) {
LL ret = 1;
while(y) {
if(y&1) ret = (ret*x)%p;
x = (x*x)%p; y >>= 1;
}
return ret;
}

int inv(int a, int p) {
if(!a) return 0;
int x = 0, y = 0;
ex_gcd(a, p, x, y);
x = (x%p+p)%p;
return !x ? p : x;
}

int Mul(int n,int pi,int pk){
if(!n)return 1;
long long ret=1;
for(register int i=2;i<=pk;i++)if(i%pi)ret=ret*i%pk;
ret=pow_mod(ret,n/pk,pk);
for(register int i=2;i<=n%pk;i++)if(i%pi)ret=ret*i%pk;
return ret*Mul(n/pi,pi,pk)%pk;
}

int C(int n, int m, int p, LL pi, LL pk) {
if(m > n) return 0;
LL a = Mul(n, pi, pk), b = Mul(m, pi, pk), c = Mul(n-m, pi, pk);
LL k = 0, ret;
for(int i = n; i; i /= pi) k += i/pi;
for(int i = m; i; i /= pi) k -= i/pi;
for(int i = n-m; i; i /= pi) k -= i/pi;
ret = a*inv(b, pk)%pk*inv(c, pk)%pk*pow_mod(pi, k, pk)%pk;
return (int)(ret*(p/pk)%p*inv(p/pk,pk)%p);
}

int p,n,m,d,w[30],M;
LL ans = 1,sum;

int main() {
scanf("%d%d%d",&p,&n,&m);
for(int i = 1; i <= m; i++) scanf("%d",&w[i]), sum += w[i];
if(sum > n) return puts("Impossible"), 0;
for(LL P, ret,i = 1; i <= m; i++){
n -= w[i-1], P = p, ret = 0;
for(int j = 2; j*j <= P; j++) {
if(!(P%j)) {
int px = 1;
while(!(P%j)) px *= j, P /= j;
ret = (ret+(C(n, w[i], p, j, px))%p)%p;
}
}
if(P > 1) ret = (ret+(C(n, w[i], p, P, P))%p)%p;
ans = ans*ret%p;
}
printf(AUTO,ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: