题目链接:HDU6646 A+B=C
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Special JudgeProblem 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补零到相同位数,分成四种情况分别考虑;
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+7;
const int N=3e5;
char s1[maxn],s2[maxn],s3[maxn];
int a[maxn],b[maxn],c[maxn],tmp[maxn];
int check(int c[],int a[],int b[],int len)
{
for (int i=0;i<N;i++) if(c[i]!=a[i])
{
if(c[i]<a[i]) return -1;
break;
}
for (int i=0;i<N;i++) tmp[i]=c[i]-a[i];
for (int i=N-1;i>=0;i--) if(tmp[i]<0) tmp[i]+=10,tmp[i-1]--;
int pos=0;
while(!tmp[pos] && pos<=N) pos++;
if(pos==N) return -1;
for (int i=0;i<len;i++) if(tmp[pos+i]!=b[i]) return -1;
for (int i=len;i<N;i++) if(tmp[pos+i]) return -1;
return N-len-pos;
}
void Out(int x1,int x2,int x3)
{
int tt=min({x1,x2,x3});
x1-=tt;x2-=tt;x3-=tt;
printf("%d %d %d\n",x1,x2,x3);
}
void rua()
{
scanf("%s%s%s",s1+1,s2+1,s3+1);
for (int i=0;i<maxn;i++) a[i]=b[i]=c[i]=0;
int len1=strlen(s1+1),len2=strlen(s2+1),len3=strlen(s3+1),kk;
for(int i=1;i<=len1;++i) a[i]=s1[i]-'0';
for(int i=1;i<=len2;++i) b[i]=s2[i]-'0';
for(int i=1;i<=len3;++i) c[i]=s3[i]-'0';
if((kk=check(c+1,a+1,b+1,len2))>0) {Out(N-len1,kk,N-len3);return ;}
if((kk=check(c+1,a,b+1,len2))>0) {Out(N-len1-1,kk,N-len3);return ;}
if((kk=check(c+1,b+1,a+1,len1))>0) {Out(kk,N-len2,N-len3);return ;}
if((kk=check(c+1,b,a+1,len1))>0) {Out(kk,N-len2-1,N-len3);return ;}
printf("-1\n");
}
int main()
{
int t;scanf("%d",&t);
while (t--) rua();
}
- 将所有的后缀0删掉,显而易见的,A B C中一定有两个是0,加加减减判断是否是第三者的倍
其实我就是来偷男朋友高精板子的嘻嘻,郑伊杰是臭猪猪嘻嘻
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define dep(i,a,b) for(ll i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
char a[maxn],b[maxn],c[maxn];
int l1,l2,l3;
struct bign{
int d[maxn],len;
void clean(){while(len>1&&!d[len-1]) len--;}
bign(){ memset(d, 0, sizeof(d)); len = 1;}
bign(int num){ *this = num;}
bign(char* num) { *this = num; }
bign operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bign operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator + (const bign& b){
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bign operator += (const bign& b){
*this = *this + b;
return *this;
}
bool operator <(const bign& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{return b < *this;}
bool operator<=(const bign& b) const{return !(b < *this);}
bool operator>=(const bign& b) const{return !(*this < b);}
bool operator!=(const bign& b) const{return b < *this || *this < b;}
bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
void print(){
dep(i,len-1,0) printf("%d",d[i]);
}
};
bign aa,bb,cc;
int check(bign x,int l,char c[]){
x.clean();
int len=x.len;
rep(i,1,l) if(x.d[len-i]!=c[i]-'0') return -1;
rep(i,0,len-1-l) if(x.d[i]!=0) return -1;
return len-l;
}
void solve(){
scanf("%s%s%s",a+1,b+1,c+1);
l1=strlen(a+1),l2=strlen(b+1),l3=strlen(c+1);
int t1=0,t2=0,t3=0;
while(a[l1]=='0') l1--,t1++;
while(b[l2]=='0') l2--,t2++;
while(c[l3]=='0') l3--,t3++;
a[l1+1]='\0';b[l2+1]='\0';c[l3+1]='\0';
aa=(a+1);bb=(b+1);cc=(c+1);
int ans1,ans2,ans3;
if((ans3=check(aa+bb,l3,c))>=0){
ans1=-t1,ans2=-t2,ans3-=t3;
int g=min({ans1,ans2,ans3});
printf("%d %d %d\n",ans1-g,ans2-g,ans3-g);
return;
}
else if((ans2=check(cc-aa,l2,b))>=0){
ans1=-t1,ans2-=t2,ans3=-t3;
int g=min({ans1,ans2,ans3});
printf("%d %d %d\n",ans1-g,ans2-g,ans3-g);
return;
}
else if((ans1=check(cc-bb,l1,a))>=0){
ans1-=t1,ans2=-t2,ans3=-t3;
int g=min({ans1,ans2,ans3});
printf("%d %d %d\n",ans1-g,ans2-g,ans3-g);
return;
}
puts("-1");
}
int main(){
int T;cin>>T;
while(T--) solve();
}