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

1141人阅读 评论(0)

[提交][状态][讨论版]

题目描述

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);
}
}
}

0
0

个人资料
• 访问：103697次
• 积分：3019
• 等级：
• 排名：第13770名
• 原创：197篇
• 转载：9篇
• 译文：0篇
• 评论：33条
博客专栏
 ACM算法 文章：17篇 阅读：7004
 ACM荣耀之路 文章：97篇 阅读：46059
阅读排行
最新评论