Cod sursa(job #1236721)

Utilizator armandpredaPreda Armand armandpreda Data 2 octombrie 2014 15:38:59
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <cstdio>
#include <cstring>

using namespace std;

const int MAX=1000000,BASE=10;
char s[MAX];
bool ok;
class HUGE
{
    private:
        int x[MAX];
    public:
        void scan()
        {
            scanf("%s",s);
            int n=strlen(s);
            x[0]=n;
            for(int i=n-1;i>=0;--i)
                x[n-i]=s[i]-'0';
        }
        void print()
        {
            for(int i=x[0];i>=1;--i)
                printf("%d",x[i]);
        }
        void copy(const HUGE & other)
        {
            x[0]=other.x[0];
            for(int i=1;i<=x[0];++i)
                x[i]=other.x[i];
        }
        void adunare(long long a)
        {
            int rest=0,i=1;
            x[i]+=a;
            rest=x[i]/BASE,x[i]%=BASE;
            while(rest)
            {
                i++;x[i]+=rest;
                rest=x[i]/BASE,x[i]%=BASE;
            }
            if(i>x[0])x[0]=i;
        }
        long long impartire(long long d)
        {
            long long rest=0;
            for(int i=x[0];i>=1;--i)
            {
                rest=rest*BASE+x[i];
                x[i]=rest/d,rest%=d;
            }
            return rest;
        }

};
HUGE a,b;

int main()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    long long x,rest;
    a.scan();
    scanf("%I64d",&x);
    b.copy(a);
    rest=b.impartire(x);
    if(rest)
        a.adunare(x-rest);
    a.print();
    return 0;
}