#757 (Div. 2) C. Divan and bitwise operations(组合数字+位运算)

题目描述

Once Divan analyzed a sequence a1,a2,…,an consisting of n non-negative integers as follows. He considered each non-empty subsequence of the sequence a, computed the bitwise XOR of its elements and added up all the XORs, obtaining the coziness of the sequence a.
A sequence c is a subsequence of a sequence d if c can be obtained from d by deletion of several (possibly, zero or all) elements. For example, [1,2,3,4], [2,4], and [2] are subsequences of [1,2,3,4], but [4,3] and [0] are not.
Divan was very proud of his analysis, but now he lost the sequence a, and also the coziness value! However, Divan remembers the value of bitwise OR on m contiguous subsegments of the sequence a. It turns out that each element of the original sequence is contained in at least one of these m segments.
Divan asks you to help find the coziness of the sequence a using the information he remembers. If several coziness values are possible, print any.
As the result can be very large, print the value modulo 109+7.

Input

The first line contains one integer number t (1≤t≤103) — the number of test cases.
The first line of each test case contains two integer numbers n and m (1≤n,m≤2⋅105) — the length of the sequence and the number of contiguous segments whose bitwise OR values Divan remembers, respectively.
The following m lines describe the segments, one per line.
Each segment is described with three integers l, r, and x (1≤l≤r≤n, 0≤x≤230−1) — the first and last elements of the segment and the bitwise OR of al,al+1,…,ar, respectively.
It is guaranteed that each element of the sequence is contained in at least one of the segments. It is guaranteed that there exists a sequence that satisfies all constraints.
It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2⋅105.

Output

For each test case print the coziness any suitable sequence a modulo 109+7.

Example

input
3
2 1
1 2 2
3 2
1 3 5
2 3 5
5 4
1 2 7
3 3 7
4 4 0
4 5 2
output
4
20
112

Note

In first example, one of the sequences that fits the constraints is [0,2]. Consider all its non-empty subsequences:
[0]: the bitwise XOR of this subsequence is 0;
[2]: the bitwise XOR of this subsequence is 2;
[0,2]: the bitwise XOR of this subsequence is 2.
The sum of all results is 4, so it is the answer.
In second example, one of the sequences that fits the constraints is [0,5,5].
In third example, one of the sequences that fits the constraints is [5,6,7,0,2].

题目分析

通 过 观 察 我 们 可 以 发 现 : 这 n 个 数 中 只 要 存 在 一 个 数 , 第 i 位 上 是 1 。 这 一 位 得 到 的 贡 献 即 为 2 i ∗ 2 n − 1 ( 这 n 个 数 通过观察我们可以发现:这n个数中只要存在一个数,第i位上是1。这一位得到的贡献即为2^i*2^{n-1}(这n个数 ni12i2n1n 中 只 要 第 i 位 上 有 1 , 就 能 产 生 这 些 贡 献 , 与 个 数 无 关 ) 中只要第i位上有1,就能产生这些贡献,与个数无关) i1

只 要 发 现 了 这 个 结 论 , 剩 下 的 就 很 好 解 决 了 。 我 们 可 以 记 录 下 m 个 区 间 或 和 的 或 和 。 这 样 就 能 够 得 到 这 个 序 列 中 只要发现了这个结论,剩下的就很好解决了。我们可以记录下m个区间或和的或和。这样就能够得到这个序列中 m 的 所 有 数 能 够 覆 盖 到 那 些 位 , 最 后 再 乘 上 2 n − 1 即 为 答 案 。 的所有数能够覆盖到那些位,最后再乘上2^{n-1}即为答案。 2n1

补 充 : 因 为 题 目 要 求 的 是 任 意 一 个 合 法 序 列 的 解 , 因 此 对 于 那 些 没 有 被 这 m 个 区 间 覆 盖 的 部 分 , 我 们 直 接 取 0 即 可 补充:因为题目要求的是任意一个合法序列的解,因此对于那些没有被这m个区间覆盖的部分,我们直接取0即可 m0

最 后 我 们 来 证 明 一 下 我 们 “ 通 过 观 察 得 到 的 ” 结 论 : 最后我们来证明一下我们 “通过观察得到的” 结论:
假 设 对 于 第 i 个 位 置 , 有 n u m 个 位 置 上 是 1 , n − n u m 个 位 置 上 是 0 。 假设对于第i个位置,有num个位置上是1,n-num个位置上是0。 inum1nnum0

那 么 第 i 位 可 以 产 生 的 贡 献 即 为 : 那么第i位可以产生的贡献即为: i
2 i ∗ 2 n − n u m ∗ ( C n u m 1 + C n u m 3 + … … ) 2^i*2^{n-num}*(C^{1}_{num}+C^{3}_{num}+……) 2i2nnum(Cnum1+Cnum3+)
= 2 i ∗ 2 n − n u m ∗ 2 n u m − 1 =2^i*2^{n-num}*2^{num-1} =2i2nnum2num1
= 2 i ∗ 2 n − 1 =2^i*2^{n-1} =2i2n1

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=2e5+5,mod=1e9+7;
int kmi(int a,int k)			//快速幂
{
	int res=1;
	while(k)
	{
		if(k&1) res=(LL)res*a%mod;
		a=(LL)a*a%mod;
		k>>=1;
	}
	return res;
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int ans=0;				//记录m个区间或和的或和,从而求出序列中每一位的贡献
		for(int i=1;i<=m;i++)
		{
			int l,r,x;
			cin>>l>>r>>x;
			ans|=x;
		}
		cout<<(LL)ans*kmi(2,n-1)%mod<<endl;		//输出答案
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值