Problem - C - Codeforces

C. Divan and bitwise operations

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Once Divan analyzed a sequence a1,a2,…,ana1,a2,…,an consisting of nn non-negative integers as follows. He considered each non-empty subsequence of the sequence aa, computed the bitwise XOR of its elements and added up all the XORs, obtaining the coziness of the sequence aa.

A sequence cc is a subsequence of a sequence dd if cc can be obtained from dd by deletion of several (possibly, zero or all) elements. For example, [1,2,3,4][1,2,3,4], [2,4][2,4], and [2][2] are subsequences of [1,2,3,4][1,2,3,4], but [4,3][4,3] and [0][0] are not.

Divan was very proud of his analysis, but now he lost the sequence aa, and also the coziness value! However, Divan remembers the value of bitwise OR on mm contiguous subsegments of the sequence aa. It turns out that each element of the original sequence is contained in at least one of these mm segments.

Divan asks you to help find the coziness of the sequence aa 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+7109+7.

Input

The first line contains one integer number tt (1≤t≤1031≤t≤103) — the number of test cases.

The first line of each test case contains two integer numbers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105) — the length of the sequence and the number of contiguous segments whose bitwise OR values Divan remembers, respectively.

The following mm lines describe the segments, one per line.

Each segment is described with three integers ll, rr, and xx (1≤l≤r≤n1≤l≤r≤n, 0≤x≤230−10≤x≤230−1) — the first and last elements of the segment and the bitwise OR of al,al+1,…,aral,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 nn and the sum of mm over all test cases do not exceed 2⋅1052⋅105.

Output

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

Example

input

Copy

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

Copy

4
20
112

Note

In first example, one of the sequences that fits the constraints is [0,2][0,2]. Consider all its non-empty subsequences:

  • [0][0]: the bitwise XOR of this subsequence is 00;
  • [2][2]: the bitwise XOR of this subsequence is 22;
  • [0,2][0,2]: the bitwise XOR of this subsequence is 22.

The sum of all results is 44, so it is the answer.

In second example, one of the sequences that fits the constraints is [0,5,5][0,5,5].

In third example, one of the sequences that fits the constraints is [5,6,7,0,2][5,6,7,0,2].

=========================================================================

我们把各区间异或和给|起来,虽然会有交叉,但|不会算重复,只会取一个并集,并集也就是a1^a2.....an

这样我们就构造了一个带求解数列的异或和,还是考虑每一个位置,因为要求每一个子段的异或和,不妨对n个数的每个二进制位单独考虑.

第p位  二进制大小为  2^(p-1)  设有k个1,n-k个0, 我们想产生 2^(p-1)的贡献,就必须选择奇数个1,

若k为奇数,选法为 2^(k-1)种,  n为偶数也是2^(k-1)种,0的话,选几个都行  2^(n-k)种,共计

2^(n-1)种

当然这是在p位置至少有1个1的情况下的结论,我们异或出来伪原数列这一位如果是1,那么n个数里面一定至少一个1,如果是0,可以有很多1,但是我们把它当成没有1即可。

那么答案就是求出来伪原数列,求出不全是0的二进制位即可。

# include <iostream>
# include<algorithm>
# include<cstring>
# include<vector>
# include<queue>
# define mod 1000000007using namespace std;
typedef long long int ll;ll qp(ll base, ll pow)
{ll ans=1;while(pow){if(pow&1)ans=ans*base%mod;pow>>=1;base=base*base%mod;}return ans;
}int main()
{ll t;cin>>t;while(t--){ll n,k;cin>>n>>k;ll sum=0;for(int i=1;i<=k;i++){ll l,r,x;cin>>l>>r>>x;sum|=x;}ll ans=0,temp=qp(2ll,n-1);for(int i=0;i<=30;i++){if((sum&(1ll<<i))){ans+=(1ll<<i)*temp;ans%=mod;}}cout<<ans<<endl;}return 0;
}

C. Divan and bitwise operations相关推荐

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

    Divan and bitwise operations [Link](Problem - C - Codeforces) 题意 你有一个长度为nnn的数组,但是不知道每个数是什么,接下来给你mmm个 ...

  2. Divan and bitwise operations 异或,同或,组合数学(1500)

    题意 : 原序列长为n,给m个连续子序列的左右端点的下标以及该连续子序列的或和,保证这m个连续子序列的并集包含原序列每一个点,求原序列的所有子序列的异或的和,输出任意一种 思路 : 由于位运算对每个位 ...

  3. 在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) 我将省略有关我是如何做到这一点 ...

  4. 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 ...

  5. C语言位运算 Bitwise Operator

    Bitwise Operators in C Programming Language When I first learn C, I found it's hard to understand th ...

  6. python矩阵运算库效率_python - 布尔矩阵运算的最快方法_performance_酷徒编程知识库...

    只需在compute中进行一些小的更改:def compute(m, n): m = np.asarray(m) n = np.asarray(n) # Apply mask N in advance ...

  7. Android系统中提供的原子操作

    代码的实现位于文件system/core/include/cutils中 http://androidxref.com/4.4.3_r1.1/xref/system/core/include/cuti ...

  8. Des和Base64的Util

    2019独角兽企业重金招聘Python工程师标准>>> DES: package com.chinacreator.videoalliance.util;import java.ne ...

  9. 坚持完成这套学习手册,你就可以去 Google 面试了

    from: http://geek.csdn.net/news/detail/107064 本文为掘金投稿,译文出自:掘金翻译计划 原文地址:Google Interview University 原 ...

最新文章

  1. 动态执行流程分析和性能瓶颈分析的利器——gperftools的Cpu Profiler
  2. 国际顶刊《PNAS》:爱发朋友圈的人,更容易长寿
  3. pytho基础之文件处理
  4. Oracle业务适合用PostgreSQL去O的一些评判标准
  5. 配置Bean的作用域对象
  6. 特殊的求和(函数和循环)
  7. 分表后需要注意的二三事
  8. 计算机软件基础 一课本,计算机软件基础(-)课后习题答案.doc
  9. Python学习 Day4-1 Python3 条件控制、循环语句
  10. 我们的高中教育造就了什么?
  11. mysql 分段执行_了解一下项目中常用的19条MySQL优化
  12. 神奇的中医!神奇的文化!
  13. keil_4/MDK各种数据类型占用的字节数
  14. 所有权链(Ownership Chain)
  15. C# 使用Log4Net记录程序日志
  16. python if elif else_Python 基础起步 (九) 条件语句 if elif else 其实很简单
  17. 安装SQL Server 2012 报错“启用windows功能NetFx3时出错”解决方法
  18. 软件_可用rtmp直播地址20200517
  19. matlab 太阳系仿真,三维仿真太阳系
  20. matlab系统频域分析,基于MATLAB的系统频域分析的实现

热门文章

  1. 用状态机做人物(AI)的状态切换以及动画效果——人物动画以及切换
  2. HTTP协议分析实验
  3. ARP 地址解析协议 IP地址到MAC地址的转换过程
  4. 大促活动如何抵御大流量 DDoS 攻击?
  5. 常见思维模型汇总(二)
  6. codevs 2806 红与黑
  7. 汉信码在iOS客户端中的应用和遇到的坑
  8. 799 - 背包问题VIII - LintCode
  9. git gc error: failed to run repack message
  10. Unity统计代码行数脚本