Cod sursa(job #1618568)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 27 februarie 2016 21:30:27
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <cstdio>
#include <cstring>

using namespace std;

char ch[1000022];
int n,i,a[1000022];
long long t,nr;

int main()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);

    gets(ch+1);
    n=strlen(ch+1);
    for(i=1;i<=n;++i) a[i+20]=ch[i]-'0';

    scanf("%lld",&nr);

    t=0LL;
    for(i=1+20;i<=n+20;++i)
    {
        t=t*10+a[i];
        a[i]=t/nr;
        t%=nr;
    }

    if(!t)
    {
        printf("%s\n",ch+1);
        return 0;
    }

    ++a[n+20];i=n;
    while(a[i+20]>9)
    {
        a[i+20]=0;
        ++a[i-1+20];
        --i;
    }

    t=0LL;
    for(i=n+20;t || i>0+20;--i)
    {
        t=t+a[i]*nr;
        a[i]=t%10;
        t/=10;
    }
    i=1;
    while(!a[i]) ++i;
    for(i;i<=n+20;++i) printf("%d",a[i]);
    printf("\n");

    return 0;
}