zoukankan      html  css  js  c++  java
  • 洛谷.4717.[模板]快速沃尔什变换(FWT)

    题目链接

    https://www.mina.moe/archives/7598

    //285ms	3.53MB
    #include <cstdio>
    #include <cctype>
    #include <cstring>
    #include <algorithm>
    //#define gc() getchar()
    #define MAXIN 300000
    #define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
    #define inv2 499122177
    #define mod 998244353
    #define Add(x,y) (x+y>=mod?x+y-mod:x+y)
    #define Sub(x,y) (x<y?x-y+mod:x-y)
    typedef long long LL;
    const int N=(1<<17)+5;
    
    int A[N],B[N],C[N],tA[N],tB[N];
    char IN[MAXIN],*SS=IN,*TT=IN;
    
    inline int read()
    {
    	int now=0;register char c=gc();
    	for(;!isdigit(c);c=gc());
    	for(;isdigit(c);now=now*10+c-'0',c=gc());
    	return now;
    }
    void FWT_OR(int *a,int lim,int opt)
    {
    	for(int i=2; i<=lim; i<<=1)
    		for(int j=0,mid=i>>1; j<lim; j+=i)
    			for(int k=j; k<j+mid; ++k)
    				if(opt==1) a[k+mid]=Add(a[k+mid],a[k]);
    				else a[k+mid]=Sub(a[k+mid],a[k]);
    }
    void FWT_AND(int *a,int lim,int opt)
    {
    	for(int i=2; i<=lim; i<<=1)
    		for(int j=0,mid=i>>1; j<lim; j+=i)
    			for(int k=j; k<j+mid; ++k)
    				if(opt==1) a[k]=Add(a[k],a[k+mid]);
    				else a[k]=Sub(a[k],a[k+mid]);
    }
    void FWT_XOR(int *a,int lim,int opt)
    {
    	for(int i=2; i<=lim; i<<=1)
    		for(int j=0,mid=i>>1; j<lim; j+=i)
    			for(int k=j,x,y; k<j+mid; ++k)
    			{
    				x=a[k], y=a[k+mid];
    				a[k]=Add(x,y), a[k+mid]=Sub(x,y);
    				if(opt==-1) a[k]=1ll*a[k]*inv2%mod, a[k+mid]=1ll*a[k+mid]*inv2%mod;
    			}
    }
    
    int main()
    {
    	int n=1<<read();
    	for(int i=0; i<n; ++i) tA[i]=read();
    	for(int i=0; i<n; ++i) tB[i]=read();
    
    	memcpy(A,tA,sizeof A), memcpy(B,tB,sizeof B);
    	FWT_OR(A,n,1), FWT_OR(B,n,1);
    	for(int i=0; i<n; ++i) C[i]=1ll*A[i]*B[i]%mod;
    	FWT_OR(C,n,-1);
    	for(int i=0; i<n; ++i) printf("%d ",C[i]);
    	putchar('
    '); //FWT_OR(A,n,-1), FWT_OR(B,n,-1);
    
    	memcpy(A,tA,sizeof A), memcpy(B,tB,sizeof B);
    	FWT_AND(A,n,1), FWT_AND(B,n,1);
    	for(int i=0; i<n; ++i) C[i]=1ll*A[i]*B[i]%mod;
    	FWT_AND(C,n,-1);
    	for(int i=0; i<n; ++i) printf("%d ",C[i]);
    	putchar('
    ');
    
    	memcpy(A,tA,sizeof A), memcpy(B,tB,sizeof B);
    	FWT_XOR(A,n,1), FWT_XOR(B,n,1);
    	for(int i=0; i<n; ++i) C[i]=1ll*A[i]*B[i]%mod;
    	FWT_XOR(C,n,-1);
    	for(int i=0; i<n; ++i) printf("%d ",C[i]);
    	putchar('
    ');
    
    	return 0;
    }
    
  • 相关阅读:
    HTTP 无法注册 URL http://+:xxxxx/ServicesName/。进程不具有此命名空间的访问权限
    C语言中宏的一些特别用法
    static和const的比较和解释
    堆和栈的区别
    c++中const用法
    链表常见笔试题
    自绘实现半透明水晶按钮 .
    C++面试题
    C/C++面试题大汇总
    C++ 值传递、指针传递、引用传递详解
  • 原文地址:https://www.cnblogs.com/SovietPower/p/10024378.html
Copyright © 2011-2022 走看看