问题 E: CS Course
时间限制: 2 Sec 内存限制: 512 MB题目描述
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
题目大意:
n个数 q次询问 每次询问第pi个数字 求除去询问的这个数外其他全部数字的and、or、xor
分析:
分别求出每个二进制位的有多少个0 然后判断去除询问的数字外剩下的每个二进制位的and、or、转为十进制
关于xor 最后在xor一遍这个数即可
AC代码:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int a[1000005];
int count0[1000005];
int main (){
int n,p;
while (scanf ("%d%d",&n,&p)!=EOF){
memset(a,0,sizeof(a));
memset(count0,0,sizeof(count0));
int ans3=0;
int maxn=0;
for (int i=0;i<n;i++){
scanf ("%d",&a[i]);
ans3^=a[i];
int tt=a[i];
int len=0;
while (tt){
if(tt%2) count0[len]++;
tt>>=1;len++;
if (maxn<len) maxn=len;
}
}
for (int i=0;i<maxn;i++) count0[i]=n-count0[i];
int temp;
for (int i=0;i<p;i++){
scanf ("%d",&temp);
int t=a[temp-1];
int ans1=0,ans2=0;
for (int j=0;j<maxn;j++){
if (count0[j]==0||(count0[j]==1&&t%2==0)) ans1+=1<<j,ans2+=1<<j;
else if ((count0[j]==n-1&&t%2==0&&n!=2)||
(count0[j]==1&&t%2!=0&&n!=2)||
(count0[j]>1&&count0[j]<n-1))
ans2+=1<<j;
t>>=1;
}
printf ("%d %d %d\n",ans1,ans2,ans3^a[temp-1]);
}
}
return 0;
}