Problem Description
Hi everyone! Welcome to the Stage 7 of this series of training contests. Cuber QQ is the problem settler of this contest; he has prepared 11 problems for you and wishes you to enjoy it. Good luck!
As the first problem of this contest, Cuber QQ thought that it’s reasonable to start with an easy one, so he modified the famous A + B problem by a little bit, so that it’s easy enough but not that trivial.
Given a,b,c , find an arbitrary set of x,y,z such that a⋅10x+b⋅10y=c⋅10z and 0≤x,y,z≤106.
Input
The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of test cases.
For each test case, there are three space-separated integers, a,b,c respectively (1≤a,b,c≤10100000).
Output
For each test case, please output three space-separated integers x,y,z. If there are multiple solutions, print any of them.
In case there are no solutions, please output −1 as a single line.
Sample Input
3
23 39 62
2 31 51
1 1 1
Sample Output
0 0 0
1 0 0
-1
思路
去a,b,c的前导零,枚举a的长度等于c长度,b的长度等于c长度,a+b=c,a的最后一位等于c的最后一位,b的最后一位等于c的最后一位的情况,计算是否符合条件,其余情况都无解。
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
const int INF=0x3f3f3f;
char a[N],b[N],c[N];
int na[N],nb[N],nc[N];
int sum[N];
int al,bl,cl;
int aq,bq,cq;
void init()
{
memset(na,0,sizeof(na));
memset(nb,0,sizeof(nb));
memset(nc,0,sizeof(nc));
int len=0,i=strlen(a)-1;
for(;i>0;i--,aq++) if(a[i]!='0') break;
for(;i>=0;i--) na[len++]=a[i]-'0';
al=len;len=0;
i=strlen(b)-1;
for(;i>0;i--,bq++) if(b[i]!='0') break;
for(;i>=0;i--) nb[len++]=b[i]-'0';
bl=len;len=0;
i=strlen(c)-1;
for(;i>0;i--,cq++) if(c[i]!='0') break;
for(;i>=0;i--) nc[len++]=c[i]-'0';
cl=len;len=0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
bool flag=true;
int i;
aq=bq=cq=0;
scanf("%s%s%s",a,b,c);
init();
memset(sum,0,sizeof(sum));
if(al==cl)
{
flag=true;
for(i=0;i<al;i++)
{
sum[i]+=(nc[i]-na[i]);
if(sum[i]<0)
{
sum[i+1]--;
sum[i]+=10;
}
}
/*for(int j=0;j<=cl;j++) cout<<sum[j];
cout<<endl;*/
for(i=0;i<cl;i++) if(sum[i]==nb[0]) break;
if(i+bl>cl) flag=false;
int w=i;
for(int j=0;j<bl && i<cl;i++,j++)
{
sum[i]-=nb[j];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
/*for(int j=0;j<=cl;j++) cout<<sum[j];
cout<<endl;*/
for(i=0;i<=cl;i++) if(sum[i]!=0) flag=false;
if(flag)
{
int minq=min(bq+cq,min(aq+cq+w,bq+aq));
printf("%d %d %d\n",bq+cq-minq,aq+cq+w-minq,bq+aq-minq);
continue;
}
}
if(bl==cl)
{
memset(sum,0,sizeof(sum));
flag=true;
for(i=0;i<bl;i++)
{
sum[i]+=(nc[i]-nb[i]);
if(sum[i]<0)
{
sum[i+1]--;
sum[i]+=10;
}
}
for(i=0;i<cl;i++) if(sum[i]==na[0]) break;
if(i+al>cl) flag=false;
int w=i;
for(int j=0;j<al && i<cl;i++,j++)
{
sum[i]-=na[j];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<=cl;i++) if(sum[i]!=0) flag=false;
if(flag)
{
int minq=min(bq+cq+w,min(aq+cq,bq+aq));
printf("%d %d %d\n",bq+cq+w-minq,aq+cq-minq,bq+aq-minq);
continue;
}
}
flag=true;
memset(sum,0,sizeof(sum));
for(i=0;i<max(al,bl);i++)
{
sum[i]+=(na[i]+nb[i]);
if(sum[i]>=10)
{
sum[i+1]+=sum[i]/10;
sum[i]%=10;
}
}
while(!sum[i]) i--;
//cout<<i<<' '<<cl<<endl;
if(cl>i+1) flag=false;
int w=i;
for(i=0;i<=w;i++) if(sum[i]) break;
int t=i;
for(int j=0;j<cl && i<=w;i++,j++)
{
sum[i]-=nc[j];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<=w+1;i++) if(sum[i]!=0) flag=false;
if(flag)
{
int minq=min(bq+cq,min(aq+cq,bq+aq+t));
printf("%d %d %d\n",bq+cq-minq,aq+cq-minq,bq+aq+t-minq);
continue;
}
flag=true;
memset(sum,0,sizeof(sum));
if(na[0]==nc[0])
{
for(i=0;i<cl;i++)
{
sum[i]+=nc[i]-na[i];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<cl;i++) if(sum[i]==nb[0]) break;
if(i+bl>cl) flag=false;
int w=i;
for(int j=0;j<bl && i<cl;i++,j++)
{
sum[i]-=nb[j];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<=cl;i++)
{
if(sum[i]!=0) flag=false;
}
if(flag)
{
int minq=min(bq+cq,min(aq+cq+w,bq+aq));
printf("%d %d %d\n",bq+cq-minq,aq+cq+w-minq,bq+aq-minq);
continue;
}
}
if(nc[0]==nb[0])
{
memset(sum,0,sizeof(sum));
flag=true;
for(i=0;i<cl;i++)
{
sum[i]+=nc[i]-nb[i];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<cl;i++) if(sum[i]==na[0]) break;
if(i+al>cl) flag=false;
int w=i;
for(int j=0;j<al && i<cl;i++,j++)
{
sum[i]-=na[j];
if(sum[i]<0)
{
sum[i]+=10;
sum[i+1]--;
}
}
for(i=0;i<=cl;i++)
{
if(sum[i]!=0)
{
flag=false;
break;
}
}
if(flag)
{
int minq=min(bq+cq+w,min(aq+cq,bq+aq));
printf("%d %d %d\n",bq+cq+w-minq,aq+cq-minq,bq+aq-minq);
continue;
}
}
printf("-1\n");
}
return 0;
}
/*
100000
33 6 633
996 996 100596
10 110 10110
23 39 62
2 31 51
1 1 1
96 3 96030
96 300003 309603
1005456 9094544 101
5450 5 55
40500 643 1048
23 39 62
2 31 51
20 49 5010000
20 49 5100000
2000000 490000000000000000000 51
200 490000000 51
2000000 49000 51
40500 643 1048
6003000000 101003 61040030000
19954009 800760010 10003001
5 6 11
1000000000000000000000 2 3
11111111 11 11111121000
9913293911030092 325365190001 9945830430030192000
99132939110300920000000000 3253651900010000000000 9945830430030192000
3 5 53
102 9080 1010000
*/