Cod sursa(job #1778154)

Utilizator iuliusVasilescu Iulius iulius Data 13 octombrie 2016 15:50:56
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.45 kb
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<assert.h>

using namespace std;

const int MAX_DIGITS = 1001;
const int BASE = 10;
const int MAX_INT = (1LL<<31)-1;
char s[MAX_DIGITS+5];
const int k[4]={1,10,100,1000};

FILE *in,*out;
class HugeN
{
    private: char x[MAX_DIGITS+5];
    private: int lenght;
    public:
        HugeN()
        {
            lenght=1;
            for(int i=1; i<=MAX_DIGITS; i++)
                x[i]=0;
        }
        HugeN(int nr)
        {
            for(int i=0;i<=MAX_DIGITS; i++)
                x[i]=0;
            do
            {
                x[++lenght]=nr%10;
                nr /= BASE;
            }while(nr);
        }
        HugeN(HugeN &other)
        {
            for(int i=0; i<=MAX_DIGITS; i++)
                x[i]=0;
            memcpy(x, other.x, sizeof(other.x));
        }
        HugeN(char *s)
        {
            lenght=strlen(s);
            for(int i=1;i<=lenght;i++)
                x[i]=s[lenght-i]-'0';
        }
        void print()
        {
            int i;
            for(i=lenght; i>=1; i--)
                fprintf(out,"%d",x[i]);
            fprintf(out,"\n");
        }
        void read()
        {
            lenght=0;
            int aux;
            char c;
            while(fscanf(in,"%c",&c)!=EOF && c!='\n')
            {
                x[++lenght]=c-'0';
            }
            for(int i=1;i<=lenght/2;i++)
            {
                aux=x[i];
                x[i]=x[lenght-i+1];
                x[lenght-i+1]=aux;
            }
        }
    int cmp(const HugeN &other);
    HugeN operator + (const HugeN &other);
    HugeN operator - (const HugeN &other);
    HugeN operator - (int k);
    HugeN operator += (const HugeN &other);
    HugeN operator += (long long k);
    HugeN operator -= (const HugeN &other);
    long long operator % (long long k);
    bool operator > (const HugeN &other);
    bool operator >= (const HugeN &other);
    bool operator < (const HugeN &other);
    bool operator <= (const HugeN &other);
    bool operator == (const HugeN &other);
    bool operator != (const HugeN &other);
};

int HugeN::cmp(const HugeN &other)
{
    int i;
    if(lenght > other.lenght)
        return 1;
    if(lenght < other.lenght)
        return -1;
    for(i=lenght;i>=1;i--)
        if(x[i] > other.x[i])
            return 1;
        else if(x[i] < other.x[i])
            return -1;
    return 0;
}
HugeN HugeN::operator + (const HugeN &other)
{
    HugeN temp; //temp.x = x+other.x;
    temp.lenght = lenght>other.lenght ? lenght:other.lenght;
    int tr=0,aux,i;
    for(i=1;i<=temp.lenght;i++)
    {
        aux=x[i]+other.x[i]+tr;
        temp.x[i]=aux % BASE;
        tr=aux/BASE;
    }
    if(tr)
        temp.x[++temp.lenght]=tr;
    return temp;
}
HugeN HugeN::operator += (const HugeN &other)
{
    //x=x+other.x
    lenght=lenght>other.lenght?lenght:other.lenght;
    int tr=0,aux,i;
    for(i=1;i<=lenght;i++)
    {
        aux = x[i]+other.x[i]+tr;
        x[i] = aux%BASE;
        tr = aux/BASE;
    }
    if(tr)
        x[++lenght]=tr;
    return *this;
}

HugeN HugeN::operator += (long long k)
{
    int tr=0,aux,i;
    for(i=1;i<=lenght || k;i++)
    {
        aux=x[i]+k%10+tr;
        x[i]=aux%BASE;
        tr=aux/BASE;
        k=k/10;
    }
    if(tr)
        x[++lenght]=tr;
    //lenght=i-1;
    return *this;
}

HugeN HugeN::operator - (const HugeN &other)
{
    HugeN temp;
    assert(x>other.x);
    temp.lenght=lenght;
    int tr=0,aux,i;
    for(i=1;i<=temp.lenght;i++)
    {
        aux = x[i]-other.x[i]-tr;
        if(aux>=0)
        {
            tr=0;
            temp.x[i]=aux;
        }
        else
        {
            tr=1;
            temp.x[i]=BASE+aux;
        }
    }
    while(temp.x[temp.lenght]==0)
        temp.lenght--;
    return temp;
}
HugeN HugeN::operator -= (const HugeN &other)
{
    assert(x>other.x);
    int tr=0,aux,i;
    for(i=1;i<=lenght;i++)
    {
        aux = x[i]-other.x[i]-tr;
        if(aux>=0)
        {
            tr=0;
            x[i]=aux;
        }
        else
        {
            tr=1;
            x[i]=BASE+aux;
        }
    }
    while(x[lenght]==0)
        lenght--;
    return *this;
}
bool HugeN::operator > (const HugeN &other)
{
    if((*this).cmp(other) == 1)
        return 1;
    return 0;
}
bool HugeN::operator >= (const HugeN &other)
{
    if((*this).cmp(other) == 1 || (*this).cmp(other) == 0)
        return 1;
    return 0;
}
bool HugeN::operator < (const HugeN &other)
{
    if((*this).cmp(other) == -1)
        return 1;
    return 0;
}
bool HugeN::operator <= (const HugeN &other)
{
    if((*this).cmp(other) == -1 || (*this).cmp(other) == 0)
        return 1;
    return 0;
}
bool HugeN::operator == (const HugeN &other)
{
    if((*this).cmp(other) == 0)
        return 1;
    return 0;
}
bool HugeN::operator != (const HugeN &other)
{
    if((*this).cmp(other) == 0)
        return 0;
    return 1;
}

long long HugeN::operator % (long long k)
{
    long long r=0;
    for(int i=lenght; i>=1; i--)
        r=(r*10+x[i])%k;
    return r;
}

int main()
{
    in=fopen("data.in","r");
    out=fopen("data.out","w");
    HugeN n1;
    n1.read();
    long long n2;
    fscanf(in,"%lld",&n2);
    n2=n2-n1%n2;
    n1+=n2;
    n1.print();
    return 0;
}
/*
citire
adunare
scriere
*/