Cod sursa(job #2770048)

Utilizator TeodoraMaria123Serban Teodora Maria TeodoraMaria123 Data 19 august 2021 00:40:50
Problema Next Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.61 kb
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int max_digits=1e6+10;
const int base=10;
const int nmax=1e6+10;
char s[nmax];
class huge
{
public:
    short x[max_digits];
public:
    huge()
    {
        memset(x,0,sizeof(x));
        x[0]=1;
    }
    huge(long long n)
    {
        memset(x,0,sizeof(x));
        do
        {
            x[++x[0]]=n%base;
            n/=base;
        }
        while(n);
    }
    void print()
    {
        for(int i=x[0]; i>=1; i--)
            out<<x[i];
        out<<"\n";
    }
    huge operator +(const huge &other);
    huge operator -(const huge &other);
    huge operator *(int k);
    huge operator /(long long k);
    long long operator %(long long k);
};
huge huge :: operator +(const huge &other)
{
    int i,tr,aux;
    huge c;
    c.x[0]=max(x[0],other.x[0]);
    for(tr=0,i=1; i<=c.x[0]; i++)
    {
        aux=x[i]+other.x[i]+tr;
        c.x[i]=aux%base;
        tr=aux/base;
    }
    if(tr)
        c.x[++c.x[0]]=tr;
    return c;
}
huge huge :: operator -(const huge &other)
{
    huge c;
    c.x[0]=max(x[0],c.x[0]);
    int i,impr,aux;
    for(impr=0,i=1; i<=c.x[0]; i++)
    {
        aux=x[i]-other.x[i]-impr;
        if(aux<0)
        {
            c.x[i]=aux+base;
            impr=1;
        }
        else
        {
            c.x[i]=aux;
            impr=0;
        }
    }
    while(c.x[0]>1  &&  c.x[c.x[0]]==0)
        c.x[0]--;
    return c;
}
huge huge :: operator *(int k)
{
    huge c;
    int i,aux,tr;
    c.x[0]=x[0];
    for(i=1; i<=c.x[0]; i++)
        c.x[i]=x[i]*k;
    for(tr=0,i=1; i<=c.x[0]; i++)
    {
        aux=c.x[i]+tr;
        c.x[i]=aux%base;
        tr=aux/base;
    }
    while(tr)
    {
        c.x[++c.x[0]]=tr%base;
        tr/=base;
    }
    return c;
}
huge huge :: operator /(long long k)
{
    huge c;
    int i;
    long long r;
    c.x[0]=x[0];
    for(r=0,i=c.x[0]; i>=1; i--)
    {
        r=1ll*(r*base+x[i]);
        c.x[i]=r/k;
        r=1ll*(r%k);
    }
    while(c.x[0]>1  &&  c.x[c.x[0]]==0)
        c.x[0]--;
    return c;
}
long long huge :: operator %(long long k)
{
    int i;
    long long r;
    for(r=0,i=x[0]; i>=1; i--)
    {
        r=1ll*(r*base+x[i]);
        r=1ll*(r%k);
    }
    return r;
}
int main()
{
    in.getline(s,nmax);
    huge nr;
    int n,i;
    long long d;
    in>>d;
    n=strlen(s);
    nr.x[0]=n;
    for(i=0; i<n; i++)
        nr.x[n-i]=s[i]-'0';
    if(nr%d==0)
        nr.print();
    return 0;
}