hdu 6186 CS Course 2017ACM/ICPC广西邀请赛-重现赛

CS Course

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 542    Accepted Submission(s): 264

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

 


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

Then n non-negative integers

a1,a2,,an follows in a line,

0ai109 for each i in range[1,n].

After that there are q positive integers

p1,p2,,pqin q lines,

1pin 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 3 1 1 1 1 2 3  

Sample Output

1 1 0 1 1 0 1 1 0

题意:

第一行表示n个数字,p次询问。

第二行为n个数字data[1-n]。

后面p行给出数字x。每次询问求出除了data[x]以外的所有n-1个数的 and, or, xor 值。

思路:

首先xor值可以通过n个数的xor值异或data[x]来求出。

然后把data[1-n]的每个数转换成二进制,如果当前位不为0,则该位的数组加一。

最后对询问的每个数字的每一位进行判断,剩余n-1个数字的位都为1 and值才能为1。剩余n-1个数字有一个为1则or值为1。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

const int maxn = 100005;
int data[maxn];
int nand[100];
//int nor [100];

int main(){
    //printf("%d\n",1^2^7);
    int n,p;
    while(scanf("%d%d",&n,&p)!=EOF){
    memset(data,0,sizeof(data));
    memset(nand,0,sizeof(nand));

    int nnn=0;
    int AND,OR,XOR=0;
    int i,j;

    for(i=1;i<=n;i++){
        scanf("%d",&data[i]);
        XOR=XOR^data[i];
        int flag = data[i];
        int nowi=1;
        while(flag){
            nand[nowi++]+=(flag%2);
            flag/=2;
        }
        nnn=max(nnn,nowi);

    }

    for(i=0;i<p;i++){
        int q;
        scanf("%d",&q);
        int ans1= XOR^data[q];
        int ans2=0,ans3=0;
        int now2=data[q];
        int fi=1;
        int fj=nnn;
        while(fj--){
            if((nand[fi]-(now2%2))==n-1)
                ans2+=pow(2,fi-1);
            if((nand[fi]-(now2%2))>0)
                ans3+=pow(2,fi-1);
            fi++;
            now2/=2;
        }
        printf("%d %d %d\n",ans2,ans3,ans1);
    }
    }
    return 0;
}

拼命恢复状态ing…

点赞