Divan and bitwise operations(组合数+思维)
Divan and bitwise operations
[Link](Problem - C - Codeforces)
题意
你有一个长度为nnn的数组,但是不知道每个数是什么,接下来给你mmm个区间,告诉你每个区间的数的或是多少,问你这个数组的每个子序列里的元素相异或的和为多少。
思路
发现我们可以将区间排序,类比于区间合并的思想,给每个位置一个恰好合适的值,然后我们知道了每一个数是什么,怎么来算子序列的异或和呢,直接枚举显然很不现实,因此我们按位枚举算贡献,先统计出来二进制每一位有多少个111,假设第iii位有kkk个111那么就有n−kn-kn−k个000,对于这一位要有贡献就要选奇数个111,即(1<<i)×(Ck1+Ck3+...+Ckk(偶数减1))×2n−k(1<<i)\times(C_k^1+C_k^3+...+C_k^{k(偶数减1)})\times2^{n-k}(1<<i)×(Ck1+Ck3+...+Ckk(偶数减1))×2n−k,枚举每一位算贡献即可。
知道Cn0+Cn2+...==Cn1+Cn3...==2n−1C_n^0+C_n^2+...==C_n^1+C_n^3...==2^{n-1}Cn0+Cn2+...==Cn1+Cn3...==2n−1,因为(1+1)n=Cn0+Cn1+...+Cnn,(1−1)n=Cn0−Cn1+...+(−1)nCnn=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,(1−1)n=Cn0−Cn1+...+(−1)nCnn=0。
所以贡献即(1<<i)×2k−1×2n−k=(1<<i)×2n−1(1<<i)\times 2^{k-1}\times 2^{n-k}=(1<<i)\times 2^{n-1}(1<<i)×2k−1×2n−k=(1<<i)×2n−1,发现每一位是否产生贡献只与这一位是否有111有关,因此直接将输入或一下,按位枚举即可,并不需要知道原来的数是什么。
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;
}
Divan and bitwise operations(组合数+思维)相关推荐
- C. Divan and bitwise operations
Problem - C - Codeforces C. Divan and bitwise operations time limit per test 1 second memory limit p ...
- Divan and bitwise operations 异或,同或,组合数学(1500)
题意 : 原序列长为n,给m个连续子序列的左右端点的下标以及该连续子序列的或和,保证这m个连续子序列的并集包含原序列每一个点,求原序列的所有子序列的异或的和,输出任意一种 思路 : 由于位运算对每个位 ...
- 在php中将5按位与运算,PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3)...
PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3) 我将省略有关我是如何做到这一点 ...
- codeforces 1395C Boboniu and Bit Operations(思维)
题目 题意:给出一个长度为n的数组a,和一个长度为m的数组,要求得到一个长度为n的数组c,使得数组c里面的每个元素执行"或"操作,得到的数组最小,其中c中的每一个元素为a中的每个元 ...
- 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)A. 小乐乐的组合数+(思维)
题目链接:https://ac.nowcoder.com/acm/contest/301/A 思路是我们用i从1-n依次枚举i+1,i+2,i+3...i+m,直接计算其中有多少个7的倍 ...
- Codeforces Round #757 (Div. 2)
A. Divan and a Store B. Divan and a New Project C. Divan and bitwise operations D1. Divan and Kostom ...
- C语言位运算 Bitwise Operator
Bitwise Operators in C Programming Language When I first learn C, I found it's hard to understand th ...
- python矩阵运算库效率_python - 布尔矩阵运算的最快方法_performance_酷徒编程知识库...
只需在compute中进行一些小的更改:def compute(m, n): m = np.asarray(m) n = np.asarray(n) # Apply mask N in advance ...
- Android系统中提供的原子操作
代码的实现位于文件system/core/include/cutils中 http://androidxref.com/4.4.3_r1.1/xref/system/core/include/cuti ...
最新文章
- CheckM——国家微生物科学数据中心云工具
- 弹性均质圆环法计算过程_蚝油的加工工艺,蚝油总固形物(水分含量)计算公式,检测方法...
- 爬取58二手数据.py
- C#指南,重温基础,展望远方!(4)表达式
- mono for android mysql_mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习...
- b站在线解析_一个小工具,教你如何轻松下载B站上喜欢的视频!
- java伪唤醒,谈谈JDK线程的伪唤醒
- Java成员的访问权限控制
- android组件化数据生命周期,Android组件化开发实践(七):开发常见问题及解决方案...
- 使用Pytorch来拟合函数
- 我把视频变成链接_H5中加视频?这才是正确姿势
- od反编译c语言插件,Ollydbg反汇编工具
- 酒店抖音小程序开发方案
- vissim交通仿真实验(1)--仿真基础
- Juniper SRX密码恢复
- 拍视频到底用手机还是相机好?
- 那些一出口就很Low的话
- 64位电脑安装32位系统不能引导启动
- Windows技巧:右键文件打开方式,该文件没有与之关联来执行该操作
- 2015年中国数据库技术大会(DTCC)PPT合集