ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【CF1438D Powerful Ksenia】题解

2022-01-25 17:04:22  阅读:183  来源: 互联网

标签:CF1438D ch ai 题解 ak aj int Ksenia oplus


题目链接

题目

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;
}


标签:CF1438D,ch,ai,题解,ak,aj,int,Ksenia,oplus
来源: https://www.cnblogs.com/zhangtingxi/p/15843661.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有