题解:CF1493E Enormous XOR
lkjlkjlkj2012 · · 题解
思路
首先注意到对于任意偶数
- 如果
x\equiv0\pmod2 且y\equiv0\pmod2 ,则 - 如果
x\equiv0\pmod2 且y\equiv1\pmod2 ,则 - 如果
x\equiv1\pmod2 且y\equiv0\pmod2 ,则 - 如果
x\equiv1\pmod2 且y\equiv1\pmod2 ,则
显然第二种情况没有第一种情况优。第一种情况在
PS:
代码
:::warning[真的要查看代码吗?]
#include <bits/stdc++.h>
using namespace std;
int n;
string l,r;
int compare(const string &a,const string &b)
// Warning: Compare as numbers instead of dictionary order. Time optimization is O(n).
{
if(a.length()<b.length())
return -1;
else if(a.length()>b.length())
return 1;
else if(a==b)
return 0;
else
return (a>b?1:-1);
}
string max(string a,string b)
// Warning: Compare as numbers instead of dictionary order. Time optimization is O(n).
{
if(compare(a,b)>=0)
return a;
else
return b;
}
void increase1(string &a)
{
int i;
for(i=a.length()-1;i>=0&&a[i]=='1';i--)
a[i]='0';
if(i>=0)
a[i]='1';
else
cout<<"[increase1]Warning: Number overflow."<<endl;
}
void decrease1(string &a)
{
int i;
for(i=a.length()-1;i>=0&&a[i]=='0';i--)
a[i]='1';
if(i>=0)
a[i]='0';
else
cout<<"[decrease1]Warning: Number overflow."<<endl;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n';
cin>>n>>l>>r;
assert(l.size()==n),assert(r.size()==n);
string mn,mx;
for(int i=1;i<=n;i++)
mn+='0',mx+='1';
// string ans=mn;
string ans=r;
// You can choose [r,r]
{
// Trying choose [r-2,r](if it is even,odd,even, bigger than r)
if(r[r.length()-1]=='0')
{
string tmp=r;
for(int i=1;i<=2;i++)
{
if(compare(tmp,l)<=0)
goto end;
decrease1(tmp);
}
// cout<<"choose ["<<tmp<<","<<r<<"]"<<endl;
string tmp2=r;
tmp2[tmp2.length()-1]='1';
ans=max(ans,tmp2);
}
end:;
}
{
if(l[0]=='0'&&r[0]=='1')
ans=max(ans,mx);
// Use 01111... and 10000...
}
cout<<ans;
}
:::