CS Course HDU - 6186

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 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 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. 

2n,q105 2≤n,q≤105 

Then n non-negative integers  a1,a2,,an a1,a2,⋯,an follows in a line,  0ai109 0≤ai≤109 for each i in range[1,n]. 

After that there are q positive integers  p1,p2,,pq p1,p2,⋯,pqin q lines,  1pin 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 ap in a line. 
Sample Input
3 3
1 1 1
1
2
3
Sample Output
1 1 0
1 1 0

1 1 0

这道题其实只要维护一下前后缀和就好了啊。。那个不要就把他的前缀和后缀&。。比赛的时候没想到。

自己想到的是把所有数的二进制每一位的和存在数组里,然后每次要去掉哪个数,就把他的贡献减去。

这时看每位的和,如果是n-1,&的结果才能为1.(因为&要求两个数都为1结果才为1 ),或是有一个1即可。异或比较简单只要在异或一遍那个数即可。

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int bit[35];
int x[35];
int main()
{
    int n,q;
    while(cin>>n>>q)
    {
        int z=0;
        memset(bit,0,sizeof(bit));
        long long xxor;
        for(int i=1;i<=n;i++)
        {
           scanf("%d",&a[i]);
           if(i==1)xxor=a[1];
           else xxor^=a[i];
           z=0;
           int tmp=a[i];
           while(tmp)
           {
               bit[z++]+=tmp%2;
               tmp/=2;
           }
        }
        while(q--)
        {
            int p;
            scanf("%d",&p);
            int tmp=a[p];
             z=0;
             for(int i=0;i<=32;i++)
                x[i]=bit[i];
            while(tmp)
            {
                int y=tmp%2;
                tmp/=2;
                x[z]-=y;
                z++;
            }
           long long  cifang=1;
            int ans=0;
            long long tmp1=0,tmp2=0,tmp3=0;
            for(int i=0;i<=32;i++)
            {
                if(x[i]==n-1)tmp1+=cifang;
                if(x[i]>=1)tmp2+=cifang;
                cifang*=2;
            }
            cout<<(tmp1)<<" "<<tmp2<<" "<<((xxor^a[p]))<<endl;

        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值