Cod sursa(job #1747937)

Utilizator cyg_LucaFlorinTanasescu Luca Florin cyg_LucaFlorin Data 25 august 2016 20:13:50
Problema Next Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.57 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define NMAX (1 << 15)
char s[1000001];

class HugeN
{
    private:
        int x[NMAX];
    public: HugeN();
            HugeN(int nr);
            HugeN(HugeN & other);
            void get_huge();
            HugeN&operator+(int k);
            HugeN&operator+(const HugeN&other);
            HugeN&operator-(const HugeN&other);
            int&operator%(int k);
};
HugeN::HugeN()
{
    int n,i;
    gets(s);
    n=strlen(s);
    for(i=n-1;i>=0;i--)
        x[++x[0]]=s[i]-'0';
}
HugeN::HugeN(int nr)
    {
        memset(x,0,sizeof(x));
        if(nr>0)
        {
            do
                {
                    x[++x[0]]=nr%10;
                    nr=nr/10;
                }while(nr);
        }
        else
            {
                x[0]=1;
            }
    }
HugeN::HugeN(HugeN&other)
{
    memset(x,0,sizeof(x));
    for(int i=0;i<=other.x[0];i++)
        x[i]=other.x[i];
}
HugeN&HugeN::operator+(const HugeN&other)
{
    HugeN temp;
    int tr,i,aux;
    tr=0;
    temp.x[0]=max(x[0],other.x[0]);
    for(i=1;i<=temp.x[0];i++)
        {
            aux=x[i]+other.x[i]+tr;
            temp.x[i]=aux%10;
            tr=aux/10;
        }
	if(tr)
	{
		temp.x[0]++;
		temp.x[temp.x[0]]++;
	}
    return temp;
}
HugeN&HugeN::operator-(const HugeN&other)
    {
        HugeN temp;
        int impr,aux,i;
        impr=0;
        temp.x[0]=max(x[0],other.x[0]);
        for(i=1;i<=temp.x[0];i++)
            {
                aux=x[i]-other.x[i]-impr;
                if(aux<0)
                    {
                        temp.x[i]=aux+10;
                        impr=1;
                    }
                else
                    {
                        temp.x[i]=aux;
                        impr=0;
                    }
            }
        while(temp.x[0]>1&&temp.x[x[0]]==0)
            --temp.x[0];
        return temp;
    }
int&HugeN::operator%(int k)
	{
		int i,r=0;
		for(i=x[0];i>=1;i--)
            {
                r=r*10+x[i];
                r=r%k;
            }
        return r;
	}
void HugeN::get_huge()
{
    for(int i=x[0];i>=1;i--)
        printf("%d",x[i]);
    printf("\n");
}
int main()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    long long r,d;
    HugeN n;
    scanf("%lld ",&d);
    r=n%d;
    if(r==0)
        n.get_huge();
    else
    {
        d=d-r;
        HugeN d1(d);
        n=n+d1;
        n.get_huge();
    }
    return 0;
}