Cod sursa(job #1437476)

Utilizator casuneanu.andreiCasuneanu Andrei Dan casuneanu.andrei Data 17 mai 2015 19:58:35
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 7.25 kb
//
//  main.cpp
//  next
//
//  Created by Casuneanu Andrei on 5/13/15.
//  Copyright (c) 2015 Casuneanu Andrei. All rights reserved.
//

#include <fstream>
using namespace std;
#define IN "next.in"
#define OUT "next.out"

ifstream fin(IN);
ofstream fout(OUT);


class BigInt {
private:
    int lg, dim;
    short *v;
    
    void copie(const short int *src, const int srcDim, short *dst);
    void aloca();
    
public:
    //constructori
    BigInt();
    BigInt(int);
    BigInt(const char *);
    ~BigInt();
    
    //constructor de copiere
    BigInt(const BigInt&);
    
    //operatorul =
    BigInt& operator=(const BigInt&);
    
    //operator indexare
    short operator[](const int) const;
    
    //operatori
    friend BigInt operator+(const BigInt&, const BigInt&);
    friend BigInt operator-(const BigInt&, const BigInt&);
    friend BigInt operator*(const BigInt&, const BigInt&);
    friend BigInt operator/(const BigInt&, const BigInt&);
    friend BigInt operator%(const BigInt&, const BigInt&);
    
    BigInt& operator+=(const BigInt&);
    BigInt& operator-=(const BigInt&);
    BigInt& operator*=(const BigInt&);
    BigInt& operator/=(const BigInt&);
    BigInt& operator%=(const BigInt&);
    
    //verificari
    friend bool operator==(const BigInt&, const BigInt&);
    friend bool operator!=(const BigInt&, const BigInt&);
    friend bool operator<=(const BigInt&, const BigInt&);
    friend bool operator<(const BigInt&, const BigInt&);
    friend bool operator>=(const BigInt&, const BigInt&);
    friend bool operator>(const BigInt&, const BigInt&);
    
    friend bool operator!(const BigInt&);
    
    //afisare
    friend istream& operator >>(istream &i, BigInt &x);
    friend ostream& operator <<(ostream &o,const BigInt &x);
    
    //++ si minus minus
    BigInt operator++(int fictiv);//postfixat
    BigInt& operator++();
    
    BigInt operator--(int fictiv);//postfixat
    BigInt& operator--();
    
    int getlg(){return lg;}
};


int main(){
    BigInt n, d, final;
    fin >>n >>d;
    
    if (n%d != 0)
        final = (n / d + 1) * d;
    else
        final = n;
    fout <<final<<'\n';
    return 0;
}


BigInt::BigInt(){
    lg = 0;
    dim = 0;
    v = NULL;
}

void BigInt::copie(const short *src, const int srcDim, short *dst){
    int i;
    for (i = 0; i < srcDim; ++i)
        dst[i] = src[i];
}

BigInt::BigInt(int x){
    dim = 1;
    lg = 0;
    v = new short[dim];
    while (x){
        v[lg++] = x % 10;
        x /= 10;
        
        if (lg >= dim)
            aloca();
    }
}

BigInt::BigInt(const char *sir){
    dim = 1;
    lg = 0;
    v = new short[dim];
    int n = (int) strlen(sir);
    int i;
    for (i = n - 1; i >= 0; --i){
        v[lg++] = sir[i] - '0';
        
        if (lg >= dim)
            aloca();
    }
}

void BigInt::aloca(){
    dim *= 2;
    short *aux;
    
    aux = new short[dim];
    copie(v, lg, aux);
    delete [] v;
    v = aux;
}

BigInt::BigInt(const BigInt &src){
    lg = src.lg;
    dim = src.dim;
    
    v = new short[dim];
    
    copie(src.v, lg, v);
}

BigInt::~BigInt(){
    delete v;
}

BigInt& BigInt::operator=(const BigInt &src){
    if ((*this) == src)
        return *this;
    lg = src.lg;
    dim = src.dim;
    
    v = new short[dim];
    
    copie(src.v, lg, v);
    return *this;
}

short BigInt::operator[](const int x) const{
    if (x >= lg)
        return 0;
    return v[x];
}

BigInt operator+(const BigInt &a, const BigInt &b){
    BigInt rez;
    int t=0, i;
    
    if (a.lg > b.lg){
        rez.dim = a.dim;
        rez.lg = a.lg;
    }
    else{
        rez.dim = b.dim;
        rez.lg = b.lg;
    }
    
    rez.v = new short[rez.dim];
    
    for (i = 0; i < rez.lg; ++i){
        rez.v[i] = (a[i] + b[i] + t) % 10;
        t = (a[i] + b[i] + t) / 10;
    }
    
    if (t){
        if (rez.lg >= rez.dim)
            rez.aloca();
        rez.v[rez.lg++] = t;
    }
    return rez;
}


BigInt operator-(const BigInt &a, const BigInt &b){
    BigInt rez;
    int t=0, i;
    
    if (a.lg > b.lg){
        rez.dim = a.dim;
        rez.lg = a.lg;
    }
    else{
        rez.dim = b.dim;
        rez.lg = b.lg;
    }
    
    rez.v = new short[rez.dim];
    
    for (i = 0; i < rez.lg; ++i){
        rez.v[i] = (a[i] - b[i] + t);
        if (rez.v[i] < 0){
            rez.v[i] += 10;
            t = -1;
        }
        else{
            rez.v[i] %= 10;
            t = 0;
        }
    }
    
    while (rez.v[rez.lg-1]==0 && rez.lg > 1)
        --rez.lg;
    
    return rez;
}

BigInt operator*(const BigInt &a, const BigInt &b){
    BigInt rez;
    rez.dim = a.dim + b.dim;
    rez.lg = a.lg + b.lg - 1;
    rez.v = new short[rez.dim];
    
    int i, j, t = 0;
    
    for (i = 0; i <= rez.lg; ++i)
        rez.v[i] = 0;
    
    for (i = 0; i < a.lg; ++i)
        for (j = 0; j < b.lg; ++j)
            rez.v[i+j] += a[i] * b[j];
    
    for (i = 0; i < rez.lg; ++i){
        t = (rez.v[i]+=t) / 10;
        rez.v[i] %= 10;
    }
    
    if (t){
        if (rez.lg >= rez.dim)
            rez.aloca();
        rez.v[rez.lg++] = t;
    }
    return rez;
}

BigInt operator/(const BigInt &a, const BigInt &b){
    int rez = 0;
    BigInt aux = a;
    while (aux >= b){
        aux -= b;
        ++rez;
    }
    BigInt final = rez;
    return final;
}

BigInt operator%(const BigInt &a, const BigInt &b){
    BigInt aux = a;
    while (aux >= b)
        aux -= b;
    return aux;
}


bool operator==(const BigInt &a, const BigInt &b){
    if (a.lg != b.lg)
        return false;
    
    int i;
    for (i = 0; i < a.lg; ++i)
        if (a[i] != b[i])
            return false;
    
    return true;
}

bool operator!=(const BigInt &a, const BigInt &b){
    return !(a==b);
}

bool operator<=(const BigInt &a, const BigInt &b){
    
    if (a.lg < b.lg)
        return true;
    else if (a.lg > b.lg)
        return false;
    
    int i;
    for (i = 0; i < a.lg; ++i)
        if (a[i] > b[i])
            return false;
    
    return true;
}

bool operator<(const BigInt &a, const BigInt &b){
    if (a<=b && (a!=b))
        return true;
    return false;
}

bool operator>=(const BigInt &a, const BigInt &b){
    return !(a<b);
}

bool operator>(const BigInt &a, const BigInt &b){
    return !(a<=b);
}

istream& operator >>(istream &i, BigInt &x){
    
    char y[100];
    i >>y;
    
    BigInt aux(y);
    x = aux;
    
    return i;
}

ostream& operator <<(ostream &o,const BigInt &x){
    int i;
    for (i = x.lg - 1; i >= 0; --i)
        o <<x[i];
    return o;
}

BigInt& BigInt::operator+=(const BigInt &x){
    *this = *this + x;
    return *this;
}

BigInt& BigInt::operator-=(const BigInt &x){
    *this = *this - x;
    return *this;
}

BigInt& BigInt::operator*=(const BigInt &x){
    *this = *this * x;
    return *this;
}

BigInt& BigInt::operator/=(const BigInt &x){
    *this = *this / x;
    return *this;
}

BigInt& BigInt::operator%=(const BigInt &x){
    *this = *this % x;
    return *this;
}

BigInt BigInt::operator++(int fictiv){
    BigInt copie = *this;
    (*this) += 1;
    return copie;
}

BigInt& BigInt::operator++(){
    (*this) += 1;
    return *this;
}

BigInt BigInt::operator--(int fictiv){
    BigInt copie = *this;
    (*this) -= 1;
    return copie;
}

BigInt& BigInt::operator--(){
    (*this) -= 1;
    return *this;
}

bool operator!(const BigInt &x){
    if (x==0)
        return 1;
    else
        return 0;
}