Cod sursa(job #2398267)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 5 aprilie 2019 11:28:15
Problema Next Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>
#include <algorithm>

using namespace std;
ifstream f("next.in");
ofstream g("next.out");
unsigned long long x,d,i,t;
int a[1000005],b[1000005];
bool ok;
char ch;
int rest(long long d)
{
    int t=0;
    long long i=0;
    for(i=a[0]; i>=1; i--)
    {
        t=t*10+a[i];
        t=t%d;
    }
    return t;
}
int main()
{
    while(f.get(ch))
    {
        if(ch=='\n')
            break;
        else
        {
            a[++a[0]]=ch-'0';
        }
    }
    f>>d;
    reverse(a+1,a+a[0]+1);
    x=rest(d);
    if(x!=0)
    {
        a[1]-=x;
        if(a[1]<0)
        {
            a[1]+=10;
            i=2;
            while(a[i]==0)
            {
                a[i]=9;
                i++;
            }
            a[i]--;
            while(a[a[0]]==0)
                a[0]--;
        }
        while(d!=0)
        {
            b[++b[0]]=d%10;
            d/=10;
        }
        i=1;
        while(i<=max(a[0],b[0]))
        {
            a[i]=a[i]+b[i]+t;
            t=a[i]/10;
            a[i]%=10;
            i++;
        }
        if(t!=0)
            a[++a[0]]=t;
    }
    for(i=a[0]; i>=1; i--)
        g<<a[i];
    g<<'\n';
    return 0;
}