E: CS Course
时间限制: 2 Sec 内存限制: 512 MB提交: 79 解决: 21
[ 提交][ 状态][ 讨论版]
题目描述
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.
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].
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
提示
亦或 可以直接 在亦或第x 个值 得数,
与和 或 要 转换成 二进制, 用数组 统计 数
对于& 而言, 如果 1的数目= n-1 并且 x%2 =0 则 结果为1;
对于| 而言, 只要存在1, 结果为1
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define S1(n) scanf("%d",&n)
#define SL1(n) scanf("%I64d",&n)
#define S2(n,m) scanf("%d%d",&n,&m)
#define SL2(n,m) scanf("%I64d%I64d",&n,&m)
#define Pr(n) printf("%d\n",n)
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
int n,m;
int a[maxn];
int b[maxn];
int main()
{
while(~scanf("%d %d",&n,&m))
{
mem(b,0);
mem(a,0);
int AND=INF,OR=0,XOR=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
AND&=a[i];
OR|=a[i];
XOR^=a[i];
int t=a[i];
int k=0;
while(t)
{
b[k++]+=t%2;
t/=2;
}
}
while(m--)
{
int x;
scanf("%d",&x);
x=a[x];
int XO=XOR^x;
int A=AND,O=OR;
for(int j=0;j<32;j++,x>>=1)
{
if(b[j]==n-1 && x%2==0) A+=(1<<j);
if(b[j]==1&& x%2) O-=(1<<j);
}
printf("%d %d %d\n",A,O,XO);
}
}
return 0;
}
13