Cod sursa(job #3131511)

Utilizator uncle_sam_007IOAN BULICA uncle_sam_007 Data 20 mai 2023 14:09:37
Problema Next Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 5.44 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;

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

//const int MAX_DIGITS =1000005;
const int MAX_DIGITS =99999;
const int BASE = 10;

class HugeN
{
private:
    int x[MAX_DIGITS];
public:
    //constructori
    HugeN()
    {
        memset(x,0,sizeof(x));
        x[0] = 1;
    }

    HugeN( int n)
    {
        memset(x,0,sizeof(x));
        x[0] = 0;
        do
        {
            x[++x[0]] = n%10;
            n = n/10;
        }
        while(n);
    }
    HugeN(string r)
    {
        memset(x,0,sizeof(x));
        int l=r.length()-1;
        while(l>=0)
        {
            x[++x[0]]=(r[l]-'0');
            l--;
        }
    }
    HugeN(const HugeN& other)
    {
        memcpy(x,other.x,sizeof(other.x));
    }
    void print()
    {
        for( int i = x[0]; i >= 1; i--)
            fout<<x[i];
        fout<<"\n";
    }

    // compararea a doua HugeN
    int cmp(const HugeN other)
    {
        // x ? other
        if (x[0] < other.x[0]) return -1;
        else if (x[0] > other.x[0]) return 1;
        for ( int i = x[0] ; i > 0 ; i--)
            if ( x[i] < other.x[i]) return -1;
            else if ( x[i] > other.x[i]) return 1;
        return 0;
    }

    //supraincarcarea operatorilor
    bool operator <  (const HugeN other );
    bool operator <= (const HugeN other );
    bool operator >  (const HugeN other );
    bool operator >= (const HugeN other );
    bool operator == (const HugeN other );
    bool operator != (const HugeN other );

    HugeN operator +(const HugeN other );
    HugeN operator -(const HugeN other );
    HugeN operator *(int k);
    HugeN operator / (int k);
    HugeN operator /=(long long k);
    HugeN operator +=(const HugeN other );
    HugeN operator -=(const HugeN other );
    HugeN operator *(const HugeN other );
    HugeN operator *=(int k);
    HugeN operator /=(int k);
    int  operator %(long long k);

    HugeN operator <<= (int k); // x = x * 10^k
    HugeN operator >>=(int k);  // x = x / 10^k
};
inline bool HugeN:: operator < (const HugeN other )
{
    // x < other.x
    if ((*this).cmp(other) == -1)
        return 1;
    return 0;
}
inline bool HugeN:: operator <= (const HugeN other )
{
    // x <= other.x
    if ((*this).cmp(other) <=0)
        return 1;
    return 0;
}
inline bool HugeN:: operator > (const HugeN other )
{
    // x > other.x
    if ((*this).cmp(other) == 1)
        return 1;
    return 0;
}

inline bool HugeN:: operator >= (const HugeN other )
{
    // x >= other.x
    if ((*this).cmp(other) >= 0)
        return 1;
    return 0;
}
inline bool HugeN:: operator == (const HugeN other )
{
    // x == other.x
    if ((*this).cmp(other) == 0)
        return 1;
    return 0;
}
inline bool HugeN:: operator != (const HugeN other )
{
    // x != other.x
    if ((*this).cmp(other) != 0)
        return 1;
    return 0;
}
HugeN HugeN:: operator +(const HugeN other )
{
    // c = a + b;  c = x + other.x
    int i,t;
    HugeN c;
    c.x[0] = x[0] > other.x[0] ? x[0] : other.x[0];

    for ( t=0, i = 1; i <= c.x[0]; i++)
    {
        t =x[i]+other.x[i]+t;
        c.x[i] = t % 10;
        t /= 10;
    }
    if (t)
        c.x[0]++, c.x[c.x[0]] = t;
    return c;
}

HugeN  HugeN:: operator += (const HugeN other )
{
    // a = a + b;  x = x + other.x
    int i,t;
    //x[0] = x[0] > other.x[0] ? x[0] : other.x[0];
    x[0] = max(x[0],other.x[0]);
    for ( t=0, i = 1; i <= x[0]; i++)
    {
        t =x[i]+other.x[i]+t;
        x[i] = t % 10;
        t /= 10;
    }
    if (t)
        x[0]++, x[x[0]] = t;
    return *this;
}
HugeN HugeN:: operator - (const HugeN other )
{
    // c = a + b;  c = x - other.x
    int i,impr,aux;
    HugeN c;
    c.x[0] = max(x[0],other.x[0]);

    for ( impr=0, i = 1; i <= c.x[0]; i++)
    {
        aux =x[i]-other.x[i]-impr;
        if(aux<0)
        {
            aux+=10;
            impr=1;

        }
        else
            impr=0;
        c.x[i] = aux % 10;
    }
    int l=c.x[0];
    while(c.x[l]==0 && l>1)
        l--;
    c.x[0]=l;


    return c;
}

HugeN HugeN::operator /=(int k)
{
    // x = x / k
    int r = 0, i;

    for( i = x[0]; i>0 ; i--)
    {
        r = r * 10 + x[i];
        x[i] = r / k;
        r = r % k;
    }
    while(x[0] > 1 && !x[x[0]])
        --x[0];
    return *this;
}
int HugeN::operator %(long long k)
{
    // r = x % k
    int r = 0, i;

    for( i = x[0]; i>0 ; i--)
    {
        r = r * 10 + x[i];
        r %= k;
    }

    return r;
}

HugeN HugeN::operator * (const HugeN other )
{
    HugeN c;

    c.x[0] = x[0] + other.x[0] - 1;

    int i,j,t;
    for ( i = 1; i<=x[0]; ++i)
        for( j = 1; j <= other.x[0]; ++j)
            c.x[i+j-1] += x[i]*other.x[j];

    t = 0;
    for ( i = 1;i<=c.x[0]; ++i)
       {
           t = t + c.x[i];
           c.x[i] = t % BASE;
           t = t / BASE;
       }
   int l = c.x[0];
   while (t)
      {
         l++; c.x[l] = t%BASE; t/=BASE;
      }
      c.x[0]=l;
      return c;
}
int main()
{
    string n;
    long long d,r;
    fin>>n;
    fin>>d;
    HugeN z(n);
    r=z%d;
    if(r==0)
    {
        z.print();
    }
    else
    {
        d-=r;
        HugeN D(d);
        z+=D;
        z.print();
    }
    return 0;
}