Cod sursa(job #39337)
# include <stdio.h>
# include <string.h>
# define _fin "next.in"
# define _fout "next.out"
# define maxn 1000001
short n[maxn], ad[maxn];
long long d, r;
char buf[maxn];
int i;
void add(short a[], short b[])
{
short i, t=0;
for (i=1; i<=a[0]||i<=b[0]||t; i++, t/=10)
a[i] = ( t += (a[i]+b[i]) ) % 10;
a[0]=i-1;
}
long long mod(short a[], long long b)
{
int i;
long long t=0;
for(i=a[0]; i>0; i--)
t = ( t*10 + a[i] ) % b;
return t;
}
int main()
{
freopen(_fin, "r", stdin);
freopen(_fout,"w", stdout);
scanf("%s\n%lld", buf, &d);
for (i=strlen(buf)-1; i>=0; i--) n[++n[0]]=buf[i]-0x30;
r = mod(n, d);
if ( r ) {
d=d-r;
while ( d ) ad[++ad[0]]=short(d%10), d/=10;
add(n, ad);
}
for (i=n[0]; i>=1; i--) printf("%d", n[i]);
printf("\n");
return 0;
}