Cod sursa(job #2440116)

Utilizator ipop20Ioana Popescu ipop20 Data 17 iulie 2019 16:37:32
Problema Next Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 5.49 kb
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream fin ("next.in");
ofstream fout ("next.out");
const int MAX_DIGITS=1000002;
const int BASE=10;
class HugeN
{
    private: int x[MAX_DIGITS];
    public: HugeN()
        {
        memset(x,0,sizeof(x));
        x[0]=1;
        }
      HugeN(long long n)
      {
          memset(x,0,sizeof(x));
          do
          {
              ++x[0];
              x[x[0]]=n%10;
              n=n/10;
          }while(n>0);
      }
      HugeN(const HugeN&other)
      {
          memcpy(x,other.x,sizeof(other.x));
      }
      HugeN (char s[MAX_DIGITS])
      {
          int n,i;
          memset (x,0,sizeof(x));
          n=strlen(s);
          x[0]=n;
          for(i=n-1;i>=0;i--)
            x[n-i]=s[i]-'0';
      }
      void print()
      {
          int i;
          for(i=x[0];i>=1;i--)
              fout<<x[i];
          fout<<"\n";
      }
     int cmp (const HugeN&other)
     {
      /* = devine 0
         < devine -1
         > devine 1   */
      if(x[0]<other.x[0]) return -1;
      else
        if(x[0]>other.x[0]) return 1;
        else
            {for(int i=x[0];i>=1;i--)
                {
                  if(x[i]<other.x[i]) return -1;
                  else
                        if(x[i]>other.x[i]) return 1;
                }
             return 0;
            }
     }
    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 * (const HugeN& other);
    HugeN operator * (int k);
    HugeN operator / (int k);
    HugeN operator += (const HugeN& other);
    HugeN operator += (long long& other);
    HugeN operator -= (const HugeN& other);
    HugeN operator *= (int k);
    HugeN operator /= (int k);
    long long operator %(long long k);
};

 bool HugeN::operator < (const HugeN& other)
 {
     if((*this).cmp(other)==-1) return 1;
     return 0;
 }
 bool HugeN::operator <= (const HugeN& other)
 {
     if((*this).cmp(other)<=0) return 1;
     return 0;
 }
 bool HugeN::operator > (const HugeN& other)
 {
     if((*this).cmp(other)==1) return 1;
     return 0;
 }
 bool HugeN::operator >= (const HugeN& other)
 {
     if((*this).cmp(other)>=0) return 1;
     return 0;
 }
 bool HugeN::operator == (const HugeN& other)
 {
     if((*this).cmp(other)==0) return 1;
     return 0;
 }
 bool HugeN::operator != (const HugeN& other)
 {
     if((*this).cmp(other)!=0) return 1;
     return 0;
 }
HugeN HugeN::operator + (const HugeN& other)
{
    HugeN temp;
    int trecere=0,d;
    temp.x[0]=max(x[0],other.x[0]);
    for(int i=1;i<=temp.x[0];i++)
    {
        d=x[i]+other.x[i]+trecere;
        temp.x[i]=d%10;
        trecere=d/10;
    }
    if(trecere>0)
        temp.x[++temp.x[0]]=trecere;
    return temp;
}
HugeN HugeN::operator - (const HugeN& other)
{
    HugeN temp;
    int trecere=0,d;
    temp.x[0]=x[0];
    for(int i=1;i<=temp.x[0];i++)
    {
        d=x[i]-other.x[i]-trecere;
        if(d<0)
        {
            d=d+10;
            trecere=1;
        }
        else
            trecere=0;
        temp.x[i]=d;
    }
    while(temp.x[temp.x[0]]==0 && temp.x[0]>=1)
        temp.x[0]--;
    return temp;
}
HugeN HugeN::operator *= (int k)
{
    int tr=0,i,aux;
    for(i=1;i<=x[0];i++)
    {
        x[i]=x[i]*k;
    }
    for(i=1;i<=x[0];i++)
    {
        aux=x[i]+tr;
        x[i]=aux%10;
        tr=aux/10;
    }
    aux=x[0];
    while(tr)
    {
        aux++;
        x[aux]=tr%10;
        tr=tr/10;
    }
    x[0]=aux;
    return (*this);
}
HugeN HugeN::operator += (const HugeN& other)
{
  int i,t,k;
  x[0]=max(x[0],other.x[0]);
  for(t=0,i=1;i<=x[0];i++)
  {
      k=x[i]+other.x[i]+t;
      x[i]=k%10;
      t=k/10;
  }
  if(t)
  {
      x[0]++;
      x[x[0]]=t;
  }
  return (*this);
}
HugeN HugeN::operator += (long long& other)
{
  int i,t,k;
  t=other;
  for(i=1;i<=x[0];i++)
  {
      k=x[i]+t;
      x[i]=k%10;
      t=k/10;
  }
  if(t)
  {
      x[0]++;
      x[x[0]]=t;
  }
  return (*this);
}
HugeN HugeN::operator * (const HugeN& other)
{
    HugeN c;
    int i,j,tr,k;
    c.x[0]=x[0]+other.x[0]-1;
    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];
    tr=0;
    for(i=1;i<=c.x[0];i++)
    {
        k=c.x[i]+tr;
        c.x[i]=k%10;
        tr=k/10;
    }
    k=c.x[0];
    while(tr)
    {
        k++;
        c.x[k]=tr%10;
        tr=tr/10;
    }
    c.x[0]=k;
    return c;
}
long long HugeN::operator % (long long k)
{
 long long i,r=0;
 for(i=x[0];i>=1;i--)
 {
     r=r*10+x[i];
     r=r%k;
 }
 return r;
}
HugeN HugeN::operator / (int k)
{
 int i,r=0;
 HugeN cat;
 cat.x[0]=x[0];
 for(i=x[0];i>=1;i--)
 {
     r=r*10+x[i];
     cat.x[i]=r/k;
     r=r%k;
 }
 while(cat.x[cat.x[0]]==0)
        cat.x[0]--;
 if(cat.x[0]==0)
       cat.x[0]=1;
 return cat;
}

int main()
{
    char s[MAX_DIGITS];
    fin.getline(s,MAX_DIGITS);
    HugeN n(s);
    long long d,rest;
    fin>>d;
    rest=n%d;
    if(rest==0)
        n.print();
    else{
        rest=d-rest;
        HugeN m(rest);
        m+=n;
        m.print();
    }
    return 0;
}