Cod sursa(job #2646224)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 31 august 2020 13:58:28
Problema Next Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <cstdio>
#include <cstring>

using namespace std;
char a[1000004];
int n;
long long mod(long long d)
{
    long long md=0;
    int i;
    for(i=n; i>=1; i--)
    {
        md=md*10+a[i];
        md%=d;
    }
    return md;
}
void adun(long long x)
{
    long long c=x,el;
    int i;
    for(i=1; i<=n; i++)
    {
        el=a[i]+c;
        c=el/10;
        a[i]=el%10;
    }
    while(c!=0)
    {
        n++;
        a[n]=c%10;
        c/=10;
    }
}
int main()
{
    int i,j,aux;
    long long d,md;
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    scanf("%s",a);
    n=strlen(a);
    for(i=n; i>=1; i--)
        a[i]=a[i-1]-'0';
    for(i=1,j=n; i<j; i++,j--)
    {
        aux=a[i];
        a[i]=a[j];
        a[j]=aux;
    }
    scanf("%lld",&d);
    md=mod(d);
    if(md!=0)
        adun(d-md);
    for(i=n; i>=1; i--)
        printf("%d",a[i]);
    return 0;
}