快速沃尔什变换(FWT) 与 快速莫比乌斯变换 与 快速沃尔什变换公式推导
后面的图片将会告诉:
如何推出FWT的公式tf
如何推出FWT的逆公式utf
用的是设系数,求系数的方法!
=========================================================
以一种高度思考
http://picks.logdown.com/posts/179290-fast-walsh-hadamard-transform
加和乘的定义
大小为1时的公式
https://blog.csdn.net/zhshrs/article/details/54838466
证明
https://blog.csdn.net/john123741/article/details/76576925
写成
or
tf(A)=(tf(A0),tf(A1)+tf(A0))
utf(A)=(utf(A0),utf(A1)-utf(A0))
and
tf(A)=(tf(A0)+tf(A1),tf(A1))
utf(A)=(utf(A0)-utf(A1),utf(A1))
xor
tf(A)=(tf(A0)+tf(A1),tf(A0)-tf(A1))
utf(A)=(1/2*(utf(A0)+utf(A1)),1/2*(utf(A0)-utf(A1)))
更好记忆一点
对于or,and
0 = 0 or 0 对应 or tf(A0)[utf(A0)]
1 = 1 and 1 对应 and tf(A1)[utf(A1)]
=================================
and or xor 二进制操作
0~2^(k-1)-1时,最高位为0
2^(k-1)~2^k-1时,最高位为1
e.g.
000
001
010
011
----
100
101
110
111


P.S.:
FFT和FWT,两者虽然实现方法都是内容分半(二分),但是其本质原理不同。
FFT: http://www.cnblogs.com/zwfymqz/p/8244902.html
快速莫比乌斯变换
study from :
https://yhx-12243.github.io/OI-transit/records/vijos%20%234.html
适用于and or,xor 行不通
FMT写法和FWT是一样的,两者考虑问题角度不一样。
https://www.luogu.org/problemnew/show/P4717

