QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#832432 | #1289. A + B Problem | linweitong | AC ✓ | 128ms | 14396kb | C++14 | 1.6kb | 2024-12-25 21:24:14 | 2024-12-25 21:24:18 |
Judging History
answer
#include<bits/stdc++.h>
#define eb emplace_back
#define mk(x,y) make_pair(x,y)
#define fi first
#define se second
using namespace std;
const int N=1000005;
int n,m,a[N],b[N],c[N],ans[N],LL,L;
string S;
void gt(int L){
LL=L;
for (int i=1;i<=LL;++i)ans[i]=c[i];
}
bool chk(int x){
int o=n;
for (int i=0;i<x;++i)a[o--]=S[i]-48;
int u=n-x+1,v=m+1;
for (int i=x;i<(int)S.size();++i){
int o=S[i]-48;
if (u==1){b[--v]=o;continue;}
if (v==1){a[--u]=o;continue;}
if (!o){
if (u>v){
b[--v]=o;
}
else{
a[--u]=o;
}
}
else{
if (u>v){
a[--u]=o;
}
else{
b[--v]=o;
}
}
}
L=max(n,m)+1;
for (int i=1;i<=max(n,m)+1;++i)c[i]=0;
for (int i=1;i<=max(n,m)+1;++i){
c[i]+=a[i]+b[i];
c[i+1]+=c[i]/2;
c[i]&=1;
}
// cout<<"\n";
// cout<<x<<"\n";
// for (int i=n;i;--i)cout<<a[i];cout<<"\n";
// for (int i=m;i;--i)cout<<b[i];cout<<"\n";
// cout<<"\n";
while (c[L]==0&&L>1)--L;
if (L>LL)gt(L);
else if (L==LL){
for (int i=L;i>=1;--i){
if (c[i]>ans[i]){
gt(L);
break;
}
else if (c[i]<ans[i])break;
}
}
int ck=0;
for (int i=m;i;--i)
if (b[i]==0){
ck=i;break;
}
for (int i=1;i<=max(n,m)+1;++i)a[i]=b[i]=0;
return !(ck<n-x);
}
void solve(){
cin>>n>>m>>S;
if (n>m)swap(n,m);
LL=1;ans[LL]=0;
int l=0,r=n;
while (l<=r){
int mid=(l+r)>>1;
if (!chk(mid))l=mid+1;
else r=mid-1;
}
for (int i=LL;i;--i)cout<<ans[i];cout<<"\n";
}
void init(){
for (int i=0;i<=LL;++i)ans[i]=0;
LL=0;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin>>T;
while (T--)solve(),init();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9800kb
input:
3 4 3 1000101 2 2 1111 1 1 00
output:
1101 110 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 128ms
memory: 14396kb
input:
11110 10 8 111011010011100100 3 5 01011000 7 6 1110101010000 9 1 0110100101 1 9 0100001110 8 10 000101101011111000 9 6 011111111000111 1 9 1011101101 10 7 00100011000100000 4 9 1000101101010 8 4 100100110000 8 9 00101111011000101 8 9 11000000101011110 7 6 1111010100110 2 9 01001110101 4 5 100010100 ...
output:
10011010100 11100 10101000 110100101 100001110 10000001100 1000010111 111101101 1110100000 111101010 11110000 1000011101 1001011110 10101110 101110101 11100 1111010 1000010 1011100010 10010101001 10010001 1001010 1000000010 1110 111 1111110001 10110111 1100010101 10000000 111000011 110 11111 1100101...
result:
ok 11110 lines