计蒜客16495 Truefriend(fwt)

本文详细介绍了快速沃尔特变换(FWT)的实现方法,并通过一个具体的C++代码示例展示了如何利用FWT进行数据处理。重点讲解了FWT及其逆变换的计算过程,适用于解决特定类型的数据处理问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
class FWT{
public:
    void fwt(LL *a, int n){
        for(int d = 1; d < n; d <<= 1){
            for(int m = d<<1, i = 0; i < n; i += m){
                for(int j = 0; j < d; j++){
                    LL x = a[i+j], y = a[i+j+d];
                    a[i+j] = x+y; a[i+j+d] = x-y;
                }
            }
        }
    }
    void ufwt(LL *a, int n){
        for(int d = 1; d < n; d <<= 1){
            for(int m = d<<1, i = 0; i < n; i += m){
                for(int j = 0; j < d; j++){
                    LL x = a[i+j], y = a[i+j+d];
                    a[i+j] = (x+y)/2; a[i+j+d] = (x-y)/2;
                }
            }
        }
    }
    void work(LL *a, LL *b, int n){
        fwt(a, n);
        fwt(b, n);
        for(int i = 0; i < n; i++) a[i] *= b[i];
        ufwt(a, n);
    }
}myfwt;
LL a[maxn*8], b[maxn*8];
int T, n, k, v1, v2;
int main(){
    cin>>T;
    while(T--){
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        cin>>n>>k;
        cin>>v1>>v2;
        int L = 1;
        while(L <= (n+1)*2) L <<= 1;
        for(int i = 1; i <= n; i++){
            a[i&v1]++;
            b[i&v2]++;
        }
        myfwt.work(a, b, L);
        LL ans = 0;
        for(int i = k; i <= L; i++){
            ans += a[i];
        }
        cout<<ans<<endl;
    }
}

 

转载于:https://www.cnblogs.com/Saurus/p/7374863.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值