Pagini recente » Cod sursa (job #1142331) | Cod sursa (job #1318643) | Cod sursa (job #515051) | Statistici Mircea Goganau (mgoganau) | Cod sursa (job #777540)
Cod sursa(job #777540)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define A (*this)
using namespace std;
ifstream in ("next.in");
ofstream out ("next.out");
class HugeInt : protected vector <int>
{
private:
static const int BASE = 10;
static const int MAXN = 1000010;
public:
HugeInt ();
HugeInt (int);
void operator += (HugeInt &);
long long f (long long);
friend istream& operator >> (istream &in, HugeInt &H){
char s[MAXN];
int len, i;
in >> s;
len = strlen (s);
for (i = len - 1; i >= 0; i --)
H[ ++ H[0] ] = (s[i] - '0');
return in;
}
friend ostream& operator << (ostream &out, HugeInt &H){
while (H[0])
out << H[ H[0] -- ];
return out;
}
};
HugeInt :: HugeInt ()
{
this -> resize (MAXN);
for (int i = 0; i < MAXN; i ++)
A[i] = 0;
}
HugeInt :: HugeInt (int x)
{
this -> resize (MAXN);
while (x){
A[ ++ A[0] ] = x % BASE;
x /= BASE;
}
}
void HugeInt :: operator += (HugeInt &B)
{
int i, t = 0;
for (i = 1; i <= A[0] || i <= B[0] || t; i ++, t /= BASE)
A[i] = (t += A[i] + B[i]) % BASE;
A[0] = i - 1;
}
long long HugeInt :: f (long long D)
{
long long R = 0;
for (int i = A[0]; i; i --)
R = ((long long) (R * 10 + A[i]) % D);
return (long long) R;
}
int main ()
{
HugeInt N;
long long D, R, now;
in >> N;
in >> D;
R = N.f (D);
HugeInt M (D - R);
N += M;
out << N;
return 0;
}