#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf LLONG_MAX
#define pb push_back
#define deb cout<<"YES"<<endl
#define fi first
#define se second
#define pii pair<int,int>
#define lc 2*pos
#define rc 2*pos+1
int calc(int n,int a){
int x= 2*a + n-1;
x*=n;
x/=2;
return x;
}
signed main(){
vector<int>pul(10);
pul[0]=1;
for(int i=1;i<=6;i++) pul[i]=pul[i-1]*10;
int t;cin>>t;while(t--){
int a,b,c,k;cin>>a>>b>>c>>k;
if (a>b) swap(a,b);
if (c-b!=1 && c-b!=0){
cout<<-1<<endl;
continue;
}
int l=pul[a-1],r=pul[a]-1;
bool yes=0;
while(l<=r){
int mid=(l+r)/2;
int aa=pul[b]-1-max(pul[c-1]-pul[a-1],1ll)+1;
int cc=max(0ll,pul[b]-1+pul[a-1]-(pul[c]-1));
aa-=cc;
int prev=calc(mid-1-pul[a-1]+1,aa);
int cur=calc(mid-pul[a-1]+1,aa);
// cout<<mid<<" "<<prev<<" "<<cur<<" "<<aa<<endl;
int dif=k-prev;
int tar=max(pul[c-1]-mid-1,0ll)+dif;
int mi=tar+mid;
if (prev<=k && k<=cur && mi<=pul[c]){
cout<<mid<<" + "<<tar<<" = "<<mi<<endl;
yes=1;
break;
}
else if (cur<k) l=mid+1;
else r=mid-1;
}
if (!yes) cout<<-1<<endl;
}
}
Hi! So this is the problem: https://codeforces.me/contest/1836/problem/C I think the solution is basically the same but I am using binary search (which is actually not needed but yeah the idea should have been the same) but this is incorrect... does anyone know where is the fault or is it logic fault? thank you so much!