Cod sursa(job #2440122)

Utilizator ipop20Ioana Popescu ipop20 Data 17 iulie 2019 16:40:51
Problema Next Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.74 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);
};


HugeN HugeN::operator += (long long& other)
{
  long long 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);
}

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;
}

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;
        n+=rest;
        n.print();
    }
    return 0;
}