BJ Simulation Birthday Gift (Sterling number +ntt)

Source: Internet
Author: User
Tags bool cmath

Description

It's Jane's birthday today. Alice and Bob had some candies, so the two of them went to buy some white boxes to pack the candies as Jane's birthday present. They will randomly divide the boxes into two piles, one for Alice and one for Bob (at least one box per heap).

We know Alice has N1 a different candy, Bob has N2 the same candy (because Bob is lazy, so he buys the same candy directly), and then Alice buys the candy and the candy that Bob buys is completely different. They both put the candy in the box, and any box would not be empty. Alice and Bob each have their own rules for wrapping candies:

Alice has a personality, and she will decide how to wrap the candy according to the number of boxes. The number of boxes Alice has is K. If K is a prime number, she randomly wraps the candy (every candy is likely to go into any box). If k is not prime, she will randomly put 1 candies in one of the k−1 boxes and put the rest of the candies in the remaining box.

Bob has an M-type pigment (which is not white in the M), and he will randomly color the box so that each box is colored and different in color. Bob then randomly wraps the candy (every candy can go into any box).

The question is how many kinds of gifts Jane will receive.

Input

The first behavior is an integer t, which indicates the number of test data groups.

For each set of test data, the first behavior is four integer n1,n2,m,q, which represents Alice's number of candies, Bob's number of candies, Bob's number of colors, and the number of queries.

The next Q line, an integer N per line, represents the total number of boxes Alice and Bob purchased (2≤N≤M).

Output

For each set of test data, the output Q line, one integer per line, represents the number of gifts Jane may receive. Since the answer can be very large, please output the answer to 786433 modulo.

Sample Input

1

4 3 3 2

2

3

Sample Output

3

27

HINT

"Sample Interpretation"

Alice's Candy for A,b,c,d,bob candy for A,a,a, Bob's color is C1, C2, C3. The initial color of the box is W.

For n = 2,jane You can receive 3 kinds of gifts:

W{a,b,c,d},c1{a,a,a}

W{a,b,c,d},c2{a,a,a}

W{a,b,c,d},c3{a,a,a}

For n = 3,jane You can receive 27 kinds of gifts:

W{a},w{b,c,d},c1{a,a,a}

W{a},w{b,c,d},c2{a,a,a}

W{a},w{b,c,d},c3{a,a,a}

W{b},w{a,c,d},c1{a,a,a}

W{b},w{a,c,d},c2{a,a,a}

W{b},w{a,c,d},c3{a,a,a}

W{c},w{a,b,d},c1{a,a,a}

W{c},w{a,b,d},c2{a,a,a}

W{c},w{a,b,d},c3{a,a,a}

W{d},w{a,b,c},c1{a,a,a}

W{d},w{a,b,c},c2{a,a,a}

W{d},w{a,b,c},c3{a,a,a}

W{a,b},w{c,d},c1{a,a,a}

W{a,b},w{c,d},c2{a,a,a}

W{a,b},w{c,d},c3{a,a,a}

W{a,c},w{b,d},c1{a,a,a}

W{a,c},w{b,d},c2{a,a,a}

W{a,c},w{b,d},c3{a,a,a}

W{a,d},w{b,c},c1{a,a,a}

W{a,d},w{b,c},c2{a,a,a}

W{a,d},w{b,c},c3{a,a,a}

W{a,b,c,d},c1{a},c2{a,a}

W{a,b,c,d},c1{a},c3{a,a}

W{a,b,c,d},c2{a},c1{a,a}

W{a,b,c,d},c2{a},c3{a,a}

W{a,b,c,d},c3{a},c1{a,a}

W{a,b,c,d},c3{a},c2{a,a}

"Data scope and conventions"

For 10% data: 1≤n1,n2,m,q≤10

For 50% data: 1≤n1,n2,m,q≤1000

For all data: 1≤n1,n2,m,q≤105,t≤5




Puzzle: Sterling Number +ntt

Violence (40):

For Alice to be prime, it is equivalent to putting n essentially different balls into a box of the first class of Sterling in M-essentially identical boxes.

is composite when the equivalent of a n-m+1 ball from the N ball into a ball in the scheme number by the partition method is known as the combination number.

For Bob You can think of the number of scenarios in which you put n the same ball into m-boxes by multiplying the M-colors into m-boxes.

For the previous part of the available plate method (n-1 empty with m-1 board) the scheme number is c[n-1][m-1].

Brute force enumeration can be.

40 min Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string
> #include <algorithm> #include <cmath> using namespace std;
typedef unsigned long long ll;
const int MAXN=3E3+50;
const LL mod=786433;
BOOL ISNOTPRIME[MAXN];
int prime[maxn],primecnt;
ll P[MAXN][MAXN],C[MAXN][MAXN];
int n,n1,n2,m,t,q;
    inline int read () {char ch=getchar ();
    int i=0,f=1; while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') {i= (i<<3) + (i<<1) +ch-' 0 '; Ch=getchar ();}
return i*f;
    } inline void Pre () {isnotprime[1]=1; for (int i=2;i<maxn;i++) {if (!
        Isnotprime[i]) {prime[++primecnt]=i;
            } for (int j=1;j<=primecnt&&i*prime[j]<maxn;j++) {isnotprime[i*prime[j]]=1;
        if (i%prime[j]==0) break;
    }} for (int i=0;i<maxn;i++) {c[i][0]=1; } for (int i=1;i<maxn;i++) for (int j=1;j<maxn;j++) {c[i][j]= (c[i-1][j-1]%mod+c[i-1][j]%mod)%mod;
    }} inline void Striling () {p[0][0]=1;
for (int i=1;i<maxn;i++) for (int j=1;j<maxn;j++) p[i][j]= (p[i-1][j-1]%mod+1ll*p[i-1][j]%mod*j)%Mod;
    } inline ll Calc1 (int n1,int i) {if (i==n1) return 1; if (!
    Isnotprime[i]) {return p[n1][i]%mod;
} else return c[n1][i-1]%mod;
    } inline ll calc2 (int n2,int i) {return (c[n2-1][i-1]%mod*c[m][i]%mod)%mod;} int main () {pre ();
    Striling ();
    T=read ();
        while (t--) {n1=read (), N2=read (), M=read (), Q=read ();
            while (q--) {ll ans=0;
            N=read (); for (int i=1;i<n;i++) {if (i>n1| |
               N-I&GT;N2) continue;
            (ans+= (Calc1 (n1,i)%mod*calc2 (n2,n-i)%mod)%mod)%=mod;
        } cout<<ans%mod<<endl;
 }
    }
}


