【CF1438D Powerful Ksenia】题解

题目链接

题目

Ksenia has an array a a a consisting of n n n positive integers a1,a2,…,an a_1, a_2, \ldots, a_n a1​,a2​,…,an​ .

In one operation she can do the following:

  • choose three distinct indices i i i , j j j , k k k , and then
  • change all of ai,aj,ak a_i, a_j, a_k ai​,aj​,ak​ to ai⊕aj⊕ak a_i \oplus a_j \oplus a_k ai​⊕aj​⊕ak​ simultaneously, where ⊕ \oplus ⊕ denotes the bitwise XOR operation.

She wants to make all ai a_i ai​ equal in at most n n n operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!

\(n\) 个正整数 \(a_i\),\(1 \le a_i\le10^9\).

每次操作是选三个不同的下标 \(i,j,k\) ,让 \(a_i,a_j,a_k\) 都变成 \(a_i \oplus a_j\oplus a_k\) 也就是这三个数的异或和.

让你判断是否能在 \(n\) 次操作内,使这 \(n\) 个正整数的值变成相同的.

若能,第一行输出YES,第二行输出 \(m\) 表示操作数两,接下来 \(m\) 行每行三个整数,描述一次操作;

若不能,输出NO

思路

考虑一下奇数的情况。

我们可以连续三个弄一下,如下图所示:

image

然后会出现两个两个相同的和最后一个数。

然后我们再倒着做一遍,就成功变成一样的。

而偶数的情况只有异或之和为0才可行。

我们先对前 \(n-1\) 个作和奇数相同的操作,当此操作做完后,所有的都相同了。

总结

这是一道非常有意思的构造题。

首先要把任选三个变成连续三个,是突破之一。

同时奇偶分开考虑,也是一个突破口。

Code

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k;

signed main()
{
//	freopen("tiaoshi.in", "r", stdin);
//	freopen("tiaoshi.out", "w", stdout);
	n=read(); 
	for(i=1; i<=n; ++i) k^=read(); 
	if(n%2==0 && k) return printf("NO"), 0;
	printf("YES\n"); 
	if(n%2==0) --n; 
	for(i=1; i+2<=n; i+=2) ++m; 
	for(i=n-2; i>=3; i-=2) ++m; 
	printf("%d\n", m); 
	for(i=1; i+2<=n; i+=2) printf("%d %d %d\n", i, i+1, i+2); 
	for(i=n-2; i>=3; i-=2) printf("%d %d %d\n", i-2, i-1, i); 
	return 0;
}


posted @ 2022-01-25 16:56  zhangtingxi  阅读(30)  评论(0编辑  收藏  举报