2017广西邀请赛CS Course(&|^位运算模拟)

中石油提交网址:http://exam.upc.edu.cn/problem.php?id=4334

4334: CS Course

时间限制: 2 Sec  内存限制: 512 MB
提交: 37  解决: 13
[提交][状态][讨论版]

题目描述

Little A has come to college and majored in Computer and Science.
Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.
Here is the problem:
You are giving n non-negative integers a1,a2,...,an, and some queries.
A query only contains a positive integer p, which means you are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.

输入

There are no more than 15 test cases.
Each test case begins with two positive integers n(2 ≤ n ≤ 105) and p(2 ≤ p ≤ 105) in a line, indicate the number of positive integers and the number of queries.
Then n non-negative integers a1,a2,...,an follows in a line, 0 ≤ ai ≤ 109 for each i in range [1,n].
After that there are q positive integers p1, p2, ...,pq in q lines, 1 ≤ pi ≤ n for each i in range [1,q].

输出

For each query p, output three non-negative integers indicates the result of bit-operations(and, or,xor) of all non-negative integers except ap in a line.

样例输入

3 3
1 1 1
1
2
3

样例输出

1 1 0
1 1 0
1 1 0

【代码】:

#include <stdio.h>
#include <string.h>  
#include <algorithm> 
#define mset(a,i) memset(a,i,sizeof(a))
#define S1(n)     scanf("%d",&n)
#define S2(n,m)   scanf("%d%d",&n,&m)
using namespace std;
typedef long long ll;
const int MAX=1e6+5;
int a[MAX],b[MAX];
int n,m, And,Or,Xor;
int main()
{
	while(~S2(n,m))
	{
		mset(b,0);
		Xor=0,And=0xffffffff,Or=0;
		for(int i=1;i<=n;i++)
		{
			int x;S1(x);
			a[i]=x;
			And&=x;
			Or|=x;
			Xor^=x;
			for(int j=0;x;j++,x>>=1)
				b[j]+=x%2;
		}
		while(m--)
		{
			int q;S1(q);q=a[q];
			int A=And,O=Or,X=Xor;
			X=X^q;
			for(int j=0;j<=30;j++,q>>=1)
			{
				if(b[j]==n-1&&q%2==0)A+=(1<<j);
				if(b[j]==1  &&q%2   )O-=(1<<j);
			}
			printf("%d %d %d\n",A,O,X);
		}
	}
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/winter2121/article/details/77654506
个人分类: ACM**闲杂模拟**
所属专栏: ACM荣耀之路
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