Positive solution (100):

The large number of the second kind of Stirling:

f[i]=1i!∑k=0i (−1) kcki∗ (i−k) n


Then convert:

f[i]=∑k=0i (−1) k (i−k) nk! (I−K)!

f[i]=∑k=0i (−1) kk!x (i−k) n (i−k)!

Positive Solution Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string
> #include <algorithm> #include <cmath> using namespace std;
typedef long Long LL;
const int mod=786433;
const int g=13;


const int MAXN=1E6+50;
	inline int read () {char ch=getchar ();
	int i=0,f=1; while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch>= ' 0 ' &&ch<= ' 9 ') {i= (i<<3) + (i<<1) +ch-' 0 '; Ch=getchar ();}
return i*f;
} ll A[MAXN],B[MAXN],C[MAXN],ALICE[MAXN],BOB[MAXN],ANS[MAXN],FAC[MAXN],INV[MAXN];
int n1,n2,m,t,q;
BOOL ISNOTPRIME[MAXN];
int prime[maxn],primecnt;
	inline ll KSM (ll A,ll b) {ll tmp=1;
		while (b) {if (b&1) tmp= (tmp*a)%mod;
		A= (a*a)%mod;
	b>>=1;
} return TMP;
	} struct NTT {ll pos[maxn],k; inline void Init (int len) {for (k=1;k<len+2000;k<<=1);//Data Card k<<1 for (ll i=1;i<k;i++) pos[i]= (i&am p;1)?
	((pos[i>>1]>>1) ^ (k>>1)):(pos[i>>1]>>1);
	}inline void NTT (ll *a) {for (int i=1;i<k;i++) if (I<pos[i]) swap (a[i],a[pos[i]);
			for (int m1=1,m2;m1<k;m1<<=1) {m2=m1<<1;
			ll Wn=ksm (G, (Mod-1)/m2);
				for (int i=0;i<k;i+=m2) {ll w=1;
					for (int j=0;j<m1;j++) {ll &A1=a[i+j],&B1=a[i+j+m1],t=1LL*w*B1%Mod;
					b1= (a1-t+mod)%mod;
					a1= (a1+t)%mod;
				w= (W*WN)%mod;
		}}}} inline void muitiply (ll *a,ll *b,ll *c) {NTT (a), NTT (b);
		for (ll i=0;i<k;i++) c[i]= (a[i]*b[i])%mod;
		Reverse (C+1,C+K);
		NTT (c);
		ll Inv=ksm (k,mod-2);
	for (ll i=0;i<k;i++) c[i]= (1ll*inv*c[i])%mod;
}}ntt;
	inline void Pre () {isnotprime[1]=1; for (int i=2;i<maxn;i++) {if (!
		Isnotprime[i]) {prime[++primecnt]=i;
			} for (int j=1;j<=primecnt&&i*prime[j]<maxn;j++) {isnotprime[i*prime[j]]=1;
		if (i%prime[j]==0) break;
	}} fac[0]=inv[0]=1;
	for (int i=1;i<maxn;i++) Fac[i]=fac[i-1]*i%mod; for (int i=1;i<maxn;i++) INV[I]=INV[I-1]*KSM (i,mod-2)%moD
	} int main () {//freopen ("lx.in", "R", stdin);
	Pre ();
	scanf ("%d", &t);
		while (t--) {memset (a,0,sizeof (A));
		memset (b,0,sizeof (B));
		scanf ("%d%d%d%d", &n1,&n2,&m,&q); Ntt.
		Init (N1+N2);
		A[0]=1;
		for (int i=1;i<=n1;i++) A[i]=a[i-1]*i%mod;
		for (int i=0;i<=n1;i++) A[I]=B[I]=KSM (a[i],mod-2);
		for (int i=1;i<=n1;i+=2) a[i]=a[i]* (Mod-1)%mod;
		for (int i=0;i<=n1;i++) B[I]=B[I]*KSM (i,n1)%mod;
		Ntt.muitiply (A,B,C);
		memset (alice,0,sizeof (Alice));
		memset (bob,0,sizeof (Bob));
		memset (ans,0,sizeof (Ans)); for (int i=1;i<=n1;i++) {if (!
			Isnotprime[i]) alice[i]=c[i];
			else if (i==n1) alice[i]=1;
		else Alice[i]=fac[n1]*inv[i-1]%mod*inv[n1-i+1]%mod;
		} for (int i=1;i<=n2;i++) Bob[i]=fac[m]*inv[i]%mod*inv[m-i]%mod*fac[n2-1]*inv[i-1]%mod*inv[n2-i]%mod;
		Ntt.muitiply (Alice,bob,ans);
			while (q--) {int x;
			scanf ("%d", &x);
		printf ("%lld\n", ans[x]);
 }
	}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.