跳到正文

个人记录

卷积:ans_S=\sum f_T*g_{S|T}

wukaichen888约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月19日
收录
2026年7月22日
更新
2026年11月8日
篇幅
约 2 分钟

版本

  1. v1当前

不考虑正确性:直接对 ff 做高维前缀和,ff 乘上 gg,再反着做高维后缀和。

错因显然,对 gg 做带 −1popcount-1^{popcount} 系数的高维后缀和预处理即可。

checker

C++
#pragma target("avx512f,sse2,sse3,sse4,sse4.2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll

const ll N=1e6+5;
ll n=5,K;
ull f[N],g[N],F[N],G[N];

ull bf[N],test[N];

mt19937 rnd(time(0));

int main(){
	K=(1<<n);
	for(int i=0;i<K;i++) f[i]=F[i]=rnd();
	for(int i=0;i<K;i++) g[i]=G[i]=rnd();
	
	//		bf
	
	for(int i=0;i<K;i++)
		for(int j=0;j<K;j++)
			bf[i]+=f[j]*g[i|j];
	
	//		test
	
	for(int b=0;b<n;b++)
		for(int i=0;i<K;i++)
			if((i>>b)&1){
				F[i]+=F[i^(1<<b)];
				G[i^(1<<b)]-=G[i];}
	for(int i=0;i<K;i++) test[i]=F[i]*G[i];
	for(int b=0;b<n;b++)
		for(int i=0;i<K;i++)
			if((i>>b)&1)
				test[i^(1<<b)]+=test[i];
	
	//		check
	
	for(int i=0;i<K;i++) printf("%llu ",bf[i]); printf("\n");
	for(int i=0;i<K;i++) printf("%llu ",test[i]); printf("\n");
	for(int i=0;i<K;i++) assert(bf[i]==test[i]);
	
	return 0;}
原文