Cod sursa(job #490760)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 7 octombrie 2010 19:11:51
Problema Next Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include<cstdio>
typedef int Huge[1<<20];
Huge a;
int b[101];
long long r,x;
void read()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    char s[1<<20];
    gets(s);
    for(int i=0;s[i];i++)
       a[++a[0]]=s[i]-'0';
    scanf("%lld",&x);
}
long long modulo(Huge A,long long x)
{
    long long T=0;
    for(int i=1;i<=A[0];i++)
    {
        T=T*10+A[i];
        if(T>=x)
            T-=(T/x)*x;
    }
    return T;
}
void sch(int &X,int &Y)
{
    int aux=X;
    X=Y;
    Y=aux;
}
void rastorn(Huge A)
{
    int dr,st;
    dr=0;
    st=A[0]+1;
    while(dr+1<st-1)
    {
        dr++;
        st--;
        sch(A[dr],A[st]);
    }
}
void getvc(long long x,int A[101])
{
    while(x)
        A[++A[0]]=x%10, x/=10;
}
int max(int x,int y)
{
    return x<y?y:x;
}
void adun(Huge A,int B[101])
{
    int T=0,i=0;
    for(i=1;i<=B[0] || i<=A[0];i++)
    {
        A[i]+=T;
        if(i<=B[0])
            A[i]+=B[i];
        T=A[i]/10;
        A[i]%=10;
    }
    A[0]=max(i-1,A[0]);
    while(T)
    {
        A[++A[0]]=T%10;
        T/=10;
    }

}
void afis(Huge A)
{
    for(int i=A[0];i>=1;i--)
        printf("%d",A[i]);
}
int main()
{
    read();
    r=modulo(a,x);
    rastorn(a);
    getvc(x-r,b);
    adun(a,b);
    afis(a);
    return 0;
}