AGAGA XOOORRR CodeForces - 1516B
生活随笔
收集整理的這篇文章主要介紹了
AGAGA XOOORRR CodeForces - 1516B
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AGAGA XOOORRR CodeForces - 1516B
題意:
給定一個長度為n的序列,對其進行若干次操作,每次操作將某相鄰兩數變為他們的異或和(操作后序列長度減一)。問是否可以經過若干次操作,得到一個長度至少為2且所有元素相等的序列?
題解:
我的思路,如果最終可以得到所有元素一樣,那將所有數異或得到的會是0,所以所有元素異或如果等于0就輸出YES
但是也有可能是奇數個一樣,這樣就會剩下一個x(其他都異或為0),而x就是所有元素都等于x,所以我們可以在一開始引入一個x,然后參與所有元素的異或中,如果過程中出現0,就tot++,,然后重新引入x,(tot記錄整個異或過程出現了幾次0)如果最后tot>1,就輸出YES,否則NO。
這個寫的很詳細
代碼:
#include<bits/stdc++.h> #define debug(a,b) printf("%s = %d\n",a,b); typedef long long ll; using namespace std;inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w; } const int maxn=2e4+9; int a[maxn]; int sum; int main() {int t;cin>>t;while(t--){int n;cin>>n;sum=0;for(int i=1;i<=n;i++){cin>>a[i];sum^=a[i];}if(sum==0)puts("YES");else{int tot=0;int ans=sum;for(int i=1;i<=n;i++){if(a[i]==0)continue;if(ans==0){tot++;ans=sum;}ans^=a[i];}if(tot>1&&(ans==0||ans==sum))puts("YES");else puts("NO");}} return 0; }總結
以上是生活随笔為你收集整理的AGAGA XOOORRR CodeForces - 1516B的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美的净水桶好用吗
- 下一篇: 351E. Jeff and Permu