Cod sursa(job #2489395)

Utilizator Robert.BrindeaBrindea Robert Robert.Brindea Data 8 noiembrie 2019 18:48:58
Problema Next Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int MAXNR = 1000003;

ifstream fin ("next.in");
ofstream fout ("next.out");

class h
{
public:
    int cif[MAXNR];
    h(){}
    h(long long a)
    {
        cif[0] = 0;
        while(a)
        {
            cif[++cif[0]] = a%10;
            a/=10;
        }
    }
    void read()
    {
        char str;
        str = fin.get();
        while(str >= '0' && str <= '9')
        {
            cif[++cif[0]] = str-'0';
            str = fin.get();
        }
        for(int i = 1; i <= cif[0]/2; i++)
            swap(cif[i], cif[cif[0]-i+1]);
    }
    void print()
    {
        for(int i = cif[0]; i >= 1; i--)
            fout << cif[i];
    }
    h operator+(h b)
    {
        h res = h(0);
        for(int i = cif[0]+1; i <= b.cif[0]; i++)
            cif[i] = 0;
        for(int i = b.cif[0]+1; i <= cif[0]; i++)
            b.cif[i] = 0;
        int t = 0;
        for(int i = 1; i <= max(cif[0], b.cif[0]); i++)
        {
            res.cif[++res.cif[0]] += cif[i] + b.cif[i] + t;
            t = res.cif[i]/10;
            res.cif[i] %= 10;
        }
        while(t)
        {
            res.cif[++res.cif[0]] = t%10;
            t/=10;
        }
        return res;
    }
    long long operator%( long long b)
    {
        long long rez=0;
        for(int i = cif[0]; i >= 1; i--)
            rez=(10*rez + cif[i])%b;
        return rez;
    }
};

h a;

int main()
{
    long long b;
    a.read();
    fin >> b;
    long long rest = a%b;
    if(rest == 0)
        a.print();
    else
    {
        h term = h(b - a%b);
        a = a+term;
        a.print();
    }
    return 0;
}