Cod sursa(job #2113352)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 24 ianuarie 2018 15:11:45
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <cstring>
#include <cctype>

using namespace std;

ifstream f("next.in");
ofstream g("next.out");

long long int n,m,d=0;
short a[1000005],b[1000005],c[1000005],rest[1000005];
char c1;
long long int t,i,j;

int main()
{

    n=0;
    while(f.get(c1)&&isdigit(c1))
        a[++n]=(c1-'0');
    a[0]=n;

    m=0;
    while(f.get(c1)&&isdigit(c1))
    {
        b[++m]=(c1-'0');
        d=d*10+b[m];
    }
    b[0]=m;

    for(i=1; i<=n/2; i++)
        swap(a[i],a[n-i+1]);
    for(i=1; i<=m/2; i++)
        swap(b[i],b[m-i+1]);

    t=0;
    i=1;
    while(i<=a[0]||i<=b[0]||t>0)
    {
        t=t+a[i]+b[i];
        c[i]=t%10;
        t=t/10;
        i=i+1;
    }
    c[0]=i-1;

    t=0;
    i=c[0];
    while(i>0)
    {
        t=t*10+c[i];
        c[i]=t/d;
        t=t%d;
        i=i-1;
    }
    if(t==0)
        for(i=a[0]; i>=1; i--)
            g<<a[i];
    else
    {
        //rest= t;
        while(c[0]>0&&c[c[0]]==0)
            c[0]=c[0]-1;

        j=0;
        t=d-t;
        while(t)
        {
            rest[++j]=t%10;
            t/=10;
        }
        rest[0]=j;

        t=0;
        i=1;
        while(i<=a[0]||i<=rest[0]||t>0)
        {
            t=t+a[i]+rest[i];
            c[i]=t%10;
            t=t/10;
            i=i+1;
        }
        c[0]=i-1;
        for(i=c[0]; i>=1; i--)
            g<<c[i];
    }
    g<<'\n';
    return 0;
}