Pagini recente » Profil susneamaria | Cod sursa (job #1008645) | Cod sursa (job #2746643) | Cod sursa (job #3191843) | Cod sursa (job #2769940)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("next.in");
ofstream fout("next.out");
const int base = 10;
using hint = int;
class HugeN {
private:
bool sign;
vector <hint> v;
int cmp(const HugeN& other) {
if(v.size() != other.v.size()) return v.size() < other.v.size() ? 1 : -1;
int k = v.size();
while(k && v[k - 1] == other.v[k - 1]) k--;
if(k == 0) return 0;
return v[k - 1] < other.v[k - 1] ? 1 : -1;
}
public:
HugeN(hint x = 0) : sign(false) {
if(x < 0) sign = true, x = -x;
do {
v.push_back(x % base);
x /= base;
} while(x);
}
HugeN(vector <hint> _v, bool _sign = false) : v(_v), sign(_sign) {}
friend istream& operator >>(istream& in, HugeN& a) {string s; in >> s; if(s[0] == '-') a.sign = true, s.erase(s.begin()); int len = s.length(); a.v.resize(len); for(int i = 0; i < len; i++) a.v[len - 1 - i] = s[i] - '0'; return in;}
friend ostream& operator <<(ostream& out, const HugeN& a) {if(a.sign) out << "-"; for(int i = a.v.size() - 1; i >= 0; i--) out << a.v[i]; return out;}
inline bool operator <(const HugeN& other) {return cmp(other) == 1;}
inline bool operator >=(const HugeN& other) {return cmp(other) <= 0;}
inline bool operator >(const HugeN& other) {return cmp(other) == -1;}
inline bool operator <=(const HugeN& other) {return cmp(other) >= 0;}
HugeN operator +(const HugeN& other) {
bool s = sign & other.sign;
if(sign != other.sign) {
sign ^= 1;
HugeN c = (*this) - other;
c.sign ^= 1;
sign ^= 1;
return c;
}
vector <hint> c(max(v.size(), other.v.size()));
int i, carry, k = min(v.size(), other.v.size()), kk = c.size();
for(i = 0, carry = 0; i < k; i++)
c[i] = (v[i] + other.v[i] + carry) % base,
carry = (v[i] + other.v[i] + carry) / base;
if(v.size() > k)
for(i = k; i < kk; i++)
c[i] = (v[i] + carry) % base,
carry = (v[i] + carry) / base;
else for(i = k; i < kk; i++)
c[i] = (other.v[i] + carry) % base,
carry = (other.v[i] + carry) / base;
if(carry)
c.push_back(carry);
return HugeN(c, s);
}
HugeN operator +=(const HugeN& other) { return *this = *this + other; }
HugeN operator -(const HugeN& other) {
bool s = sign & other.sign;
if(sign != other.sign) {
sign ^= 1;
HugeN c = (*this) + other;
c.sign ^= 1;
sign ^= 1;
return c;
}
vector <hint> c(max(v.size(), other.v.size()));
int i, complement, k = min(v.size(), other.v.size()), kk = c.size();
if(*this >= other) {
for(i = 0, complement = 0; i < k; i++)
if(v[i] - complement < other.v[i]) c[i] = v[i] - complement - other.v[i] + base, complement = 1;
else c[i] = v[i] - complement - other.v[i], complement = 0;
if(v.size() > k) for(i = k; i < kk; i++)
c[i] = v[i] - complement, complement = 0;
else for(i = k; i < kk; i++)
c[i] = - complement - other.v[i] + base, complement = 1;
} else {
s ^= 1;
for(i = 0, complement = 0; i < k; i++)
if(other.v[i] - complement < v[i]) c[i] = other.v[i] - complement - v[i] + base, complement = 1;
else c[i] = other.v[i] - complement - v[i], complement = 0;
if(other.v.size() > k) for(i = k; i < kk; i++)
c[i] = other.v[i] - complement, complement = 0;
else for(i = k; i < kk; i++)
c[i] = - complement - v[i] + base, complement = 1;
}
if(complement) s ^= 1;
while(kk > 1 && c[k - 1] == 0) kk--;
c.resize(kk);
return HugeN(c, s);
}
HugeN operator -=(const HugeN& other) { return *this = *this - other; }
HugeN operator *(long long x) {
if(x == 0) return 0;
bool s = sign ^ (x < 0);
if(x < 0) x = -x;
long long carry = 0, k = v.size(); vector <hint> p(k);
for(int i = 0; i < k; i++)
carry += x * v[i],
p[i] = carry % base,
carry /= base;
while(carry)
p.push_back(carry % base),
carry /= base;
return HugeN(p, s);
}
long long operator %(long long d) {
long long r = 0;
for(int i = v.size() - 1; i >= 0; i--)
r = r * base + v[i],
r %= d;
return sign ? -r : r;
}
};
int main()
{
HugeN a; long long b;
fin >> a >> b;
long long c = a % b;
//cout << c << " " << a;
if(c) a += HugeN(b - c);
fout << a;
return 0;
}