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;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值