存储函数地址
void (*addr1[3])(ll a[])={fwt1,fwt2,fwt3};
void (*addr2[3])(ll a[])={ufwt1,ufwt2,ufwt3};
also can use (int type)=1ll*...
注意当数字范围为[0,x]
遍历范围 [0,z) tot=2*x(大于any i op j, 其中i,j in [0,x])
而数组开z大小
对于代码中的tot,要写成2^k的形式,
否则如写成tot,e.g. tot=9, 8(i)+3(j)>9。
对应好记忆
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long const double eps=1e-;
const ll inf=1e9;
const ll mod=;
const int maxn=(<<)*; ///any n or <2n /**
补全为2^k
**/ ll aa[maxn],bb[maxn],a[maxn],b[maxn];
int tot;
ll mod_inv_2=(mod+)/; ///or
void fwt1(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j+cnt_pre]+=a[i+j])%=mod;
} void ufwt1(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j+cnt_pre]-=a[i+j]-mod)%=mod;
} ///and
void fwt2(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j]+=a[i+j+cnt_pre])%=mod;
} void ufwt2(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j]-=a[i+j+cnt_pre]-mod)%=mod;
} ///xor
void fwt3(ll *a)
{
int cnt_pre,cnt_cur,i,j;
ll x;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
{
x=a[i+j];
a[i+j]=(a[i+j]+a[i+j+cnt_pre])%mod;
a[i+j+cnt_pre]=(x-a[i+j+cnt_pre]+mod)%mod;
}
} void ufwt3(ll *a)
{
int cnt_pre,cnt_cur,i,j;
ll x;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
{
x=a[i+j];
a[i+j]=(a[i+j]+a[i+j+cnt_pre])*mod_inv_2%mod;
a[i+j+cnt_pre]=(x-a[i+j+cnt_pre]+mod)*mod_inv_2%mod; ///x-
}
} int main()
{
int n,i,j;
scanf("%d",&n);
tot=<<n;
for (i=;i<tot;i++)
scanf("%lld",&aa[i]);
for (i=;i<tot;i++)
scanf("%lld",&bb[i]); void (*addr1[]) (ll a[])={fwt1,fwt2,fwt3};
void (*addr2[]) (ll a[])={ufwt1,ufwt2,ufwt3}; for (i=;i<;i++)
{
memcpy(a,aa,sizeof(aa));
memcpy(b,bb,sizeof(bb));
(*addr1[i])(a);
(*addr1[i])(b);
for (j=;j<tot;j++)
a[j]=a[j]*b[j]%mod;
(*addr2[i])(a);
for (j=;j<tot;j++)
printf("%lld%c",a[j],j==tot-?'\n':' ');
}
return ;
}
/*
0
1000000
1000000 3
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
*/
对应下方的图片公式推导
tf a0+a1 a1-a0
utf a1 a0
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long const double eps=1e-;
const ll inf=1e9;
const ll mod=;
const int maxn=(<<)*; ///any n or <2n /**
补全为2^k
**/ ll aa[maxn],bb[maxn],a[maxn],b[maxn];
int tot;
ll mod_inv_2=(mod+)/; ///or
void fwt1(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j+cnt_pre]+=a[i+j])%=mod;
} void ufwt1(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j+cnt_pre]-=a[i+j]-mod)%=mod;
} ///and
void fwt2(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j]+=a[i+j+cnt_pre])%=mod;
} void ufwt2(ll *a)
{
int cnt_pre,cnt_cur,i,j;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
(a[i+j]-=a[i+j+cnt_pre]-mod)%=mod;
} ///xor
void fwt3(ll *a)
{
int cnt_pre,cnt_cur,i,j;
ll x;
for (cnt_pre=,cnt_cur=;cnt_pre<tot;cnt_pre<<=,cnt_cur<<=)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
{
x=a[i+j];
(a[i+j]+=a[i+j+cnt_pre])%=mod;
(a[i+j+cnt_pre]-=x-mod)%=mod; ///-x
}
} void ufwt3(ll *a)
{
int cnt_pre,cnt_cur,i,j;
ll x;
for (cnt_pre=tot>>,cnt_cur=tot;cnt_pre>;cnt_pre>>=,cnt_cur>>=)
// for (cnt_pre=1,cnt_cur=2;cnt_pre<tot;cnt_pre<<=1,cnt_cur<<=1)
for (i=;i<tot;i+=cnt_cur)
for (j=;j<cnt_pre;j++)
{
x=a[i+j];
a[i+j]=(a[i+j]+a[i+j+cnt_pre])*mod_inv_2%mod;
a[i+j+cnt_pre]=(x-a[i+j+cnt_pre]+mod)*mod_inv_2%mod; ///x-
}
} int main()
{
int n,i,j;
scanf("%d",&n);
tot=<<n;
for (i=;i<tot;i++)
scanf("%lld",&aa[i]);
for (i=;i<tot;i++)
scanf("%lld",&bb[i]); void (*addr1[]) (ll a[])={fwt1,fwt2,fwt3};
void (*addr2[]) (ll a[])={ufwt1,ufwt2,ufwt3}; for (i=;i<;i++)
{
memcpy(a,aa,sizeof(aa));
memcpy(b,bb,sizeof(bb));
(*addr1[i])(a);
(*addr1[i])(b);
for (j=;j<tot;j++)
a[j]=a[j]*b[j]%mod;
(*addr2[i])(a);
for (j=;j<tot;j++)
printf("%lld%c",a[j],j==tot-?'\n':' ');
}
return ;
}
/*
0
1000000
1000000 3
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
*/
and,or,xor等操作
C=A@B @为卷积运算(通过两个函数生成第三个函数的一种数学算子)
目标:tf(C)=tf(A)*tf(B)
设置tf(X)=(... , ...) 使满足上述条件
基础部分

公式推导






验证公式正确性



