zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    c++3种内存管理方式
    什么是向上兼容和向下兼容?
    回溯法解马的遍历问题
    c++内联函数
    2009年NCRE考试有新变化
    sql server日期时间函数
    Web开发工具大集合
    javascript屏幕高度和宽度等信息代码
    gridview无数据行时显示表头的方法
    IE, FF, Safari前端开发常用调试工具
  • 原文地址:https://www.cnblogs.com/qwertiLH/p/9505052.html
Copyright © 2011-2022 走看看