码迷,mamicode.com
首页 > 其他好文 > 详细

FWT模板

时间:2018-08-20 13:44:48      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:oid   nbsp   hid   技术分享   display   图片   分享   pen   lap   

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模板

标签:oid   nbsp   hid   技术分享   display   图片   分享   pen   lap   

原文地址:https://www.cnblogs.com/Rubbishes/p/9505052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!