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

计蒜客16495 Truefriend(fwt)

时间:2017-08-16 19:21:49      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:eof   while   div   cstring   --   技术分享   long   using   9.png   

技术分享

#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;
    }
}

 

计蒜客16495 Truefriend(fwt)

标签:eof   while   div   cstring   --   技术分享   long   using   9.png   

原文地址:http://www.cnblogs.com/Saurus/p/7374863.html

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