Cod sursa(job #339036)
#include <algorithm>
using namespace std;
#define LG_MAX 1000004
int N[LG_MAX],b[LG_MAX];
long long D,r;
void clean (int b[LG_MAX]) //functie curatare numar
{
int i;
for (i=1;i<=b[0];i++)
b[i]=0;
b[0]=0;
}
long rest (int a[LG_MAX], long b) //functie rest nr%mare nr mic
{
long i,t=0;
for (i=a[0];i>0;i--)
t=(t*10+a[i])%b;
return t;
}
void adunare (int a[LG_MAX], int b[LG_MAX]) //functie aduanre nr mari
{
int i,t;
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;
}
void afisare () //functie bagare in fisier
{
int i;
for (i=N[0]; i; --i)
printf ("%d",N[i]);
}
int main()
{
freopen ("next.in","r",stdin); //fisier intrare
freopen ("next.out","w",stdout); //fisier iesire
int i;
char ch;
while (scanf ("%c",&ch))
{
if (ch=='\n' /*|| ch==' '*/) //citesc caracter cu caracter
break;
b[++b[0]]=ch-'0';
}
N[0]=b[0];
for (i=1; i<=N[0]; ++i)
N[i]=b[b[0]-i+1];
clean (b);
scanf ("%lld",&D); //il iau pe D
r=rest(N,D);
r=(D-r)%D;
while (r)
{
b[++b[0]]=r%10;
r/=10;
}
adunare(N,b);
afisare();
return 0;
}