Cod sursa(job #1958282)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 8 aprilie 2017 11:17:22
Problema Next Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std ;

class Huge : protected vector<int>{

private:

    static const int SIZE = 400002;
    static const int BASE = 1000;
    static const int EXP = 3;

public:

    inline Huge();
    inline Huge(char N[]);

    inline void print();

    inline void operator *= (long long B);
    inline void operator /= (long long B);
    inline long long  operator %  (long long B);

};

#define A (*this)

inline Huge :: Huge(){
    A.resize(SIZE);
}

inline Huge :: Huge(char N[]){

    A.resize(SIZE); A[0] = 0;

    for(int length = strlen(N); length > 0; length -= EXP){

        A[0]++;

        for(int i = max(0, length - EXP); i < length; i++){
            A[A[0]] = A[A[0]] * 10 + N[i] - '0';
        }
    }
}

inline void Huge :: print(){

    printf("%d", A[A[0]]);

    for(int i = A[0] - 1; i > 0; i--){
        printf("%03d", A[i]); // %0EXPd
    }//printf("\n");
}


inline void Huge :: operator *= (long long B){

    int i; long long carry = 0;

    for(i = 1; i <= A[0] || carry; i++, carry /= BASE)
        A[i] = (carry += ((i <= A[0]) ? A[i] : 0) * B) % BASE;
    A[0] = i - 1;
}


inline void Huge :: operator /= (long long B){

    long long carry = 0;

    for(int i = A[0]; i > 0; i--, carry %= B)
        A[i] = (carry = carry * BASE + A[i]) / B;

    for(; A[0] > 1 && !A[A[0]]; A[0]--);

    A[1]++;
}

inline long long Huge :: operator % (long long x){

        long long  R = 0;
        for (long long i = A[0]; i; --i) {
            R = R * BASE + A[i];
            R %= x;
        }
        return R;
}

#undef A

char numberN[1000020];
long long D;
Huge N;

int main(){

    freopen("next.in", "r", stdin);
    freopen("next.out", "w", stdout);

    scanf("%s", numberN);
    scanf("%lld", &D);

    N = numberN;

    if(N % D == 0){
        printf("%s", numberN);
        return 0;
    }

    N /= D; N *= D;

    N.print();

return 0;
}