[BZOJ2142] Gift (Extended Lucas)

2142: Gift

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 2286  Solved: 1009
[Submit][Status][Discuss]

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.

HINT

 

Source

 
[ Submit ][ Status ][ Discuss ]

The Big Collection of Number Theory.

The answer is obviously $C_n^{n - w[1]}C_{n - w[1]}^{w[2]}C_{n - w[1] - w[2]}^{w[3]} ......$, the modulus is not coprime to become a difficulty.

Extended Lucas: Decompose modulo prime factors and combine them with CRT.

The problem is only to find $n!%p_i^{k_i}$.

First of all, because the inverse element cannot be found because it is not coprime, take out all $p_i$ in $n!$: $ans=\lfloor \frac{n}{p} \rfloor+\lfloor \frac{n}{p^2 } \rfloor+\lfloor \frac{n}{p^3} \rfloor+...$

Then consider how to do the rest, use $F(n,p_i,p_i^{k_i})$ to represent the answer, $f(n,p_i,p_i^{k_i})$ to represent $\prod_{i=1,i \perp p_i}^{n}i(mod\ p_i^{k_i})$, then:

$F(n,p_i,p_i^{k_i})=\lfloor\frac{n}{p_i}\rfloor!\times p_i^{\lfloor\frac{n}{p_i}\rfloor}\times f(p_i^{k_i},p_i,p_i^{k_i})^{\lfloor\frac{n}{p_i^{k_i}}\rfloor}\times f(n\%p_i^{k_i})$

It can be processed recursively, and exgcd is used.

The complexity seems to be, $O(\sqrt{P}+m\log_{2}^{2}n\log_{p_i})$, but it is definitely not enough.

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=l; i<=r; i++)
 4 typedef long long ll;
 5 using namespace std;
 6 
 7 const int N=10010;
 8 ll mod,n,m,w[10],ans,x,y,Mod[N],st[N],r[N],num;
 9 
10 ll ksm(ll a,ll b,ll p){
11     ll res;
12     for (res=1; b; a=a*a%p,b>>=1)
13         if (b&1) res=res*a%p;
14     return res;
15 }
16 
17 ll exgcd(ll a,ll b,ll &x,ll &y){
18     ll d=a;
19     if (b) d=exgcd(b,a%b,y,x),y-=a/b*x; else x=1,y=0;
20     return d;
21 }
22 
23 ll inv(ll t,ll p){ ll x,y; exgcd(t,p,x,y); return (x+p)%p; }
24 
25 ll F(ll n,ll pi,ll pk){
26     if (!n) return 1;
27     ll ans=1;
28     rep(i,2,pk) if (i%pi) ans=ans*i%pk;
29     ans=ksm(ans,n/pk,pk);
30     rep(i,2,n%pk) if (i%pi) ans=ans*i%pk;
31     return ans*F(n/pi,pi,pk)%pk;
32 }
33 
34 ll exlucas(ll n,ll m,ll pi,ll pk){
35     if (m>n) return 0;
36     ll a=F(n,pi,pk),b=F(m,pi,pk),c=F(n-m,pi,pk);
37     ll k=0;
38     for (ll i=n; i; i/=pi) k+=i/pi;
39     for (ll i=m; i; i/=pi) k-=i/pi;
40     for (ll i=n-m; i; i/=pi) k-=i/pi;
41     return a*inv(b,pk)%pk*inv(c,pk)%pk*ksm(pi,k,pk)%pk;
42 }
43 
44 ll CRT(ll n,ll r[],ll m[]){
45     ll M=1,res=0,w;
46     rep(i,1,n) M*=m[i];
47     rep(i,1,n) w=M/m[i],res=(res+w*inv(w,m[i])*r[i])%M;
48     return (res+M)%M;
49 }
50 
51 ll par(ll n,ll m[],ll st[]){
52     ll num=0;
53     for (ll i=2; i*i<=n; i++) if (n%i==0){
54         ll pk=1;
55         while (n%i==0) pk*=i,n/=i;
56         m[++num]=pk; st[num]=i;
57     }
58     if (n>1) m[++num]=n,st[num]=n;
59     return num;
60 }
61 
62 ll excomb(ll n,ll m){
63     rep(i,1,num) r[i]=exlucas(n,m,st[i],Mod[i]);
64     return CRT(num,r,Mod);
65 }
66 
67 int main(){
68     freopen("bzoj2142.in","r",stdin);
69     freopen("bzoj2142.out","w",stdout);
70     scanf("%lld\n",&mod); scanf("%lld%lld",&n,&m);
71     ll sum=0; rep(i,1,m) scanf("%lld",&w[i]),sum+=w[i];
72     if (n<sum){ puts("Impossible"); return 0; }
73     num=par(mod,Mod,st); ans=1;
74     rep(i,1,m) n-=w[i-1],ans=ans*excomb(n,w[i])%mod;
75     printf("%lld\n",ans);
76     return 0;
77 }

 

Guess you like

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