C. XOR-distance
1.C. Insert and Equalize2.C. Removal of Unattractive Pairs3.D. Jumping Through Segments4.E. Good Triples5.F. Shift and Reverse6.D. Yet Another Monster Fight7.A. Constructive Problems8.C. Game with Multiset9.A. Rating Increase10.B. Swap and Delete11.A. Problemsolving Log12.B. Preparing for the Contest13.C. Quests14.D. Three Activities15.E2. Game with Marbles (Hard Version)16.cf刷题有感17.A. Anonymous Informant18.A. Forked!19.B. Make Almost Equal With Mod20.C. Heavy Intervals21.D. Split Plus K22.A. 202323.B. Two Divisors24.C. Training Before the Olympiad25.D. Mathematical Problem26.F. Greetings27.C. Partitioning the Array28.G. Bicycles29.E. Eat the Chip30.G. Lights31.D. Array Repetition32.D. Berserk Monsters33.E. Increasing Subsequences34.D. Very Different Array35.G. Mischievous Shooter36.B. Plus-Minus Split37.B. A Balanced Problemset38.C. Did We Get Everything Covered39.D. Find the Different Ones!40.C. Grouping Increases41.D. Good Trip42.C. Physical Education Lesson43.E. Final Countdown44.D. Divisible Pairs45.G. Vlad and Trouble at MIT46.A. Brick Wall47.B. Minimize Inversions
48.C. XOR-distance
49.A. Moving Chips50.B. Monsters Attack!51.C. Find B52.D. Slimes53.C. Turtle Fingers: Count the Values of k54.D. Turtle Tenacity: Continual Mods55.D. Vlad and Division56.C. LR-remainders57.D. Lonely Mountain Dungeons题解
既然是按位异或,那么我们尝试在二进制视角下考虑问题
我们发现,当两个数的同一位置上都有1的时候,这个1消与不消对结果没有影响,而这个位置上其中一个有一,另一个没有一时,我们可以通过x来转移一
所以在ab俩个数出现第一个10情况不同的位置之后,我们要尽可能地把剩余的一尽可能地移到较小的数
并且高位一加成效果远大于地位一,所以我们贪心移一
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll a,b,r;
cin>>a>>b>>r;
ll x=0,i=log2(max(a,b)),len=log2(r);
for(;i>=1;i--)//找第一个不同10的位置
{
if(((a>>i)&1)==1&&((b>>i)&1)==0)//判断一个位置上是一还是零的方法是右移该位置至第0位,然后与1
{
for(ll j=min(i-1,len);j>=0;j--)
{
ll dif=(1LL<<j);
if(r-dif>=0&&(a&dif)!=0&&(b&dif)==0)
{
a^=dif;
b^=dif;
r-=dif;
}
}
break;
}
else if(((a>>i)&1)==0&&((b>>i)&1)==1)
{
for(ll j=min(i-1,len);j>=0;j--)
{
ll dif=(1LL<<j);//这里一定要1LL!不然会溢出错误
if(r-dif>=0&&(a&dif)==0&&(b&dif)!=0)//对于与的判断是不等于零,因为与的结果实际上是该位置的2的次方!!
{
a^=dif;
b^=dif;
r-=dif;
}
}
break;
}
}
cout<<abs(a-b)<<endl;
}
return 0;
}
合集:
codeforces
, 思维题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~