Codeforces Round #682 (Div. 2)D Powerful Ksenia ///思维
cf地址
題目大意:Ksenia has an array a consisting of n positive integers a1,a2,…,an.
In one operation she can do the following:
choose three distinct indices i, j, k, and then
change all of ai,aj,ak to ai⊕aj⊕ak simultaneously, where ⊕ denotes the bitwise XOR operation.
She wants to make all ai equal in at most n operations, or to determine that it is impossible to do so. She wouldn’t ask for your help, but please, help her!
Input
The first line contains one integer n (3≤n≤105) — the length of a.
The second line contains n integers, a1,a2,…,an (1≤ai≤109) — elements of a.
Output
Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most n operations.
If it is possible, print an integer m (0≤m≤n), which denotes the number of operations you do.
In each of the next m lines, print three distinct integers i,j,k, representing one operation.
If there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.
Examples
inputCopy
5
4 2 1 7 2
outputCopy
YES
1
1 3 4
inputCopy
4
10 4 49 22
outputCopy
NO
Note
In the first example, the array becomes [4⊕1⊕7,2,4⊕1⊕7,4⊕1⊕7,2]=[2,2,2,2,2].
題目大意: 每次選擇3個下標 i,j,k,然后把a[i],a[j],a[k]變成a[i] ^ a[j] ^a[k] (異或),問在n次操作內 能不能把所有的數變成一樣的。
思路:把a[1]除去,然后兩兩配對如:(2,3),(4,5),(6,7)……;
用a[1]依次和這些對進行異或操作:(1,2,3)
這時a[1]=a[1]^a[2] ^a[3],且a[2]=a[3] (意味這a[2] ^a[3]=0);
然后(1,4,5)
這時a[1]=a[1]^a[2] ^a[3] ^a[4] ^a[5] ,且a[4]=a[5] (意味這a[4] ^a[5]=0);
進行完一輪之后 a[1]=a[1]^a[2] ^a[3] ^a[4] ^a[5]…… ^a[n];
且剩下的對中的數,都是相等的。
然后再進行一輪操作:所以有的數就會變成a[1] (最后的a[1]);
如果n是偶數,那么a[1]=a[1]^a[2] ^a[3] ^a[4] ^a[5]…… ^a[n-1]; 如果最后的這個數 !=a[n],則無解。
代碼:
#include<bits/stdc++.h> #define INF 0x3f3f3f3f3f3f3f3f #define inf 0x3f3f3f3f #define FILL(a,b) (memset(a,b,sizeof(a))) #define re register #define lson rt<<1 #define rson rt<<1|1 #define lowbit(a) ((a)&-(a)) #define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0); #define fi first #define rep(i,n) for(int i=0;(i)<(n);i++) #define rep1(i,n) for(int i=1;(i)<=(n);i++) #define se secondusing namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int > pii; int dx[4]= {-1,1,0,0},dy[4]= {0,0,1,-1}; const ll mod=10; const ll N =2e6+10; const double eps = 1e-4; const double pi=acos(-1); ll gcd(int a,int b){return !b?a:gcd(b,a%b);} ll a[N];int main() {iosint t;t=1;while(t--){int n;cin>>n;int s=0;rep1(i,n) cin>>a[i];if(n%2==0){for(int i=1;i<=n-1;i++) s^=a[i];if(s!=a[n]) {cout<<"NO\n";return 0;}n--;}cout<<"YES\n";cout<<n-1<<endl;for(int i=2;i<n;i+=2){cout<<1<<" "<<i<<" "<<i+1<<endl;}for(int i=2;i<n;i+=2){cout<<1<<" "<<i<<" "<<i+1<<endl;}}return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Codeforces Round #682 (Div. 2)D Powerful Ksenia ///思维的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 萝卜汤要怎么做 白萝卜汤的做法
- 下一篇: cf平民玩家怎么获得永久神器 为你介绍几