Pagini recente » Cod sursa (job #2527466) | Cod sursa (job #1173931) | Cod sursa (job #752645) | Cod sursa (job #618538) | Cod sursa (job #2770699)
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("next.in");
ofstream cout("next.out");
const int MAX_DIGITS=1000003;
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));
x[0]=s.size();
for (int 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 *= (int n);
HUGE operator / ( int n);
void operator /=( int n);
long long operator % ( long long n);
void operator = (const HUGE &other);
};
void HUGE :: operator = (const HUGE &other){
memcpy(x,other.x, sizeof(other.x));
}
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;
}
}
void HUGE :: operator /=(int k){
int i, t = 0;
for (i = x[0]; i > 0; i--, t%=k){
t = t * 10 + x[i];
x[i] = t / k;
}
for (; x[0] > 1 and !x[x[0]]; x[0]--);
}
long long HUGE :: operator % ( long long k) {
int i, t = 0;
for (i = x[0]; i > 0; i--)
t = (t * 10 + x[i]) % k;
return t;
}
string s;
int main(){
cin>>s;
HUGE a(s);
long long d;
cin>>d;
long long r= (a%d);
if (!r)
a.print();
else {
r=d-r;
HUGE aux(r);
a+=aux;
a.print();
}
}