Pagini recente » Cod sursa (job #2052707) | Cod sursa (job #3243) | Cod sursa (job #13398) | Cod sursa (job #2770281)
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int max_digits=1e6+10;
const int base=10;
const int nmax=1e6+10;
char s[nmax];
class huge
{
public:
short x[max_digits];
public:
huge()
{
memset(x,0,sizeof(x));
x[0]=1;
}
huge(long long n)
{
memset(x,0,sizeof(x));
do
{
x[++x[0]]=n%base;
n/=base;
}
while(n);
}
void print()
{
for(int i=x[0]; i>=1; i--)
out<<x[i];
out<<"\n";
}
huge operator +(const huge &other);
huge operator -(const huge &other);
huge operator *(int k);
huge operator /(long long k);
long long operator %(long long k);
};
huge huge :: operator +(const huge &other)
{
int i,tr,aux;
huge c;
c.x[0]=max(x[0],other.x[0]);
for(tr=0,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[++c.x[0]]=tr;
return c;
}
huge huge :: operator -(const huge &other)
{
huge c;
c.x[0]=max(x[0],c.x[0]);
int i,impr,aux;
for(impr=0,i=1; i<=c.x[0]; i++)
{
aux=x[i]-other.x[i]-impr;
if(aux<0)
{
c.x[i]=aux+base;
impr=1;
}
else
{
c.x[i]=aux;
impr=0;
}
}
while(c.x[0]>1 && c.x[c.x[0]]==0)
c.x[0]--;
return c;
}
huge huge :: operator *(int k)
{
huge c;
int i,aux,tr;
c.x[0]=x[0];
for(i=1; i<=c.x[0]; i++)
c.x[i]=x[i]*k;
for(tr=0,i=1; i<=c.x[0]; i++)
{
aux=c.x[i]+tr;
c.x[i]=aux%base;
tr=aux/base;
}
while(tr)
{
c.x[++c.x[0]]=tr%base;
tr/=base;
}
return c;
}
huge huge :: operator /(long long k)
{
huge c;
int i;
long long r;
c.x[0]=x[0];
for(r=0,i=c.x[0]; i>=1; i--)
{
r=1ll*(r*base+x[i]);
c.x[i]=r/k;
r=1ll*(r%k);
}
while(c.x[0]>1 && c.x[c.x[0]]==0)
c.x[0]--;
return c;
}
long long huge :: operator %(long long k)
{
int i;
long long r;
for(r=0,i=x[0]; i>=1; i--)
r=(r*base+x[i])%k;
return r;
}
int main()
{
in.getline(s,nmax);
huge nr;
int n,i;
long long d;
in>>d;
n=strlen(s);
out<<s<<"\n";
out<<d;
nr.x[0]=n;
for(i=0; i<n; i++)
nr.x[n-i]=s[i]-'0';
if(nr%d==0)
nr.print();
else
{
huge aux(d-nr%d);
nr=nr+aux;
nr.print();
}
return 0;
}