首页 > 其他分享 >CF1748D ConstructOR 题解

CF1748D ConstructOR 题解

时间:2022-11-16 09:56:39浏览次数:75  
标签:10 101 题解 ConstructOR 样例 CF1748D lowbit aligned operatorname

可能更好的食用体验

既然题目中用到了位运算,那我们就用二进制来解决这道题。

1.判无解

观察 \(3\,4\,6\) 这个样例,我们将其分解二进制:

\[\begin{aligned} (3)_{10} &= (11)_2 \\ (4)_{10} &= (100)_2\\ (6)_{10} &= (110)_2\\ \end{aligned} \]

我们设 \(\operatorname{lowbit}(x)\) 表示 \(x\) 最末尾的 \(1\) 在第几位,那么我们可以发现一条性质:\(\operatorname{lowbit}(d)\le\operatorname{lowbit}(a\operatorname{ or }b)\)。如果不满足,那么一定无解。

2.构造 \(x\)

我们来分析样例中的第一组数据:

\[\begin{aligned} a &= 1100\\ b &= 100111\\ d &= 101\\ \end{aligned} \]

\[\begin{aligned} 1100&\\ 100111&\\ \text{---------------}\\ 101&\\ 101\;\;&\\ 101\;\;\;\;\;\;\;\;\;\\ \text{---------------}\\ 10101111 \end{aligned} \]

我们可以发现 \(x\) 有若干个 \(d\) 相加得到,并且 \((a\operatorname{or}x )=x\),\((b\operatorname{or}x )=x\),所以一定满足 \(d\,|\,(a\operatorname{or}x)\) 且 \(d\,|\,(a\operatorname{or}x)\)。

\(x\) 的构造方法如上所示。设 \(c\gets (a\operatorname{or}b)\),则对于 \(c\) 的每 \(i\) 位二进制位为 \(1\),如果 \(x\) 的第 \(i\) 位也为 \(1\) 则跳过,否则 \(x\gets x+2^{i-\operatorname{lowbit}(d)}\)。

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,a,b,d,k,x;
void solve(){
	cin>>a>>b>>d;
	k=x=0;//k表示lowbit(d)
	while((d>>k&1)^1)k++;
	for(int i=0;i<30;i++)
		if( ((a|b)>>i&1) && (ans>>i&1)==0 )
			if(i<k)return void(puts("-1"));//判无解
			else x+=(d<<i-k);
	cout<<x<<endl;
}
signed main(){
	cin>>T;
	while(T--)solve();
	return 0;
}

P.s. 应为构造方式比较特殊,所以输出会和样例输出不一样,不要像我一样傻傻的为此去调了二十分钟

样例输出

175
14
-1
-1
2
27
64446
143838208856161791

标签:10,101,题解,ConstructOR,样例,CF1748D,lowbit,aligned,operatorname
From: https://www.cnblogs.com/As-Snow/p/16894871.html

相关文章

  • 洛谷 P8509 题解(待完善)
    题面。要求所有点到关键点的距离和最小。首先要确定这个点去哪个关键点最短。树上两点间路径与距离是唯一的,所以我们从两个关键点分别跑dfs。第一遍求出每个点到s的最......
  • 神奇脑洞题解——USACO追查坏牛奶(CSGO894)
    COGS的这一题是超级满配版本比洛谷的要强力的多:894.追查坏牛奶-COGS额外要求是:求出最小割流量,同时求出割边最小,同时字典序最小的方案输出割掉的边最小割流量好求,最......
  • 【题解】[模拟赛20221115] Tree
    模拟赛20221115Tree|CQNK\(O(m*n*2^n)\)很好做,但是本题有更优秀的做法:在此记录复杂度\(O(n*2^n)\)的做法。考虑从后往前dp,设dp状态\(f_{s,0/1}\)分别表示在填......
  • D. ConstructOR
    D.ConstructORYouaregiventhreeintegers$a$,$b$,and$d$.Yourtaskistofindanyinteger$x$whichsatisfiesallofthefollowingconditions,ordetermi......
  • 题解 ARC001C【パズルのお手伝い】
    postedon2021-01-1118:20:37|under题解|source前言这道题与八皇后很像,可以做完八皇后再来做这道题。如果你\(\color{white}\colorbox{red}{WA}\)了,请检查你有......
  • 题解 UVA12265【贩卖土地 Selling Land】
    postedon2022-09-2414:33:29|under题解|sourceproblem一个黑白矩阵,求以每个点为右下角,能围出的周长最大的全白矩形的周长。\(n\leq2000\)。solution试图进行......
  • 焦点科技编程挑战赛2022题解
    比赛说明:比赛在四个学校开展,南理南航南农和矿大。题目查找文本差异要求origin和dest中分别包含1000w+条数据dest对数据进行了打乱操作,即origin和dest中相同数据行的......
  • CF1598F RBS 题解
    题意给\(n\)个串,求将这\(n\)个串以任意顺序接起来的最大的是合法括号序列的前缀数。分析\(n\)很小,考虑状压dp。有一个很典的转化是将(看成\(+1\),将)看成\(-......
  • CF 1743 题解
    比赛链接:https://codeforces.com/contest/1743题解:AB水题//bySkyRainWind#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include......
  • week3题解
    1.寄包柜   看到题目最容易想到开二位数组但数据量过大,因此需要map#include<bits/stdc++.h>usingnamespacestd;map<int,map<int,int>>a;这里开了一个map......