日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 6186 CS Course

發(fā)布時間:2024/10/6 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 6186 CS Course 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


點擊打開鏈接

CS Course

Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1699????Accepted Submission(s): 726


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 31 1 1123 Sample Output 1 1 01 1 01 1 0
Source 2017ACM/ICPC廣西邀請賽-重現(xiàn)賽(感謝廣西大學)
Recommend liuyiding???|???We have carefully selected several similar problems for you:??6275?6274?6273?6272?6271
?

題意:

給定n個數(shù),然后是q次詢問,每次詢問為去掉第i個數(shù),輸出剩下n-1個數(shù)與、或、異或的結果

解法1:

前綴數(shù)組和后綴數(shù)組

用p1[i]表示 [1,i] 所有數(shù)與的結果,b1[i]表示 [i,n] 所有數(shù)與的結果


#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; int a[maxn]; int p1[maxn],b1[maxn];//and int p2[maxn],b2[maxn];//or int p3[maxn],b3[maxn];//xor int main() {ios::sync_with_stdio(0);int n,q,p;while(cin>>n>>q){cin>>a[1];p1[1]=p2[1]=p3[1]=a[1];for(int i=2;i<=n;i++){cin>>a[i];p1[i]=p1[i-1]&a[i];p2[i]=p2[i-1]|a[i];p3[i]=p3[i-1]^a[i];}b1[n]=b2[n]=b3[n]=a[n];for(int i=n-1;i>=1;i--){b1[i]=b1[i+1]&a[i];b2[i]=b2[i+1]|a[i];b3[i]=b3[i+1]^a[i];}while(q--){cin>>p;if(p==1)printf("%d %d %d\n",b1[p+1],b2[p+1],b3[p+1]);else if(p==n)printf("%d %d %d\n",p1[p-1],p2[p-1],p3[p-1]);elseprintf("%d %d %d\n",p1[p-1]&b1[p+1],p2[p-1]|b2[p+1],p3[p-1]^b3[p+1]);}}return 0; }

解法2:

用num數(shù)組記錄n個數(shù)轉為二進制后,每一位上1的個數(shù)之和,

當去掉一個數(shù)后,更新num數(shù)組,然后遍歷num數(shù)組

如果num[i]==n-1,那么這一位與的結果為1,否則為0(與運算:全1為1,有0為0)

如果num[i]>=1,那么這一位或的結果為1,否則為0,(或運算:全0為0,有1為1)

對于異或運算:

先讓n個數(shù)異或的結果為XOR,然后XOR和去掉的數(shù)字異或,結果就是n-1個數(shù)字異或的結果(異或運算的逆運算)


#include<bits/stdc++.h> #include<bitset> using namespace std; const int maxn=1e5+5; int a[maxn]; int num[32];int main() {ios::sync_with_stdio(0);int n,q,p;while(cin>>n>>q){int XOR=0;fill(num,num+32,0);for(int i=1;i<=n;i++){cin>>a[i];XOR^=a[i];bitset<32>bit(a[i]);for(int j=0;j<32;j++)if(bit[j]==1)num[j]++;}while(q--){cin>>p;bitset<32>bit(a[p]);bitset<32>AND;//n-1個數(shù)字與的結果bitset<32>OR;//n-1個數(shù)字或的結果for(int j=0;j<32;j++){if(num[j]-bit[j]==(n-1))//這一位有n-1個1,與的結果為1AND.set(j);//把第j位置1elseAND.reset(j);//把第j位置0if(num[j]-bit[j]>=1)//這一位上有1,或的結果為1OR.set(j);elseOR.reset(j);}printf("%d %d %d\n",AND,OR,XOR^a[p]);}}return 0; }

總結

以上是生活随笔為你收集整理的HDU 6186 CS Course的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。