Divan and bitwise operations(组合数+思维)

Divan and bitwise operations

[Link](Problem - C - Codeforces)

题意

你有一个长度为 n n n的数组,但是不知道每个数是什么,接下来给你 m m m个区间,告诉你每个区间的数的或是多少,问你这个数组的每个子序列里的元素相异或的和为多少。

思路

发现我们可以将区间排序,类比于区间合并的思想,给每个位置一个恰好合适的值,然后我们知道了每一个数是什么,怎么来算子序列的异或和呢,直接枚举显然很不现实,因此我们按位枚举算贡献,先统计出来二进制每一位有多少个 1 1 1,假设第 i i i位有 k k k 1 1 1那么就有 n − k n-k nk 0 0 0,对于这一位要有贡献就要选奇数个 1 1 1,即 ( 1 < < i ) × ( C k 1 + C k 3 + . . . + C k k ( 偶 数 减 1 ) ) × 2 n − k (1<<i)\times(C_k^1+C_k^3+...+C_k^{k(偶数减1)})\times2^{n-k} (1<<i)×(Ck1+Ck3+...+Ckk(1))×2nk,枚举每一位算贡献即可。

知道 C n 0 + C n 2 + . . . = = C n 1 + C n 3 . . . = = 2 n − 1 C_n^0+C_n^2+...==C_n^1+C_n^3...==2^{n-1} Cn0+Cn2+...==Cn1+Cn3...==2n1,因为 ( 1 + 1 ) n = C n 0 + C n 1 + . . . + C n n , ( 1 − 1 ) n = C n 0 − C n 1 + . . . + ( − 1 ) n C n n = 0 (1+1)^n=C_n^0+C_n^1+...+C_n^n,(1-1)^n=C_n^0-C_n^1+...+(-1)^nC_n^n=0 (1+1)n=Cn0+Cn1+...+Cnn,(11)n=Cn0Cn1+...+(1)nCnn=0

所以贡献即 ( 1 < < i ) × 2 k − 1 × 2 n − k = ( 1 < < i ) × 2 n − 1 (1<<i)\times 2^{k-1}\times 2^{n-k}=(1<<i)\times 2^{n-1} (1<<i)×2k1×2nk=(1<<i)×2n1,发现每一位是否产生贡献只与这一位是否有 1 1 1有关,因此直接将输入或一下,按位枚举即可,并不需要知道原来的数是什么。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
	e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
struct Node {
	int l, r;
	int x;
	bool operator <(Node t)const {
		if (l != t.l) return l < t.l;
		return r < t.r;
	}
}a[N];
int cnt[31];
LL b[N];
LL qmi(LL a, LL b) {
	LL res = 1;
	while (b) {
		if (b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	cin >> T;
	while (T -- ) {
		cin >> n >> m;
		int res = 0;
		for (int i = 1; i <= m; i ++) {
			int x; cin >> x >> x >> x;
			res |= x;
		}
		
		LL sum = 0;
		for (int i = 0; i < 30; i ++) 
			if (res >> i & 1) 
				sum = (sum + (1ll << i) * qmi(2, n - 1) % mod) % mod;
		
		cout << sum << endl;
	
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值