Codeforces 1842B Tenzing and Books

文章描述了一个编程挑战,Tenzing收到了三叠书,每叠有n本书,每本书都有一个非负的难度评级。他从每一叠书的顶部开始读,每次读完一本书后,他的知识会通过位运算更新。目标是检查Tenzing是否可以通过读书使他的知识达到特定的值xx。输入包含多组测试用例,每组包括书的数量n,目标值xx以及三叠书的难度评级。程序需要判断是否可能通过读书达到目标值。
摘要由CSDN通过智能技术生成

题目描述:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Tenzing received 3n3n books from his fans. The books are arranged in 33 stacks with nn books in each stack. Each book has a non-negative integer difficulty rating.

Tenzing wants to read some (possibly zero) books. At first, his knowledge is 00.

To read the books, Tenzing will choose a non-empty stack, read the book on the top of the stack, and then discard the book. If Tenzing's knowledge is currently uu, then his knowledge will become u|vu|v after reading a book with difficulty rating vv. Here || denotes the bitwise OR operation. Note that Tenzing can stop reading books whenever he wants.

Tenzing's favourite number is xx. Can you help Tenzing check if it is possible for his knowledge to become xx?

Input

Each test contains multiple test cases. The first line of input contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and xx (1≤n≤1051≤n≤105, 0≤x≤1090≤x≤109) — the number of books in each stack and Tenzing's favourite number.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109)  — the difficulty rating of the books in the first stack, from top to bottom.

The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (0≤bi≤1090≤bi≤109)  — the difficulty rating of the books in the second stack, from top to bottom.

The fourth line of each test case contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤1090≤ci≤109)  — the difficulty rating of the books in the third stack, from top to bottom.

It is guaranteed that the sum of nn does not exceed 105105.

Output

For each test case, output "Yes" (without quotes) if Tenzing can make his knowledge equal to xx, and "No" (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

input

3
5 7
1 2 3 4 5
5 4 3 2 1
1 3 5 7 9
5 2
3 2 3 4 5
5 4 3 2 1
3 3 5 7 9
3 0
1 2 3
3 2 1
2 2 2

output

Yes
No
Yes

题目大意:给你三个长度为n的数组,每次只能从数组的最顶部取一个数,问是否能从0开始进行或运算得到x

思路分析:与x进行或运算结果仍然为x的可以被使用

代码:

#include<iostream>
using namespace std;
int a[100005],b[100005],c[100005];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		long long int x,ans=0;
		cin>>n>>x;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&b[i]);
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&c[i]);
		}
		if(ans==x)
		{
			cout<<"Yes\n";
			continue;
		}
		for(int i=1;i<=n;i++){
			if((x|a[i])==x){
				ans=ans|a[i];
			}
			else
			break;
		}
		if(ans==x)
		{
			cout<<"Yes\n";
			continue;
		}
		for(int i=1;i<=n;i++){
			if((x|b[i])==x){
				ans=ans|b[i];
			}
			else
			break;
		}
		if(ans==x)
		{
			cout<<"Yes\n";
			continue;
		}
		for(int i=1;i<=n;i++){
			if((x|c[i])==x){
				ans=ans|c[i];
			}
			else
			break;
		}
		if(ans==x)
		{
			cout<<"Yes\n";
			continue;
		}
		else
		cout<<"No\n";
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值