【Browsing】BZOJ 2142 Gift

Description

The annual Christmas is coming soon. Every Christmas, little E receives many gifts, and of course he also gives many gifts. Different characters in small E

The importance in his mind is different. The heavier the weight in Little E's heart, the more gifts he will receive. Xiao E bought n gifts from the store and planned to give them to m people

, where the number of gifts given to the i-th person is wi. Please help to count the number of options for giving gifts (two options are considered different if and only if there is a

Individuals receive different gifts in the two programs). Since the number of solutions may be large, you only need to output the modulo P result.

Input

The first line of input contains a positive integer P, which means modulo;

The second line contains two integers n and m, which represent the number of gifts that Xiao E bought from the store and the number of people who received gifts;

Each of the following m lines contains only a positive integer wi, which represents the number of gifts that small E wants to give to the i-th person.

Output

If there is no feasible solution, output "Impossible", otherwise output an integer representing the number of solutions modulo P.

Sample Input

100
4 2
1
2

Sample Output

12
[Example description]
The following is the description of Example 1.
Separated by "/", before and after "/" respectively indicate the gift number for the first person and the second person. The details of the 12 schemes are as follows:
1/23 1/24 1/34
2/13 2/14 2/34
3/12 3/14 3/24
4/12 4/13 4/23
[Data scale and convention]
Let P =p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct, pi is a prime number.
For 100% data, 1≤n≤109, 1≤m≤5, 1≤pi^ci≤10^5.

Solution

Deduce the formula for finding the answer:
\(ans=C_{n}^{w_1}C_{n-w_1}^{w_2}C_{n-w_1-w_2}^{w_3}...\)
See \(n \) , 1e9, that is to extend Lucas
Then? It seems to be an extension of Lucas' template problem. . Merging with CRT in the middle
will not extend Lucas's look here

(PS: If the formula is disassembled, it becomes like this \(ans=\frac{n!}{w1!w2!w3!.....(n-\sum_{i=1}^nw_i)!}\ mod p)\) , can it be faster?Although the complexity is the same

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXM=10;
int p,w[MAXM];
ll ans=1;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline ll qexp(ll a,ll b,ll n)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%n;
        a=a*a%n;
        b>>=1;
    }
    return res;
}
inline ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    ll r=exgcd(b,a%b,x,y);
    ll t=x;
    x=y;
    y=t-(a/b)*y;
    return r;
}
inline ll fac(ll n,ll pi,ll pk)
{
    if(!n)return 1;
    ll res=1;
    for(register ll i=2;i<=pk;++i)
        if(i%pi)(res*=i)%=pk;
    res=qexp(res,n/pk,pk);
    for(register ll i=2;i<=n%pk;++i)
        if(i%pi)(res*=i)%=pk;
    return res*fac(n/pi,pi,pk)%pk;
}
inline ll inv(ll n,ll Mod)
{
    ll d,x,y;
    exgcd(n,Mod,x,y);
    return (x+Mod)%Mod==0?Mod:(x+Mod)%Mod;
}
inline ll C(ll n,ll m,ll pi,ll ki,ll pk)
{
    if(n<m)return 0;
    ll Mul1=fac(n,pi,pk),Mul2=fac(m,pi,pk),Mul3=fac(n-m,pi,pk);
    ll k=0;
    for(register ll i=n;i;i/=pi)k+=i/pi;
    for(register ll i=m;i;i/=pi)k-=i/pi;
    for(register ll i=n-m;i;i/=pi)k-=i/pi;
    return Mul1*inv(Mul2,pk)%pk*inv(Mul3,pk)%pk*qexp(pi,k,pk)%pk;
}
inline ll CRT(ll B,ll W)
{
    return B*inv(p/W,W)%p*(p/W)%p;
}
inline ll exLucas(ll n,ll m)
{
    ll res=0,tmp=p;
    for(register ll i=2;i<=tmp;++i)
        if(tmp%i==0)
        {
            ll ki=0,pk=1;
            while(tmp%i==0)tmp/=i,pk*=i,ki++;
            (res+=CRT(C(n,m,i,ki,pk),pk))%=p;
        }
    return res;
}
int main()
{
    read(p);
    int n,m,tot=0;
    read(n);read(m);
    for(register int i=1;i<=m;++i)read(w[i]),tot+=w[i];
    if(tot>n)
    {
        puts("Impossible");
        return 0;
    }
    for(register int i=1;i<=m;++i)(ans*=exLucas(n,w[i]))%=p,n-=w[i];
    write(ans,'\n');
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325157167&siteId=291194637