Pagini recente » Cod sursa (job #1059153) | Cod sursa (job #1555372) | Cod sursa (job #1843548) | Cod sursa (job #479860) | Cod sursa (job #2440138)
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>`
using namespace std;
ifstream fin("next.in");
ofstream fout("next.out");
const int BASE = 10;
const int MAXDIGIT = 1000001;
class HugeN {
private:
int x[MAXDIGIT];
public:
HugeN() {
memset(x,0,sizeof(x));
x[0]=1;
}
HugeN(int n) {
memset(x,0,sizeof(x));
do {
++x[0];
x[x[0]]=n%10;
n=n/10;
} while(n!=0);
}
HugeN(char s[MAXDIGIT]) {
int n,i;
memset(x,0,sizeof(x));
n=strlen(s);
x[0]=n;
for(i = n-1; i>=0; i--)
x[n-i] = s[i] - '0';
}
HugeN(const HugeN & other) { // & optimizeaza timpul de executie
memcpy(x,other.x,sizeof(other.x));
//supraincarcare pt a copia
}
void print() {
int i;
for(int i = x[0]; i>=1; i--)
fout<<x[i];
fout<<"\n";
}
int cmp(const HugeN & other) {
if(x==other.x) {
for(int i = x[0]; i>=1; i--)
if(x[i]>other.x[i])
return 1;
else if(x[i]<other.x[i])
return -1;
return 0;
}
if(x[0]>other.x[0])
return 1;
else
return -1;
}
//Supraincarcarea operatorilor
bool operator < (const HugeN & other);
bool operator > (const HugeN & other);
bool operator <= (const HugeN & other);
bool operator >= (const HugeN & other);
bool operator == (const HugeN & other);
bool operator != (const HugeN & other);
HugeN operator + (const HugeN & other);
HugeN operator - (const HugeN & other);
HugeN operator * (const HugeN & other);
HugeN operator /= (int k);
HugeN operator += (const HugeN & other);
HugeN operator -= (const HugeN & other);
HugeN operator *= (int k);
HugeN operator / (int k);
HugeN operator += (long long k);
long long operator % (long long k);
};
bool HugeN :: operator < (const HugeN & other) {
// x < other.x
if((*this).cmp(other) == -1) // *this = obiectul curent , this adresa obiectului curent
return true;
else
return false;
}
bool HugeN :: operator <= (const HugeN & other) {
if((*this).cmp(other) > 0)
return false;
return true;
}
bool HugeN :: operator > (const HugeN & other) {
if((*this).cmp(other) == 1)
return true;
return false;
}
bool HugeN :: operator >= (const HugeN & other) {
if((*this).cmp(other) == -1)
return false;
return true;
}
bool HugeN :: operator == (const HugeN & other) {
if((*this).cmp(other) == 0)
return true;
return false;
}
bool HugeN :: operator != (const HugeN & other) {
if((*this) == other)
return false;
return true;
}
HugeN HugeN :: operator + (const HugeN & other) {
HugeN temp;
int t = 0, k;
int maxv = max(x[0],other.x[0]);
temp.x[0]=maxv;
for(int i = 1 ; i <= maxv; i++) {
k = (x[i] + other.x[i] + t);
temp.x[i] = k % 10;
t = k/10;
}
if(t!=0) {
temp.x[0]++;
k=temp.x[0];
temp.x[k] = t;
}
return temp;
}
HugeN HugeN :: operator += (const HugeN & other) {
// a + = b
int i,t,k;
x[0] = max(x[0],other.x[0]);
for(t = 0,i = 1; i <= x[0]; i++) {
k=x[i];
+ other.x[i] + t;
x[i] = k % 10;
t = k /10;
}
if(t) {
x[0]++;
x[x[0]] = t;
}
return *this;
}
HugeN HugeN :: operator *= (int k) {
//huge inmultit cu un int
if(k == 0) {
x[0] = 1;
x[1] = 0;
return *this;
}
int t = 0,i,aux;
for(i = 1; i<=x[0]; i++)
x[i]=x[i]*k;
for(i=1; i<=x[0]; i++) {
aux = x[i] + t;
x[i] = aux % 10;
t = aux / 10;
}
aux = x[0];
while(t) {
aux++;
x[aux] = t % 10;
t = t /10;
}
x[0] = aux;
return *this;
}
HugeN HugeN :: operator - (const HugeN & other) {
HugeN temp;
int tr = 0,k;
for(int i = 1; i <= x[0]; i++) {
k = x[i]- other.x[i] - tr;
if(k<0) {
tr=1;
k = k + BASE;
} else
tr = 0;
}
temp.print();
while(temp.x[x[0]] == 0 && x[0]!=1)
temp.x[0]--;
return temp;
}
HugeN HugeN :: operator * (const HugeN & other) {
//inmltirea a 2 nr huge
HugeN c;
if(x[1] == 0 || other.x[1] == 0) {
c.x[0]=1;
c.x[1]=0;
return c;
}
int i,t,j,k;
c.x[0] = x[0] + other.x[0] - 1;
for(i = 1 ; i <= x[0]; i++)
for(j=1; j<=other.x[0]; j++)
c.x[i+j-1] += x[i] * other.x[j];
t = 0;
for(i = 1; i <= c.x[0]; i++) {
k=c.x[i]+t;
c.x[i] = k%10;
t = k / 10;
}
k = c.x[0];
while(t) {
k++;
c.x[k] = t % 10;
t = t / 10;
}
c.x[0] = k;
return c;
}
long long HugeN :: operator % (long long k) {
long long i,r;
r=0;
for(i=x[0]; i>=1; i--) {
r = r * 10 + x[i];
r = r % k;
}
return r;
}
HugeN HugeN :: operator /= (int k) {
int i,r=0;
for(int i = x[0]; i >= 1; i--) {
r = r * 10 + x[i];
x[i] = r / k;
r = r % k;
}
while(x[0]>1 && x[x[0]]==0)
x[0]--;
return *this;
}
HugeN HugeN :: operator += (long long k)
{
long long tr = k;
for(int i = 1; i<= x[0]; i++)
{
tr = x[i] + tr;
x[i] = tr % 10;
tr = tr / 10;
}
while(tr)
{
x[0]++;
x[x[0]] = tr % 10;
tr = tr / 10;
}
return *this;
}
int main() {
char s[MAXDIGIT];
fin.getline(s,MAXDIGIT);
HugeN n(s);
long long d,r;
fin>>d;
r = n % d;
if(r == 0)
n.print();
else
{
d = d - r;
n+=d;
n.print();
}
return 0;
}