首页 > 其他分享 >TypeDB Forces 2023 E. The Harmonization of XOR

TypeDB Forces 2023 E. The Harmonization of XOR

时间:2023-01-31 00:01:13浏览次数:60  
标签:TypeDB 片段 xor Harmonization int tot long Forces XOR

链接:https://codeforces.com/contest/1787/problem/E
题意:给定n,有一个数组a,满足其所有元素均为1~n,给定k,问能否将数组拆为k个,其每一组的xor为x。
我们发现任意三个xor为k的片段合并后仍为k,我们考虑将序列分为尽量多的xor为k的片段,若tot<k,失败;否则若tot-k为奇数,由总数xor发现不符合条件,失败;否则成功,因为我们可以将多余的(多余的xor一定为0)放入其中一个片段中。
现在问题转化为如何尽量多地划分片段,考虑上界,对于x的二进制最高位,若由s个<=n的数在此位为1,则最多可以分为s个片段。
现证明上界可以取到:对于每个y满足在最高位为1,y xor x < y, 且不同y产生的值两两不同,故可以取到上界。
代码:

#define int long long
using namespace std;

int v[200100],tot;
pair<int,int> c[200100];
void solve(){
	tot=0;
	int sum=0;
	int ans=0;
	int cnt=0;
	int n,k,x;cin>>n>>k>>x;
	for(int i=0;i<=n;i++) v[i]=0;
	for(int i=1;i<=n;i++){
		ans^=i;
		if(v[i]) continue;
		int w=x^i;
		if(w<=n){
			v[i]=1,v[w]=1;
			c[++tot]={i,w};
			ans^=i,ans^=w;
		}
	}
	for(int i=1;i<=n;i++){
		if(!v[i]) sum++;
	}
	if(ans!=0||tot<k||(tot-k)%2){
		puts("NO");
		return;
	}
	puts("YES");
	int p=tot-k;
	for(int i=p+1;i<=tot-1;i++){
		if(c[i].second!=0) cout<<2<<" ";
		else{
		cnt++;
		 cout<<1<<" ";
		 }
		if(c[i].second!=0)
		cout<<c[i].first<<" "<<c[i].second<<" ";
		else cout<<c[i].first<<" ";
		cout<<endl;
	}
		cnt+=2;
		cnt+=sum;
		if(x<=n) cnt--;
		cnt+=p*2;
		cout<<cnt<<" ";
	if(c[tot].second!=0)
	cout<<c[tot].first<<" "<<c[tot].second<<" ";
	else cout<<c[tot].first<<" ";
	for(int i=1;i<=n;i++){
		if(!v[i]){
			cout<<i<<" ";
		}
	}
	if(p){
			for(int i=1;i<=p;i++){
				if(c[i].second!=0)
				cout<<c[i].first<<" "<<c[i].second<<" ";
				else cout<<c[i].first<<" ";
			}
		p=0;
		}
	cout<<endl;
}
signed main(){
	int T;cin>>T;
	while(T--) solve();
}

标签:TypeDB,片段,xor,Harmonization,int,tot,long,Forces,XOR
From: https://www.cnblogs.com/wjhqwq/p/17077586.html

相关文章

  • TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!)(持续更新)
    Preface猜结论场,很久没打比赛了然后赛前和ztc吹牛说每次隔一段时间来打CF就会有强运加持结果好家伙BCE全部秒出结论(而且我比赛时都证不来),而且写的A~E都是一发入魂凭借这......
  • TypeDB Forces 2023 (Div. 1 + Div. 2)
    题目链接A核心思路直接把其中一个数置为1就好了。//Problem:A.ExponentialEquation//Contest:Codeforces-TypeDBForces2023(Div.1+Div.2,Rated,Priz......
  • TypeDB Forces 2023 (Div. 1 + Div. 2) 题解
    更新中……A~D略。E.TheHarmonizationofXOR题目链接题意简述\(t\)组testcase,每组给定\(n,k,x\)三个数。求将\(1\simn\)划分成\(k\)个子序列(可以不连......
  • CodeForces 1415D XOR-gun
    洛谷传送门CF传送门纯纯的诈骗。下文令\(f(x)\)为\(x\)最高位使得这一位为\(1\)。考虑若存在\(i\in[1,n-2]\)使得\(f(a_i)=f(a_{i+1})=f(a_{i+2})\),那么......
  • 【FPGA】Verilog 编码实现:与非门 | 或非门 | 异或门 | NAND/NOR/XOR 行为验证
    写在前面:本章主要内容为了解和确认NAND/NOR/XOR门的行为,并使用Verilog实现,生成输入信号后通过模拟,验证每个门的操作,并使用FPGA来验证Verilog实现的电路的行为。本章目......
  • [CF1748F] Circular Xor Reversal
    题目描述Youhaveanarray$a_0,a_1,\ldots,a_{n-1}$oflength$n$.Initially,$a_i=2^i$forall$0\lei\ltn$.Notethatarray$a$iszero-i......
  • xorg 屏幕分辨率设置(x11分辨率设置/linux分辨率设置)
    记录一下,用于linux虚拟机分辨率设置。https://blog.csdn.net/weixin_36084095/article/details/116839103(在谷歌搜索是简书的文章,在百度搜市CSDN,百度似乎搜不到简书。)......
  • Optimization practice xor OR mov
    Therearetwosnippetsofcodesafteroptimizedbythecompilerwithlevels1and3:(Thiscodeisresponsiblefor"return0"inthebodyofthemainfunction!......
  • Count Pairs With XOR in a Range
    CountPairsWithXORinaRangeGivena(0-indexed) integerarray nums andtwointegers low and high ,returnthenumberofnicepairs.Anicepair is......
  • HDU-3949 XOR 题解报告
    题目地址题意:从一个序列中取任意个元素进行异或,求能异或出的所有数字中的第k小。分析:性质:一个线性基异或上另一个不同的线性基,并作为自己的新值,这并不改变整个线性......