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 second

using 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()
{
    ios
    int 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值