Pagini recente » Cod sursa (job #3240674) | Cod sursa (job #2770694) | Cod sursa (job #3031819) | Cod sursa (job #2167304) | Cod sursa (job #2770696)
#include <fstream>
//#include <iostream>
#include <climits>
#include <cctype>
#include <cstring>
using namespace std;
ifstream cin("next.in");
ofstream cout("next.out");
const int MAX_DIGITS=104166;
const int BASE=10;
class huge
{
private :
int x[MAX_DIGITS];
public :
//constructori
huge()
{
memset (x,0, sizeof(x));
x[0]=1;
}
huge (const string s)
{
memset(x,0,sizeof(x));
int i,j;
for (i=1; s[i-1]=='0' and i!=s.size(); ++i);
x[0]=s.size()-i+1;
for (j=s.size()-1,i=1; i<=x[0]; ++i,--j)
x[i]=s[j]-'0';
}
huge(int n)
{
memset(x,0,sizeof(x));
x[0]=0;
do
{
++x[0];
x[x[0]]=n%10;
n/=10;
}
while (n);
}
huge(const huge &other)
{
memcpy(x,other.x, sizeof(other.x));
}
int cmp (const huge &other)
{
if (x[0]> other.x[0])
return 1;
if (x[0]<other.x[0])
return -1;
for (int i=x[0]; i; --i)
{
if (x[i]>other.x[i])
return 1;
if (x[i]<other.x[i])
return -1;
}
return 0;
}
void print ()
{
for (int i=x[0]; i>0; --i)
cout<<x[i];
cout<<"\n";
}
huge operator + (const huge &other);
void operator += (const huge &other);
huge operator - (const huge &other);
void operator -=(const huge &other);
huge operator + ( int n);
void operator +=( int n);
huge operator * ( int n);
huge operator *(const huge &other);
void operator *= (const huge &other);
void operator *= ( int n);
huge operator / ( int n);
huge operator /(const huge &other);
void operator /=( int n);
int operator % ( int n);
bool operator == (const huge &other);
bool operator < (const huge &other);
bool operator> (const huge &other);
bool operator<= (const huge &other);
bool operator >=(const huge &other);
bool operator !=(const huge &other);
huge operator ^ (int n);
void operator ^= (int n);
};
huge huge::operator +(int n)
{
huge a(n);
a+=(*this);
return a;
}
void huge::operator +=(int n)
{
huge a(n);
(*this)+=a;
}
huge huge::operator ^(int n)
{
huge bb(*this), p(1);
for (; n; n>>=1)
{
if (n&1)
p=p*bb;
bb=bb*bb;
}
return p ;
}
void huge::operator ^=(int n)
{
huge p(1);
for (; n; n>>=1)
{
if (n&1)
p*=(*this);
*this*=(*this);
}
*this=p;
}
bool huge :: operator == (const huge &other)
{
return (*this). cmp( other) ==0 ;
}
bool huge :: operator < (const huge &other)
{
return (*this). cmp( other) ==-1 ;
}
bool huge :: operator > (const huge &other)
{
return (*this). cmp( other) ==1 ;
}
bool huge :: operator <=(const huge &other)
{
return (*this). cmp( other) <=0;
}
bool huge :: operator >=(const huge &other)
{
return (*this). cmp( other) >=0;
}
bool huge :: operator !=(const huge &other)
{
return (*this). cmp( other) !=0;
}
huge huge::operator + (const huge &other)
{
//c.x = x+ other.x
huge c;
int i,tr=0, aux;
c.x[0]=max(x[0],other.x[0]);
for (i=1; i<=c.x[0]; i++)
{
aux=x[i]+other.x[i]+tr;
c.x[i]=aux%BASE;
tr=aux/BASE;
}
if (tr)
{
c.x[0]++;
c.x[c.x[0]]=tr;
}
return c;
}
void huge:: operator += (const huge&other)
{
//x=x+other.x
int i,tr, aux;
x[0]=max(x[0], other.x[0]);
for (i=1,tr=0; i<=x[0]; ++i)
{
aux=x[i]+other.x[i]+tr;
x[i]=aux%BASE;
tr=aux/BASE;
}
if (tr)
{
x[0]++;
x[x[0]]=tr;
}
}
huge huge::operator - (const huge &other)
{
//c.x = x- other.x
huge c;
int i,imprumut=0, aux;
c.x[0]=max(x[0],other.x[0]);
for (i=1; i<=c.x[0]; i++)
{
aux=x[i]-other.x[i]-imprumut;
c.x[i]=aux;
if (c.x[i]<0)
{
c.x[i]+=BASE;
imprumut=1;
}
else
imprumut=0;
}
while (c.x[c.x[0]]==0 and c.x[0]>1)
{
c.x[0]--;
}
return c;
}
void huge::operator -=(const huge &other)
{
//x.x = x- other.x
int i,imprumut=0, aux;
x[0]=max(x[0],other.x[0]);
for (i=1; i<=x[0]; i++)
{
aux=x[i]-other.x[i]-imprumut;
x[i]=aux;
if (x[i]<0)
{
x[i]+=BASE;
imprumut=1;
}
else
imprumut=0;
}
while (x[x[0]]==0 and x[0]>1)
{
x[0]--;
}
}
huge huge :: operator * (int k )
{
//c=x*k
huge c;
c.x[0]=x[0];
int i,tr=0,aux;
for (i=1; i<=c.x[0]; ++i)
{
aux=x[i]*k+tr;
c.x[i]=aux%BASE;
tr=aux/BASE;
}
while (tr)
{
c.x[0]++;
c.x[c.x[0]]=tr%BASE;
tr/=BASE;
}
return c;
}
void huge :: operator *=(int k )
{
//x*=k
int i,tr=0,aux;
for (i=1; i<=x[0]; ++i)
{
aux=x[i]*k+tr;
x[i]=aux%BASE;
tr=aux/BASE;
}
while (tr)
{
x[0]++;
x[x[0]]=tr%BASE;
tr/=BASE;
}
}
huge huge :: operator * (const huge &other)
{
huge c;
int i,j, tr;
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];
for(i=1,tr=0; i<=c.x[0]; ++i)
{
tr+=c.x[i];
c.x[i]=tr%BASE;
tr/=BASE;
}
while (tr)
{
c.x[0]++;
c.x[c.x[0]]=tr%BASE;
tr/=BASE;
}
if (c.x[c.x[0]]==0)
c.x[0]=1;
return c;
}
void huge :: operator *=(const huge &other)
{
huge c;
c= (*this)* other;
*this= c;
}
void huge :: operator /=(int k)
{
int i, r = 0;
for (i = x[0]; i > 0; i--, r%=k)
{
r = r * 10 + x[i];
x[i] = r / k;
}
for (; x[0] > 1 and !x[x[0]]; x[0]--);
}
huge huge :: operator /(int k)
{
huge c;
int i, r;
c.x[0]=x[0];
for (i = x[0], r = 0; i > 0; i--)
{
r=r*BASE+ x[i];
c.x[i]=r/k;
r%=k;
}
while (c.x[0]>1 and c.x[c.x[0]]==0)
c.x[0]--;
return c;
}
int huge :: operator % ( int k)
{
int i, r;
for ( i=x[0], r=0 ; i>=1; --i)
{
r=r*BASE + x[i];
r%=k;
}
return r;
}
string s;
int main()
{
unsigned long long d;
cin>>s>>d;
huge a(s),ans;
int r= a%d;
if (r==0)
a.print();
else
{
ans=a+d-r;
ans.print();
}
}