CS Course
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
Input There are no more than 15 test cases.
Each test case begins with two positive integers n and p
in a line, indicate the number of positive integers and the number of queries.
2≤n,q≤105
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,⋯,pqin q lines, 1≤pi≤n for each i in range[1,q].
Output 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.
Sample Input 3 31 1 1123
Sample Output 1 1 01 1 01 1 0
/*
* @Author: Administrator
* @Date: 2017-08-31 17:13:07
* @Last Modified by: Administrator
* @Last Modified time: 2017-08-31 17:45:05
*/
/*
题意:给你n个数,然后m次查询,每次给你一个q ,问你除去a[q]之后所有数的 and or xor
思路:xor可以直接得出,and 和 or 只需要判断一下每位1的个数
感悟:上学期我以后学分选完了,结果一看XK 的都没选,这学期课挺多的...今下午没打比赛
在教室想了一下1005和1004下了课就过来实现了,没想到1A了,看来我还是适合紧张的学习
环境...
*/
#include <bits/stdc++.h>
#define MAXN 100005
#define LL long long
using namespace std;
int n,m;
LL a[MAXN];
int q;
int vis[33];//统计每位的1的个数
LL res1,res2,res3;//未去除的最后结果
LL pos;//指针
LL cur1,cur2,cur3;//最后结果
LL cnt;
int cnt1[33],cnt2[33];//最后结果的二进制,xor操作可O(1)算出来所以不用保存
void cal(LL x){//统计每位上1的个数
for(int i=0;i<32;i++){
if(x%2==1){
vis[i]++;
}
x/=2;
}
}
void po(LL x,int sw){
for(int i=0;i<32;i++){
switch(sw){
case 1:
cnt1[i]=x%2;
break;
case 2:
cnt2[i]=x%2;
break;
}
x/=2;
}
}
inline void init(){
memset(vis,0,sizeof vis);
cur1=0;
cur2=0;
cur3=0;
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
cal(a[i]);
if(i==0){
res1=a[i],res2=a[i],res3=a[i];
}else{
res1&=a[i];
res2|=a[i];
res3^=a[i];
}
}
//将结果处理成二进制
po(res1,1);po(res2,2);
for(int i=0;i<m;i++){
cur1=0;
cur2=0;
cur3=0;
scanf("%d",&q);
//处理and操作
pos=a[q-1];
cnt=1;
for(int i=0;i<32;i++){
if(pos%2==0){
if(vis[i]==n-1){
cur1+=cnt;
}
}else{
if(cnt1[i]==1){
cur1+=cnt;
}
}
pos/=2;
cnt*=2;
}
//处理or操作
pos=a[q-1];
cnt=1;
for(int i=0;i<32;i++){
if(pos%2==1){
if(vis[i]!=1){
cur2+=cnt;
}
}else{
if(cnt2[i]==1){
cur2+=cnt;
}
}
pos/=2;
cnt*=2;
}
//处理xor操作
cur3=res3^a[q-1];
printf("%lld %lld %lld\n",cur1,cur2,cur3);
}
}
return 0;
}