Cod sursa(job #1728363)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 12 iulie 2016 19:58:25
Problema Next Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.82 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

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

typedef long long ll;

#define A (*this)
class Huge : protected vector < ll > {

protected:

	static const ll base = 1000000000, nBase = 9;

public:

	Huge() {
		this->resize(100);
	}

	Huge(ll x) {
		this->resize(100);
		A = x;
	}

	Huge(char* s) {
		this->resize(100);
		A = s;
	}

	void operator = (ll x) {
		for (A[0] = 0; x; x /= base)
			A[++A[0]] = x % base;
	}

	void operator = (char *s) {
		A[0] = 0;
		for (ll i = strlen(s); i > 0; i -= nBase) {
			++A[0];
			for (ll j = max(0ll, i - nBase); j < i; ++j)
				A[A[0]] = A[A[0]] * 10 + s[j] - '0';
		}
	}

	void print(void) {
		if (A[0] == 0) {
			fout << 0;
			return;
		}
		fout << A[A[0]];
		for (ll i = A[0] - 1; i > 0; --i) {
			ll p = base / 10;
			while (p > A[i] && p > 1) {
				fout << 0;
				p /= 10;
			}
			fout << A[i];
		}
	}

	bool operator < (const Huge &B) {
		if (A[0] != B[0])
			return A[0] < B[0];
		for (ll i = A[0]; i; --i) {
			if (A[i] < B[i]) return true;
			if (B[i] < A[i]) return false;
		}
		return true;
	}

	Huge operator + (const Huge &B) {
		ll i, t = 0;
		Huge C;
		for (i = 1; i <= A[0] || i <= B[0] || t; ++i, t /= base) {
			t += (i <= A[0] ? A[i] : 0);
			t += (i <= B[0] ? B[i] : 0);
			C[i] = t % base;
		}
		C[0] = i - 1;
		return C;
	}

	Huge operator - (const Huge &B) {
		Huge C = A;
		ll i, t = 0;
		for (i = 1; i <= A[0]; ++i) {
			C[i] -= (i <= B[0] ? B[i] : 0) + t;
			t = 0;
			if (C[i] < 0) C[i] += base, t = 1;
		}
		while (C[0] > 1 && C[C[0]] == 0)
			--C[0];
		return C;
	}

	Huge operator * (ll x) {
		Huge C = A;
		ll t = 0;
		for (ll i = 1; i <= C[0]; ++i) {
			t = 1LL * C[i] * x + t;
			C[i] = t % base;
			t /= base;
		}
		while (t) {
			C[++C[0]] = t % base;
			t /= base;
		}
		return C;
	}

	Huge operator * (const Huge &B) {
		Huge C;
		for (ll i = 1; i <= A[0]; ++i) {
			ll t = 0; ll j;
			for (j = 1; j <= B[0] || t; ++j, t /= base) {
				t += C[i + j - 1] + (j <= B[0] ? 1LL * A[i] * B[j] : 0);
				C[i + j - 1] = t % base;
			}
			if (i + j - 2 > C[0])
				C[0] = i + j - 2;
		}
		return C;
	}

	Huge operator / (ll x) {
		Huge C;
		C = A;
		ll R = 0;
		for (ll i = A[0]; i; --i) {
			R = R * base + A[i];
			C[i] = ll(R / x);
			R %= x;
		}
		while (C[0] > 1 && C[C[0]] == 0)
			--C[0];
		return C;
	}

	ll operator % (ll x) {
		ll R = 0;
		for (ll i = A[0]; i; --i) {
			R = R * base + A[i];
			R %= x;
		}
		return (ll)R;
	}

};

char s[1000005];
ll d;

int main() {

	fin >> s >> d;

	Huge n(s);

	Huge cur; cur = n / d;
	cur = cur * d;

	if (cur < n)
		cur = cur + d;

	cur.print();

	return 0;

}

//Trust me, I'm the Doctor!