首页 > 其他分享 >【模板】快速沃尔什变换 FWT

【模板】快速沃尔什变换 FWT

时间:2022-11-16 20:24:04浏览次数:33  
标签:int void 沃尔什 FWT include LL 模板

解决形如

\[c_k=\sum_{i\oplus j=k}a_ib_j. \]

的卷积式子,这里解决 \(\oplus=\{\cup,\cap,\text{xor}\}\)。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr,##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
typedef long long LL;
const int P=998244353;
void red(LL&x){x=(x%P+P)%P;}
void fwt_or(LL *f,int n,int op){
	for(int len=2,k=1;len<=n;len<<=1,k<<=1)
		for(int i=0;i<n;i+=len)
			for(int j=0;j<k;j++)
				red(f[i+j+k]+=f[i+j]*op);
}
void fwt_and(LL *f,int n,int op){
	for(int len=2,k=1;len<=n;len<<=1,k<<=1)
		for(int i=0;i<n;i+=len)
			for(int j=0;j<k;j++)
				red(f[i+j]+=f[i+j+k]*op);
}
void fwt_xor(LL *f,int n,int op){
	for(int len=2,k=1;len<=n;len<<=1,k<<=1)
		for(int i=0;i<n;i+=len)
			for(int j=0;j<k;j++){
				LL ta=f[i+j],tb=f[i+j+k];
				red(f[i+j]=(ta+tb)*op);
				red(f[i+j+k]=(ta-tb)*op);
			}
}
void multiple(LL *a,LL *b,LL *c,int n){
	for(int i=0;i<n;i++) red(c[i]=a[i]*b[i]);
}
int n;
LL a[1<<17],b[1<<17],c[1<<17];
int main(){
//	#ifdef LOCAL
	 	freopen("input.in","r",stdin);
//	#endif
	rewind(stdin);
	scanf("%d",&n),n=1<<n;
	for(int i=0;i<n;i++) scanf("%lld",&a[i]);
	for(int i=0;i<n;i++) scanf("%lld",&b[i]);
	fwt_or(a,n,1),fwt_or(b,n,1),multiple(a,b,c,n),fwt_or(c,n,-1);
	for(int i=0;i<n;i++) printf("%lld%c",c[i]," \n"[i==n-1]);
	rewind(stdin);
	scanf("%d",&n),n=1<<n;
	for(int i=0;i<n;i++) scanf("%lld",&a[i]);
	for(int i=0;i<n;i++) scanf("%lld",&b[i]);
	fwt_and(a,n,1),fwt_and(b,n,1),multiple(a,b,c,n),fwt_and(c,n,-1);
	for(int i=0;i<n;i++) printf("%lld%c",c[i]," \n"[i==n-1]);
	rewind(stdin);
	scanf("%d",&n),n=1<<n;
	for(int i=0;i<n;i++) scanf("%lld",&a[i]);
	for(int i=0;i<n;i++) scanf("%lld",&b[i]);
	fwt_xor(a,n,1),fwt_xor(b,n,1),multiple(a,b,c,n),fwt_xor(c,n,499122177);
	for(int i=0;i<n;i++) printf("%lld%c",c[i]," \n"[i==n-1]);
	return 0;
}

标签:int,void,沃尔什,FWT,include,LL,模板
From: https://www.cnblogs.com/caijianhong/p/solution-fwt.html

相关文章

  • 模板奇异递归+扩展方法
    #include<iostream>template<classderived>structbase{derivedgetDerivedType(){};voidinterface(){static_cast<derived*>(this)->interface();};};s......
  • Aspose.Words利用Word模板导出Word文档
       今天工作中遇到了导出Word文档的问题,但是在搜索Aspose.Words导出Word文档时发现网上的方法都是有头没尾的,有的只有一小段实例,让人看着摸不着头脑。借着https://w......
  • 第7章 类模板array和vector、异常捕获(笔记)
    7.1简介数据结构7.2array对象一组具有相同类型、连续的内存区域,用下标法和索引来操作。7.3array对象的声明array<类型,大小>array对象名7.4使用array对象的例子......
  • 尚硅谷vue课程之模板语法
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content......
  • P8436 【模板】边双连通分量
    P8436【模板】边双连通分量//这个是看边和点,而不是看点和点#include<bits/stdc++.h>usingnamespacestd;constintN=5e5+5;constintM=2e6+5;inth[N],ne[M<<1......
  • 4.django-模板
    在django中,模板引擎(DTL)是一种可以让开发者将服务端数据填充到html页面中的完成渲染的技术模板引擎的原理分为以下三步:在项目配置文件中指定保存模板文件的的模板目录,一......
  • P8435 【模板】点双连通分量
    P8435【模板】点双连通分量#include<bits/stdc++.h>usingnamespacestd;constintN=5e5+5;constintM=4e6+6;inth[N],ne[M],e[M],tot;voidadd(intfrom,int......
  • Word13 《经费联审结算单》模板office真题
    1.根据题目一的要求,打开素材文件,点击【文件】-【另存为】,选择【当前文件夹】,命名为Word。   2.根据题目二的要求,在【布局】里点击【页面设置】的右下角,打开页面设......
  • C++模板——函数模板
    1.1定义函数模板1.2使用函数模板1.3两阶段翻译Two-PhaseTranslation1.3.1模板的编译和链接问题1.4多模板参数1.4.1引入额外模板参数作为返回值类型1.4.......
  • 算法基础:离散化及模板详解
    ⭐写在前面的话:本系列文章旨在复习算法刷题中常用的基础算法与数据结构,配以详细的图例解释,总结相应的代码模板,同时结合例题以达到最佳的学习效果。本专栏面向算法零基础但有......