当前位置:网站首页>【集训DAY5】选数字【数学】

【集训DAY5】选数字【数学】

2022-08-09 22:35:00 VL——MOESR

在这里插入图片描述

思路:

预处理出&i==ai的所有数,然后容斥判断就行了

c o d e code code

#include<iostream>
#include<cstdio>


using namespace std;

const int MAXN = 1e5 + 10;

int n, q;
int sum[MAXN][256], a[MAXN], p[256];

long long jisuan(long long x) {
    
	return (x * (x - 1) * (x - 2) / 6);
}

int main() {
    
	p[0] = 0, p[1] = 1, p[2] = 1;
	for(int i = 3; i <= 255; i ++) p[i] = p[i / 2] + (i & 1);
	scanf("%d%d", &n, &q);
	for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
	for(int i = 1; i <= n; i ++)
		for(int j = 0; j <= 255; j ++) {
    
			sum[i][j] = sum[i - 1][j] + ((a[i] & j) == a[i]);
		}
	while(q --) {
    
		int l, r, x;
		long long ans = 0;
		scanf("%d%d%d", &l, &r, &x);
		for(int i = 0; i <= 255; i ++) {
    
			if((i & x) != i) continue;
			int g = p[x] - p[i];
			if(g & 1) ans -= jisuan(sum[r][i] - sum[l - 1][i]);
			else ans += jisuan(sum[r][i] - sum[l - 1][i]);
		}
		printf("%lld\n", ans);
		ans = 0;
	}
	return 0;
}
原网站

版权声明
本文为[VL——MOESR]所创,转载请带上原文链接,感谢
https://blog.csdn.net/liuziha/article/details/126253842