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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

牛客练习赛26 Dxor序列 (线性基)

發(fā)布時(shí)間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客练习赛26 Dxor序列 (线性基) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

鏈接:https://ac.nowcoder.com/acm/contest/180/D
來(lái)源:牛客網(wǎng)

xor序列
時(shí)間限制:C/C++ 1秒,其他語(yǔ)言2秒
空間限制:C/C++ 262144K,其他語(yǔ)言524288K
64bit IO Format: %lld
題目描述
小a有n個(gè)數(shù),他提出了一個(gè)很有意思的問(wèn)題:他想知道對(duì)于任意的x, y,能否將x與這n個(gè)數(shù)中的任意多個(gè)數(shù)異或任意多次后變?yōu)閥

輸入描述:
第一行為一個(gè)整數(shù)n,表示元素個(gè)數(shù)
第二行一行包含n個(gè)整數(shù),分別代表序列中的元素
第三行為一個(gè)整數(shù)Q,表示詢(xún)問(wèn)次數(shù)
接下來(lái)Q行,每行兩個(gè)數(shù)x,y,含義如題所示
輸出描述:
輸出Q行,若x可以變換為y,輸出“YES”,否則輸出“NO”
示例1
輸入
復(fù)制
5
1 2 3 4 5
3
6 7
2 1
3 8
輸出
復(fù)制
YES
YES
NO
說(shuō)明
對(duì)于(6,7)來(lái)說(shuō),6可以先和3異或,再和2異或
對(duì)于(2,1)來(lái)說(shuō),2可以和3異或
對(duì)于(3,8)來(lái)說(shuō),3不論如何都不能變換為8
備注:
對(duì)于100%的數(shù)據(jù),n,Q<=105
保證所有運(yùn)算均在int范圍內(nèi)

題意:

思路:

異或的性質(zhì):

y^y=0 則 x^y^y=x

令 x^z=y 兩邊異或x , 則 x^z^x=y^x -> z= y^x

即在數(shù)組中 找出一些數(shù)異或起來(lái)等于z即可。

這恰好是線(xiàn)性基的基礎(chǔ)功能。

細(xì)節(jié)見(jiàn)代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/struct LB {// 注意最高是60還是62ll d[61], p[61];int cnt, mx;LB() {memset(d, 0, sizeof(d));memset(p, 0, sizeof(p));cnt = 0, mx = 61;}void init() {memset(d, 0, sizeof(d));memset(p, 0, sizeof(p));}bool add(ll val) {/*插入時(shí)判斷之前是否有數(shù)會(huì)與val異或得0,判第k小時(shí)如果有為0的情況,k要減一*/for (int i = mx - 1; i >= 0; i--) {if (val & (1LL << i)) {if (!d[i]) {d[i] = val; break;}val ^= d[i];}}return val > 0;}bool query(ll val) { // 查詢(xún)val這個(gè)數(shù)是否存在for (int i = mx - 1; i >= 0; i--) {if (val & (1LL << i)) {if (!d[i]) return 0;val ^= d[i];}}return 1;}ll query_max(ll val) {ll ret = val;for (int i = mx - 1; i >= 0; i--) if ((ret ^ d[i]) > ret) ret ^= d[i];return ret;}ll query_min() {for (int i = 0; i < mx; i++) if (d[i]) return d[i];return 0;}void rebuild() {//消元,保存到p數(shù)組cnt = 0;for (int i = 0; i < mx; i++) {for (int j = 0; j < i; j ++ )if (d[i] & (1LL << j)) d[i] ^= d[j];}for (int i = 0; i < mx; i++) if (d[i]) p[cnt++] = d[i];}ll query_kth(ll k) { //使用前需要rebuildll ret = 0;if (k >= (1LL << cnt)) return -1;for (int i = cnt - 1; i >= 0; i--) if (k & (1LL << i)) ret ^= p[i];return ret;}ll find(ll x) { //找x是第幾大的數(shù),需保證x一定在ll ret = 0, c = 0;for (int i = 0; i < mx; i++) {if (d[i]) {if (x >> i & 1) ret += (1LL << c);c++;}}return ret;}LB operator+(const LB & _A)const { //合并LB ret = *this;for (int i = mx - 1; i >= 0; i--) if (_A.d[i]) ret.add(_A.d[i]);return ret;} }; LB base=LB();int main() {//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int q;int n,x;cin>>n;repd(i,1,n){cin>>x;base.add(x);}cin>>q;int y;while(q--){cin>>x>>y;x^=y;if(base.query(x)){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11249973.html

總結(jié)

以上是生活随笔為你收集整理的牛客练习赛26 Dxor序列 (线性基)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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