MTT代码求调
查看原帖
MTT代码求调
1201743
QinYulang楼主2024/6/22 23:54
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO {
	const int IN_LEN=10000000,OUT_LEN=10000000;
	char ibuf[IN_LEN],obuf[OUT_LEN],*ih=ibuf+IN_LEN,*oh=obuf,*lastin=ibuf+IN_LEN,*lastout=obuf+OUT_LEN-1;
	inline char getchar_() {
		return (ih==lastin)&&(lastin=(ih=ibuf)+fread(ibuf,1,IN_LEN,stdin),ih==lastin)?EOF:*ih++;
	}
	inline void putchar_(const char x) {
		if(oh==lastout)fwrite(obuf,1,oh-obuf,stdout),oh=obuf;
		*oh++=x;
	}
	inline void flush() {
		fwrite(obuf,1,oh-obuf,stdout);
	}
}
using namespace fast_IO;
#define getchar() getchar_()
#define putchar(x) putchar_((x))
typedef long long LL;
#define rg register
template <typename T> inline T max(const T a,const T b) {
	return a>b?a:b;
}
template <typename T> inline T min(const T a,const T b) {
	return a<b?a:b;
}
template <typename T> inline T mind(T&a,const T b) {
	a=a<b?a:b;
}
template <typename T> inline T maxd(T&a,const T b) {
	a=a>b?a:b;
}
template <typename T> inline T abs(const T a) {
	return a>0?a:-a;
}
template <typename T> inline T gcd(const T a,const T b) {
	if(!b)return a;
	return gcd(b,a%b);
}
template <typename T> inline T square(const T x) {
	return x*x;
};
template <typename T> inline void read(T&x) {
	char cu=getchar();
	x=0;
	bool fla=0;
	while(!isdigit(cu)) {
		if(cu=='-')fla=1;
		cu=getchar();
	}
	while(isdigit(cu))x=x*10+cu-'0',cu=getchar();
	if(fla)x=-x;
}
template <typename T> void printe(const T x) {
	if(x>=10)printe(x/10);
	putchar(x%10+'0');
}
template <typename T> inline void print(const T x) {
	if(x<0)putchar('-'),printe(-x);
	else printe(x);
}
const int maxn=262145;
int n,m;
struct Ntt {
	LL mod,a[maxn],b[maxn];
	inline LL pow(LL x,LL y) {
		rg LL res=1;
		for(; y; y>>=1,x=x*x%mod)if(y&1)res=res*x%mod;
		return res;
	}
	int lenth,Reverse[maxn];
	inline void init(const int x) {
		rg int tim=0;
		lenth=1;
		while(lenth<=x)lenth<<=1,tim++;
		for(rg int i=0; i<lenth; i++)Reverse[i]=(Reverse[i>>1]>>1)|((i&1)<<(tim-1));
	}
	inline void NTT(LL*A,const int fla) {
		for(rg int i=0; i<lenth; i++)if(i<Reverse[i]) swap(A[i],A[Reverse[i]]);
		for(rg int i=1; i<lenth; i<<=1) {
			LL w=pow(3,(mod-1)/i/2);
			if(fla==-1)w=pow(w,mod-2);
			for(rg int j=0; j<lenth; j+=(i<<1)) {
				LL K=1;
				for(rg int k=0; k<i; k++,K=K*w%mod) {
					const LL x=A[j+k],y=A[j+k+i]*K%mod;
					A[j+k]=(x+y)%mod;
					A[j+k+i]=(mod+x-y)%mod;
				}
			}
		}
		if(fla==-1) {
			const int inv=pow(lenth,mod-2);
			for(rg int i=0; i<lenth; i++)A[i]=A[i]*inv%mod;
		}
	}
} Q[3];
LL EXgcd(const LL a,const LL b,LL &x,LL &y) {
	if(!b) {
		x=1,y=0;
		return a;
	}
	const LL res=EXgcd(b,a%b,y,x);
	y-=a/b*x;
	return res;
}
inline LL msc(LL a,LL b,LL mod) {
	LL v=(a*b-(LL)((long double)a/mod*b+1e-8)*mod);
	return v<0?v+mod:v;
}
int N,a[3],p[3];
LL CRT() {
	LL P=1,sum=0;
	for(rg int i=1; i<=N; i++)P*=p[i];
	for(rg int i=1; i<=N; i++) {
		const LL m=P/p[i];
		LL x,y;
		EXgcd(p[i],m,x,y);
		sum=(sum+msc(msc(y,m,P),a[i],P))%P;
	}
	return sum;
}
int P;
inline void MTT(){
	Q[0].NTT(Q[0].a,1),Q[0].NTT(Q[0].b,1);
	Q[1].NTT(Q[1].a,1),Q[1].NTT(Q[1].b,1);
	Q[2].NTT(Q[2].a,1),Q[2].NTT(Q[2].b,1);
	for(rg int i=0; i<Q[0].lenth; i++)
		Q[0].a[i]=(LL)Q[0].a[i]*Q[0].b[i]%Q[0].mod,
		          Q[1].a[i]=(LL)Q[1].a[i]*Q[1].b[i]%Q[1].mod,
		                    Q[2].a[i]=(LL)Q[2].a[i]*Q[2].b[i]%Q[2].mod;
	Q[0].NTT(Q[0].a,-1);
	Q[1].NTT(Q[1].a,-1);
	Q[2].NTT(Q[2].a,-1);
	N=2,p[1]=Q[0].mod,p[2]=Q[1].mod;
}
auto main() [[Ofast]] -> signed{
	P = 1e18;
    string s;
    getline(cin,s);n=s.size()-1;
    string s1;
    getline(cin,s1);m=s.size()-1;
	Q[0].mod=469762049,Q[0].init(n+m);
	Q[1].mod=998244353,Q[1].init(n+m);
	Q[2].mod=1004535809,Q[2].init(n+m);
	for(rg size_t i=s.size()-1,j=0;i>=0;i--,j++){
        Q[0].a[j]=Q[1].a[j]=Q[2].a[j]=s[i]-'0';
    }
    // read(Q[0].a[i]),Q[2].a[i]=Q[1].a[i]=Q[0].a[i];
	for(rg size_t i=s.size()-1,j=0;i>=0;i--,j++){
        Q[0].b[j]=Q[1].b[j]=Q[2].b[j]=s[i]-'0';
    }
    // read(Q[0].b[i]),Q[2].b[i]=Q[1].b[i]=Q[0].b[i];
	const int INV=Q[2].pow(Q[0].mod,Q[2].mod-2)*Q[2].pow(Q[1].mod,Q[2].mod-2)%Q[2].mod;
	for(rg int i=0; i<=n+m; i++) {
		a[1]=Q[0].a[i],a[2]=Q[1].a[i];
		const LL ans1=CRT();
		const LL ans2=((Q[2].a[i]-ans1)%Q[2].mod+Q[2].mod)%Q[2].mod*INV%Q[2].mod;
		print((ans2*Q[0].mod%P*Q[1].mod%P+ans1)%P);
	}
	return flush(),0;
}

2024/6/22 23:54
加载中...