Cod sursa(job #2222259)

Utilizator CiprianC11Constantinescu Ciprian CiprianC11 Data 16 iulie 2018 19:01:02
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.89 kb
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

class huge
{
private :
    int x[54164];
public :
    huge() { memset(x, 0, sizeof(x)); x[0] = 1; }
    huge(long long a)
    {
        memset(x, 0, sizeof(x));
        x[0] = 0;
        do
        {
            x[++x[0]] = a % (int)1e6;
            a /= (int)1e6;
        }while(a);
    }
    void scan()
    {
        char b[1000005];
        scanf("%s", &b);
        int l = strlen(b);
        x[0] = l / 6;
        for(int i = l - 7, k = 1; i >= 0; i -= 6, k++) { x[k] = b[i + 1] * 1e5 + b[i + 2] * 1e4 + b[i + 3] * 1e3 + b[i + 4] * 1e2 + b[i + 5] * 1e1 + b[i + 6] - 111111 * '0'; }
        x[0]++;
        if(l % 6 == 1) x[x[0]] = b[0] - '0';
        else if(l % 6 == 2) x[x[0]] = b[0] * 1e1 + b[1] - '0' * 11;
        else if(l % 6 == 3) x[x[0]] = b[0] * 1e2 + b[1] * 1e1 + b[2] - '0' * 111;
        else if(l % 6 == 4) x[x[0]] = b[0] * 1e3 + b[1] * 1e2 + b[2] * 1e1 + b[3] - '0' * 1111;
        else if(l % 6 == 5) x[x[0]] = b[0] * 1e4 + b[1] * 1e3 + b[2] * 1e2 + b[3] * 1e1 + b[4] - '0' * 11111;
        else x[0]--, x[x[0]] = b[0] * 1e5 + b[1] * 1e4 + b[2] * 1e3 + b[3] * 1e2 + b[4] * 1e1 + b[5] - 111111 * '0';
    }
    void print()
    {
        printf("%d", x[x[0]]);
        for(int i = x[0] - 1; i > 0; i--)
        {
            printf("%06d", x[i]);
        }
    }
    huge operator - (const huge &other)
    {
        int aux;
        bool d = 0;
        huge c;
        c.x[0] = x[0];
        for(int i = 1; i <= x[0]; i++)
        {
            aux = x[i] - other.x[i] - d;
            d = (aux < 0);
            if(d) c.x[i] = aux + (int)1e6;
            else c.x[i] = aux % (int)1e6;
        }
        while(c.x[c.x[0]] == 0 and c.x[0] > 1) c.x[0]--;
        return c;
    }
    huge operator + (const huge &other)
    {
        int t = 0;
        huge c;
        c.x[0] = max(x[0], other.x[0]);
        for(int i = 1; i <= c.x[0]; i++)
        {
            t += x[i] + other.x[i];
            c.x[i] = t % (int)1e6;
            t /= (int)1e6;
        }
        if(t) c.x[++c.x[0]] = t;
        return c;
    }
    huge operator %(long long k)
    {
        long long r = 0;
        for(int i = x[0]; i > 0 ; i--)
        {
            r = r * (int)1e6 + x[i];
            r %= k;
        }
        huge c(r);
        return c;
    }
    bool operator ==(const huge &other)
    {
        if(x[0] != other.x[0]) return 0;
        for(int i = x[0]; i > 0; i--)
        {
            if(x[i] != other.x[i]) return 0;
        }
        return 1;
    }
};

int main()
{
    freopen("next.in", "r", stdin);
    freopen("next.out", "w", stdout);
    huge n, m, a;
    long long d;
    n.scan();
    scanf("%lld", &d);
    a = n % d;
    if(a == m) { n.print(); return 0; }
    m = n - a;
    huge dc(d);
    m = m + dc;
    m.print();
    return 0;
}