HDUOJ 6186 CS Course

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186

题意:n个数,若干询问,每次询问q,问拿掉a[q]后剩余数的And Or Xor值。

思路:先求所有数的And Or Xor还有每一位上1的总数。对于查询的话,拿掉a[q]由异或的性质可得a[q]^ Xor,直接输出就好,那么对于AndOr我们应该采用二进制展开,然后一位位看,对于某一位而言And后为1的条件就是该位上n个数都为1,所以如果这一位1的总数为n-1a[q]的这一位是0那么拿掉后对这位的贡献就是增加1<<j,同理分析Or,如果这一位1的总数为1a[q]的这一位是1那么拿掉后对这位的贡献就是减少1<<j

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const ll INF=0xffffffff;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T> inline T gcd(T&a,T&b){return b==0?a:gcd(b,a%b);}
template<typename T> inline T lcm(T&a,T&b){return a/gcd(a,b)*b;}
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
int n,q,a[maxn],cnt[100];
ll x,y,z;
int main(){
    while (~scanf("%d%d",&n,&q)){
        memset(cnt,0,sizeof(cnt));
        x=INF,y=0,z=0;
        for (int i=1;i<=n;i++){
            read(a[i]);
            int tmp=a[i];
            x&=a[i],y|=a[i],z^=a[i];
            for (int j=0;tmp;j++){
                cnt[j]+=(tmp%2);
                tmp>>=1;
            }
        }
        for (;q--;){
            int p;read(p);
            int tmp=a[p],tmpx=x,tmpy=y;
            for (int j=0;j<=30;j++){
                if (cnt[j]==n-1 && (tmp%2==0))tmpx+=(1<<j);
                if (cnt[j]==1 && (tmp%2)) tmpy-=(1<<j);
                tmp>>=1;
            }
            printf("%I64d %I64d %I64d\n",tmpx,tmpy,z^a[p]);
        }
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注