[51nod 1538] a problem--characteristic polynomial, polynomial modulus

Source: Internet
Author: User
Tags modulus time limit

The problem is equivalent to selecting a number of scenarios where the ball is arranged so that the total weight is n

Apparently F[n]=∑f[n-ai]

Here AI will go to 23333, direct write O (N^2logn) of the brute force solution will tle consider using polynomial modulo + multiplication to find x^n mod h (x)

Kathang time limit 5s at the beginning of the machine ran 20s is running but pay attention to the rapid power of the high to the low when the calculation so long as the logn time to take the mold and not 2logn times there is no need for the number of the time to delete this can keep the length of 65536

Preprocessing rotation factor is much faster

#include "bits/stdc++.h" using namespace std;
typedef long Long LL;

const int n=66666,m=1000005,p=104857601,g=3,l=23333;
	int power (int a,int t) {int r=1;
		while (t) {if (t&1) r= (LL) r*a%p;
	A= (LL) a*a%p;t>>=1;
} return R;

} int _wn[25],z[25][n];
	void NTT (int a[],int len,int DFT) {int i,j=len>>1,k,l,u,v,inv;
		for (i=1;i<len-1;i++) {if (i<j) swap (a[i],a[j]);
	For (k=len>>1; (j^=k) <k;k>>=1);
		} if (!_wn[0]) {int c=0,w,wn;
		for (_wn[i=22]=39193363;i;i--) _wn[i-1]= (LL) _wn[i]*_wn[i]%p; For (l=2;l<= (1<<16), l<<=1) for (i=l>>1,wn=_wn[++c],j=0;j< (1<<16); j+=l) for (w=1,k=j;k
	<j+i;k++,w= (LL) w*wn%p) z[c][k]=w;
	} int c=0,*w; for (l=2;l<=len;l<<=1) for (i=l>>1,w=z[++c],j=0;j<len;j+=l) for (k=j;k<j+i;k++) u=A[k],v= (LL) A[
	k+i]*w[k]%p,a[k]= (u+v)%p,a[k+i]= (u-v+p)%P;
		if (dft==-1) {inv=power (len,p-2);
		for (i=0;i<len;i++) a[i]= (LL) a[i]*inv%p; 
	for (i=1;i<len/2;i++) swap (a[i],a[len-i]); }} int W1[n],w2[n],w3[n];
	void Convol (int a[],int b[],int r[],int len) {memcpy (w1,a,len<<2);
	memcpy (W2,B,LEN&LT;&LT;2);
	NTT (w1,len,1);
	NTT (w2,len,1);
	for (int i=0;i<len;i++) w3[i]= (LL) w1[i]*w2[i]%p;
	NTT (w3,len,-1);
memcpy (R,W3,LEN&LT;&LT;2);

} int wi1[n],wi2[n],wi3[n],wi4[n];
	void inverse (int a[],int t[],int len) {if (len==1) {t[0]=power (a[0],p-2); return;}
	Inverse (a,wi1,len>>1);
	memset (wi2,0,len<<3);
	memset (wi3,0,len<<3);
	memcpy (wi2,wi1,len<<1);
	memcpy (WI3,A,LEN&LT;&LT;2);
	NTT (wi2,len<<1,1);
	NTT (wi3,len<<1,1);
	for (int i=0;i< (len<<1); i++) wi4[i]= ((2*wi2[i]-(LL) wi2[i]*wi2[i]%p*wi3[i])%p+p)%P;
	NTT (wi4,len<<1,-1);
memcpy (T,WI4,LEN&LT;&LT;2);

} int wm1[n],wm2[n],wm3[n],wm4[n];
	void modulo (int a[],int b[],int r[],int len) {int dega,degb,degc;
	for (Dega=len;dega&&a[dega]==0;--dega);
	for (DEGB=LEN;DEGB&AMP;&AMP;B[DEGB]==0;--DEGB);
	if (DEGA&LT;DEGB) {for (int i=0;i<len;i++) R[i]=a[i];return;}
	DEGC=DEGA-DEGB; memset (wm1,0, len<<2);
	memset (WM2,0,LEN&LT;&LT;2);
	memset (WM3,0,LEN&LT;&LT;2);
	memset (WM4,0,LEN&LT;&LT;2);
	for (int i=0;i<=degc;i++) wm1[i]=a[dega-i];
	for (int i=0;i<=degb;i++) wm2[i]=b[degb-i];
	Inverse (wm2,wm3,len>>1);
	for (int i=degc+1;i<len;i++) wm3[i]=0;
	Convol (Wm1,wm3,wm4,len);
	memset (WM1,0,LEN&LT;&LT;2);
	for (int i=0;i<=degc;i++) wm1[degc-i]=wm4[i];
	memset (WM2,0,LEN&LT;&LT;2);
	memcpy (WM2,B,DEGB+1&LT;&LT;2);
	Convol (Wm1,wm2,wm3,len);
for (int i=0;i<=dega;i++) r[i]= (a[i]-wm3[i]+p)%P;

} int wl1[n],wl2[n];
	void Sqr (int a[],int d[],int len) {memset (wl1,0,len<<2);
	memset (WL2,0,LEN&LT;&LT;2);
	memcpy (wl1,a,len<<1);
	NTT (wl1,len,1);
	for (int i=0;i<len;i++) wl1[i]= (LL) wl1[i]*wl1[i]%p;
	NTT (wl1,len,-1);
	Modulo (Wl1,d,wl2,len);
memcpy (a,wl2,len<<1);
	} void work (int w[],int mod[],int len,ll t) {int l=0,i;
	while ((1ll<<l) <=t) ++l;
	while ((t>>l) <=l)--l;
	w[t>>l+1]=1;
		while (l>=0) {sqr (W,mod,len);
if ((t>>l) &1) {			for (int i=l;i>=1;i--) w[i]=w[i-1];
			w[0]=0;
			for (int i=0;i<l;i++) w[i]= ((w[i]-(LL) w[l]*mod[i])%p+p;
		w[l]=0;
	}--l;
}} int n,a[m],a,b,w[n],mo[n],res[n];

LL m;
	int main () {cin>>n>>m>>a[1]>>a>>b;
	for (int i=2;i<=n;i++) a[i]= ((LL) a[i-1]*a+b)%l+1;
	for (int i=1;i<=n;i++) if (a[i]<=23333) w[a[i]]++;
	for (int i=mo[l]=1;i<=l;i++) mo[l-i]= (p-w[i])%P;
	Work (RES,MO,1&LT;&LT;16,M+L-1);
	printf ("%d\n", Res[l-1]);
return 0; }


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.