位运算

题目连接:https://ac.nowcoder.com/acm/problem/214618

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 int n, q;
 5 int a[100010];
 6 int x;
 7 int awy, awh, awyh;
 8 int cnt01[35];//统计每一位上1的个数
 9 int main()
10 {
11     cin>>t;
12     while(t--)
13     {
14         cin>>n>>q;
15         for(int i=1; i<=n; i++)
16         {
17             cin>>a[i];
18             for(int j=0; j<32; j++)//统计数组中每个数字在二进制位中1的个数
19                 if(a[i] & (1<<j))
20                     cnt01[j]++;
21         }
22         while(q--)
23         {
24             cin>>x;
25             awy=awh=awyh=0;
26             int by, bh, byh;//当前位上有效数字
27             for(int i=0; i<32; i++)
28             {
29                 int cc=((a[x] & (1<<i)) != 0);
30                 int c=cnt01[i]- cc;
31                 by=(c == n-1);//判读是否有0;
32                 awy=awy+by*(1<<i);
33                 bh=(c > 0);//判读是否有1;
34                 awh=awh+bh*(1<<i);
35                 byh=(c%2==1);//偶数个1该位为0,奇数个1该位为1
36                 awyh=awyh+byh*(1<<i);
37             }
38             cout<<awy<<" "<<awh<<" "<<awyh<<endl;
39         }
40     }
41     return 0;
42 }

 

posted @ 2022-03-24 11:39  TFLSNOI  阅读(13)  评论(0编辑  收藏  举报