Cod sursa(job #1236874)

Utilizator YoChinezuWeng Mihai Alexandru YoChinezu Data 2 octombrie 2014 18:58:07
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.32 kb
#include <cstdio>
#include <cstring>

using namespace std;

char s[1000005];

class HUGE{
private: int x[10000001];
public: HUGE(){
    memset(x,0,sizeof(x));
    x[0]=1;
}
    HUGE(bool sss){
        gets(s);
        int nn;
        x[0]=strlen(s);
        nn=x[0]-1;
        for(int i=1;i<=x[0];++i){
            x[i]=s[nn]-'0';
            --nn;
        }
    }
    HUGE(const HUGE& other){
        memcpy(x,other.x,sizeof(other.x));
    }
    HUGE (int n){
        memset(x,0,sizeof(x));
        x[0]=0;
        do{
            x[++x[0]]=n%10;
            n/=10;
        }while(n);
    }
    int cmp(const HUGE& other){
        if(x[0]<other.x[0]) return -1;
        else if(x[0]>other.x[0])    return 1;
        for(int i=x[0];i>=0;--i)
            if(x[i]<other.x[i]) return -1;
            else if(x[i]>other.x[i])    return 1;
        return 0;
    }
    void print(){
        for(int i=x[0];i>=1;--i)    printf("%d",x[i]);
        printf("\n");
    }
    HUGE operator *(const HUGE& other);
    HUGE operator /=(int kk);
    bool operator ==(const HUGE& other);
    HUGE operator +(int po);
};

HUGE HUGE::operator *(const HUGE& other){
    HUGE c;
    c.x[0]=x[0]+other.x[0]-1;
    int i,j,t;
    for(i=1;i<=x[0];++i)
        for(j=1;j<=other.x[0];++j)
            c.x[i+j-1]+=x[i]*other.x[j];
    t=0;
    for(i=1;i<=c.x[0];++i){
        t=t+c.x[i];
        c.x[i]=t%10;
        t/=10;
    }
    int l=c.x[0];
    while(t){
        ++l;
        c.x[l]=t%10;
        t/=10;
    }
    c.x[0]=l;
    return c;
}

HUGE HUGE::operator /=(int kk){
    int r=0,i;
    for(i=x[0];i>0;--i){
        r=r*10+x[i];
        x[i]=r/kk;
        r=r%kk;
    }
    while(x[0]>1&&!x[x[0]]) --x[0];
    return *this;
}

inline bool HUGE:: operator ==(const HUGE& other){
    if((*this).cmp(other)!=0)
        return 0;
    return 1;
}

HUGE HUGE:: operator +(int po){
    x[1]++;
    for(int i=1;i<=x[0];++i){
        if(x[i]>10){
            x[i+1]++;
            x[i]-=10;
        }
    }
    if(x[x[0]+1]==1){
        x[0]++;
    }
    return *this;
}

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

    int k,po;
    po=1;
    bool sss;
    HUGE A(sss);
    scanf("%d",&k);
    HUGE Q(k),S,L;
    L=A;
    A/=k;
    S=A;
    if(S*Q==L){
        L.print();
    }else{
        S=S+po;
        S=S*Q;
        S.print();
    }

    return 0;
}