快速沃尔什变换(FWT) 与 快速莫比乌斯变换 与 快速沃尔什变换公式推导的更多相关文章
- 集合并卷积的三种求法(分治乘法,快速莫比乌斯变换(FMT),快速沃尔什变换(FWT))
也许更好的阅读体验 本文主要内容是对武汉市第二中学吕凯风同学的论文<集合幂级数的性质与应用及其快速算法>的理解 定义 集合幂级数 为了更方便的研究集合的卷积,引入集合幂级数的概念 集合幂级 ...
- 快速沃尔什变换&快速莫比乌斯变换小记
u1s1 距离省选只剩 5 days 了,现在学新算法真的合适吗(( 位运算卷积 众所周知,对于最普通的卷积 \(c_i=\sum\limits_{j+k=i}a_jb_k\),\(a_jb_k\) ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
- 一个数学不好的菜鸡的快速沃尔什变换(FWT)学习笔记
一个数学不好的菜鸡的快速沃尔什变换(FWT)学习笔记 曾经某个下午我以为我会了FWT,结果现在一丁点也想不起来了--看来"学"完新东西不经常做题不写博客,就白学了 = = 我没啥智 ...
- 快速沃尔什变换FWT
快速沃尔什变换\(FWT\) 是一种可以快速完成集合卷积的算法. 什么是集合卷积啊? 集合卷积就是在集合运算下的卷积.比如一般而言我们算的卷积都是\(C_i=\sum_{j+k=i}A_j*B_k\) ...
- BZOJ4589 Hard Nim(快速沃尔什变换FWT)
这是我第一道独立做出来的FWT的题目,所以写篇随笔纪念一下. (这还要纪念,我太弱了) 题目链接: BZOJ 题目大意:两人玩nim游戏(多堆石子,每次可以从其中一堆取任意多个,不能操作就输).$T$ ...
- 快速莫比乌斯变换(FMT)
快速莫比乌斯变换(FMT) 原文出处:虞大的博客.此仅作蒟蒻本人复习用~ 给定两个长度为n的序列 \(a_0, a_1, \cdots, a_{n-1}\)和\(b_0, b_1, \cdots, b ...
- 【学习笔鸡】快速沃尔什变换FWT
[学习笔鸡]快速沃尔什变换FWT OR的FWT 快速解决: \[ C[i]=\sum_{j|k=i} A[j]B[k] \] FWT使得我们 \[ FWT(C)=FWT(A)*FWT(B) \] 其中 ...
- 【learning】快速沃尔什变换FWT
问题描述 已知\(A(x)\)和\(B(x)\),\(C[i]=\sum\limits_{j\otimes k=i}A[j]*B[k]\),求\(C\) 其中\(\otimes\)是三种位运算的其中一 ...
随机推荐
- (转)RSA加密解密及数字签名Java实现
转:http://my.oschina.net/jiangli0502/blog/171263?fromerr=hc4izFe2 RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rives ...
- hdu多校第十场 1009 (hdu6699) Block Breaker bfs/模拟
题意: 紧密排列的方块因为摩擦力一个一个稳定地挤在一起,但当一个方块的四个邻居中,上下两个至少空缺一个,左右两个至少空缺一个,则这个方块也将掉落. 每次锤掉一个方块,求多少个方块受牵连落下. 题解: ...
- Python爬虫-《神雕侠侣》
Python3.5 爬取<神雕侠侣>http://www.kanunu8.com/wuxia/201102/1610.html 武侠迷,所以喜欢爬取武侠小说 #!/usr/bin/pyth ...
- pandas 使用dataframe 索引项相同时出现bug
使用的是join函数来合并两个dataframe: df=df2.join(df1) bug:columns overlap but no suffix specified: Index([u'muk ...
- Python中字典的详细用法
#字典 #字典是Python中唯一内建的映射类型.字典中没有特殊的顺序,但都是存储在一个特定的键(key)下面,键可以是数字,字符串,甚至是元组 #一.字典的使用 #在某些情况下,字典比列表更加适用: ...
- 为kubectl配置别名和命令行补齐
配置别名 # vim ~/.bashrc 添加 alias k='kubectl' # source ~/.bashrc 配置命令行补齐 # yum install -y bash-completio ...
- linux下使用自带mail发送邮件
linux下使用自带mail发送邮件 mailx工具说明: linux可以通过安装mailx工具,mailx是一个小型的邮件发送程序,一般可以通过该程序在linux系统上,进行监控linux系统状态并 ...
- try-with-resources with JDBC
I realize this was long ago answered but want to suggest an additional approach that avoids the nest ...
- linq语句,常用的查询,模糊查询,实体查询
查询: //List是要查询的实体列表的集合 List.FindAll(n => n.NAME == NAME), //NAME变量是要查询的条件 模糊查询 List.FindAll(s ...
- expect安装
expect是在tcl基础上创建起来的,因此在安装expect之前需要安装tcl 安装TCL下载地址:http://www.tcl.tk/software/tcltk/download.html[ro ...