Codeforces Round #757 (Div. 2) - C. Divan and bitwise operations

#757(Div.2) - C. Divan and bitwise operations

比赛链接:Codeforces Round #757 (Div. 2)

题目链接:C. Divan and bitwise operations

官方题解:https://codeforces.com/blog/entry/97283

贴一位大佬的题解:知乎 - pzr - Codeforces Round #757 (Div. 2) A~D1

题意:

已知一组序列某子序列的或,求其所有子序列的异或的和

题解:

子序列的个数为2^n,(n为序列长度,即序列中每个数有0或1两种情况:选或不选)

按位(二进制)来看,假设所有数的只要第i位上有1,那么一定是一半的子序列的异或和第i位上都有1:

  1. 假设只有一个数的第i位上有1,那么只有0,1两种情况(其余的数第i位上为0,异或得数不变)
  2. 假设有两个数的第i位上有1,那么是 0&0,0&1,1&0,1&1这四种情况,最后也是0,1均分的两种情况。
  3. 同理....

即:子序列的异或的和 = 所有数的每一位上的 或 乘以子序列的半数

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 5e6+10;
int num[N], x[N];
const int mod = 1e9+7;

ll quickpow(ll x, ll n){
    ll ans = 1;
    while(n){
        if(n&1) ans = ans*x %mod;
        x = x*x % mod;
        n >>=1;
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t;
    cin>>t;
    while(t--){
        ll n, m, x;
        cin>>n>>m;
        int ans = 0;
        for(int i = 1; i <= m; i++) cin>>x>>x>>x, ans |= x;
        cout<< ans * quickpow(2,n-1) %mod<<endl;
    }

    return 0;
}
posted @ 2021-11-28 01:01  gverzh  阅读(102)  评论(0)    收藏  举报