Cod sursa(job #2489348)

Utilizator Robert.BrindeaBrindea Robert Robert.Brindea Data 8 noiembrie 2019 17:11:09
Problema Next Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

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

class h
{
public:
    int cif[10003];
    h(){}
    h(unsigned long long a)
    {
        cif[0] = 0;
        while(a)
        {
            cif[++cif[0]] = a%10;
            a/=10;
        }
    }
    void read()
    {
        char str[1004];
        fin.getline(str, 1004);
        for(int i = strlen(str)-1;i >= 0; i--)
            cif[++cif[0]] = (str[i]-'0');
    }
    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;
    }
    unsigned long long operator%(unsigned long long b)
    {
        unsigned long long rez=0;
        for(int i = cif[0]; i >= 1; i--)
            rez=(10*rez + cif[i])%b;
        return rez;
    }
};

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