HDU-6646 A + B = C (Large Number Simulation)

Keywords: PHP

Topic link: http://acm.hdu.edu.cn/showproblem.php?pid=6646

Topic: Give a, b, c. If there is a*10^x + b*10^y = c*10^z output x y z, there is no output - 1.

Idea: First remove all the zeros behind a b c, and now a b c length is la lb lc.

If LC > La + lb, there are only two cases: a is 0 in the front, b is 0 in the back, or b is 0 in the front, a is 0 in the back, direct violent comparison, if not equal to c is - 1.

If lc<=la+lb, we must make sure that every bit of the result equals C equals the corresponding value in C. So we can compare a with C first. There are x-phases at the end, which can't be changed. If the end of b is aligned with the front of x, the value of X-Y-Z at this time will be calculated if it equals C. Unequal reproduction is compared with b and c, and the same treatment is done as above. If not, then - 1;

When this question is struck, it's really about explosion and extreme self-closure. Fortunately, it finally came out.

Code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=200005;
char a[N],b[N],c[N];
int d[N],ca[N],cb[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(d,0,sizeof(d));
        scanf("%s%s%s",a+1,b+1,c+1);
        int la=strlen(a+1),lb=strlen(b+1),lc=strlen(c+1);
        int aa=0,bb=0,cc=0,dd=0;
        while(a[la]=='0') la--,aa++;
        while(b[lb]=='0') lb--,bb++;
        while(c[lc]=='0') lc--,cc++;
        int ma=max(la,lb);
        if(la+lb<lc)
        {
            int flag=0;
            for(int i=1; i<=lc; i++)
            {
                if(i<=la&&a[i]!=c[i]) flag=1;
                else if(i>=la+1&&i<=lc-lb&&c[i]!='0') flag=1;
                else if(i>lc-lb&&c[i]!=b[i-(lc-lb)]) flag=1;
            }
            if(flag==0)
            {
                int maa=max(aa,max(bb,cc));
                printf("%d %d %d\n",(lc-la)+maa-aa,maa-bb,maa-cc);
            }
            else
            {
                flag=0;
                for(int i=1; i<=lc; i++)
                {
                    if(i<=lb&&b[i]!=c[i]) flag=1;
                    else if(i>=lb+1&&i<=lc-la&&c[i]!='0') flag=1;
                    else if(i>lc-la&&c[i]!=a[i-(lc-la)]) flag=1;
                }
                if(flag) printf("-1\n");
                else
                {
                    int maa=max(aa,max(bb,cc));
                    printf("%d %d %d\n",maa-aa,maa+lc-lb-bb,maa-cc);
                }
            }
        }
        else
        {
            memset(ca,0,sizeof(ca));
            memset(cb,0,sizeof(cb));
            int i=lb,j=lc,cnt=0,flag=1;
            for(; i>0; i--,j--)
                if(b[i]==c[j]) cnt++;
                else break;
            if(cnt==lc)
            {
                printf("-1\n");
                continue;
            }

            for(i=0,j=la; j>0; i++,j--) ca[i]=a[j]-'0';

            for(i=0,j=lb-cnt; j>0; i++,j--) cb[i]=b[j]-'0';
            int ll=max(la,lb-cnt);
            for(i=0; i<ll; i++)
            {
                int x=ca[i]+cb[i]+d[i];
                d[i+1]+=x/10;
                d[i]=x%10;
            }
            int dd=0;
            if(cnt==0)
            {
                i=0;
                while(d[i]==0)
                {
                    dd++; i++;
                }
            }
            for(i=lc-cnt,j=dd; i>0; j++,i--)
                if(c[i]!=d[j]+'0')
                {
                    flag=0;
                    break;
                }
            if(flag!=0)
            {
                int maa=max(aa,max(bb,cc));
                printf("%d %d %d\n",maa+cnt-aa,maa-bb,maa-cc+dd);
            }

            else
            {
                memset(ca,0,sizeof(ca));
                memset(cb,0,sizeof(cb));
                memset(d,0,sizeof(d));
                int i=la,j=lc,cnt=0,flag=1;
                for(; i>0; i--,j--)
                    if(a[i]==c[j]) cnt++;
                    else break;
                if(cnt==lc)
                {
                    printf("-1\n");
                    continue;
                }
                i=0,j=la-cnt;
                for(; j>0; i++,j--)
                    ca[i]=a[j]-'0';
                i=0,j=lb;
                for(; j>0; i++,j--)
                    cb[i]=b[j]-'0';
                int ll=max(la-cnt,lb);
                for(i=0; i<ll; i++)
                {
                    int x=ca[i]+cb[i]+d[i];
                    d[i+1]+=x/10;
                    d[i]=x%10;
                }
                int dd=0;
                if(cnt==0)
                {
                    i=0;
                    while(d[i]==0)
                    {
                        dd++; i++;
                    }
                }
                for(i=lc-cnt,j=dd; i>0; j++,i--)
                    if(c[i]!=d[j]+'0')
                    {
                        flag=0;
                        break;
                    }
                if(flag!=0)
                {
                    int maa=max(aa,max(bb,cc));
                    printf("%d %d %d\n",maa-aa,maa+cnt-bb,maa-cc+dd);
                }
                else printf("-1\n");
            }
        }
    }
    return 0;
}

 

Posted by lordphate on Thu, 03 Oct 2019 12:53:47 -0700