FWT模板

Posted rubbishes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FWT模板相关的知识,希望对你有一定的参考价值。

FWT 是求多项式位元算卷积的一种高效方法

最常见的有 or、and、xor 这三种操作

 

技术分享图片
void FWT(LL f[], int n, int op) {
    int mx = 0;
    while((1LL<<mx) < n) mx++;
    for (int i = 1; i <= mx; ++i) {
        int m = (1 << i), len = m >> 1;
        for (int r = 0; r < n; r += m) {
            int t1 = r, t2 = r + len;
            for (int j = 0; j < len; ++j, ++t1, ++t2) {
                LL x1 = f[t1], x2 = f[t2];
                if (op == 1) {   //xor
                    f[t1] = x1 + x2;
                    f[t2] = x1 - x2;
                    //if(f[t1] >= mod) f[t1] -= mod;
                    //if(f[t2] < 0) f[t2] += mod;
                }
                if (op == 2) {   //and
                    f[t1] = x1 + x2;
                    f[t2] = x2;
                    //if(f[t1] >= mod) f[t1] -= mod;
                }
                if (op == 3) {   //or
                    f[t1] = x1;
                    f[t2] = x2 + x1;
                    //if(f[t2] >= mod) f[t2] -= mod;
                }
            }
        }
    }
}
void IFWT(LL f[], int n, int op) {
    int mx = 0;
    while((1LL<<mx) < n) mx++;
    for (int i = mx; i >= 1; --i) {
        int m = (1 << i), len = m >> 1;
        for (int r = 0; r < n; r += m) {
            int t1 = r, t2 = r + len;
            for (int j = 0; j < len; ++j, ++t1, ++t2) {
                LL x1 = f[t1], x2 = f[t2];
                if (op == 1) {   //xor
                    f[t1] = (x1 + x2) / 2;
                    f[t2] = (x1 - x2) / 2;
                    //f[t1] = (x1 + x2) * inv2;
                    //f[t2] = (x1 - x2) * inv2;
                    //if(f[t1] >= mod) f[t1] %= mod;
                    //if(f[t2] >= mod) f[t2] %= mod;
                    //if(f[t2] < 0) f[t2] = f[t2] % mod + mod;
                }
                if (op == 2) {   //and
                    f[t1] = x1 - x2;
                    f[t2] = x2;
                    //if(f[t1] < 0) f[t1] += mod;
                }
                if (op == 3) {   //or
                    f[t1] = x1;
                    f[t2] = x2 - x1;
                    //if(f[t2] < 0) f[t2] += mod;
                }
            }
        }
    }
}
View Code

 

以上是关于FWT模板的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 - P4717 模板快速莫比乌斯/沃尔什变换 (FMT/FWT)

FWT模板

FWT模板

FWT模板

2023.4.7模板快速沃尔什变换FWT

Luogu4717 模板快速沃尔什变换(FWT)