Cod sursa(job #2429305)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 9 iunie 2019 00:03:30
Problema Cifra Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.01 kb
#include <fstream>
#define LGMAX 110

std::ifstream fin("cifra.in");
std::ofstream fout("cifra.out");

class BigInt {
  private:
    int lg;
    int *nr;

  public:
    ~BigInt();
    BigInt(int val = 0);
    BigInt& operator=(const BigInt& other);

    operator int();
    inline int operator[](int pos) const { return pos < lg ? nr[pos] : 0; }

    friend BigInt operator/(const BigInt& a, int b);
    friend BigInt operator%(const BigInt& a, int b);

    friend std::istream& operator>>(std::istream& in, BigInt& x);
    friend std::ostream& operator<<(std::ostream& out, const BigInt& x);
};

BigInt::BigInt(int val) {
    int cpy = val;
    lg = 0;
    do {
        lg++;
        cpy /= 10;
    } while (cpy);
    nr = new int[lg];

    int i = 0;
    do {
        nr[i++] = val % 10;
        val /= 10;
    } while (val);
}

BigInt::~BigInt() {
    delete[] nr;
}

BigInt& BigInt::operator=(const BigInt& other) {
    if (this != &other) {
        if (lg != other.lg) {
            lg = other.lg;
            delete[] nr;
            nr = new int[lg];
        }

        for (int i = 0; i < lg; i++)
            nr[i] = other.nr[i];
    }
    return *this;
}

BigInt::operator int() {
    int ret = 0;
    for (int i = lg - 1; i >= 0; i--)
        ret = ret * 10 + nr[i];
    return ret;
}

BigInt operator/(const BigInt& a, int b) {
    BigInt c;
    int val = 0;
    int cat[LGMAX];

    for (int i = a.lg - 1; i >= 0; i--) {
        val = val * 10 + a.nr[i];
        cat[i] = val / b;
        val %= b;
    }

    c.lg = a.lg;
    while (c.lg > 1 && !cat[c.lg - 1])
        c.lg--;

    delete[] c.nr;
    c.nr = new int[c.lg];

    for (int i = 0; i < c.lg; i++)
        c.nr[i] = cat[i];
    return c;
}

BigInt operator%(const BigInt& a, int b) {
    int val = 0;
    for (int i = a.lg - 1; i >= 0; i--) {
        val = val * 10 + a.nr[i];
        val %= b;
    }
    return val;
}

std::istream& operator>>(std::istream& in, BigInt& x) {
    char chr;
    int temp[LGMAX];

    x.lg = 1;
    in >> chr;
    temp[0] = chr - '0';

    while (true) {
        in.get(chr);
        if ('0' <= chr && chr <= '9')
            temp[x.lg++] = chr - '0';
        else
            break;
    }

    delete[] x.nr;
    x.nr = new int[x.lg];

    for (int i = 0; i < x.lg; i++)
        x.nr[i] = temp[x.lg - i - 1];
    return in;
}

std::ostream& operator<<(std::ostream& out, const BigInt& x) {
    for (int i = x.lg - 1; i >= 0; i--)
        out << x.nr[i];
    return out;
}

int main() {
    const int pre[] = {0, 1, 4, 7, 6, 5, 6, 3, 6, 9, 0, 1, 6, 3, 6, 5, 6, 7, 4, 9, 0};
    auto sum = [&](int pos) {
        int ret = 0;
        for (int i = 1; i <= pos; i++)
            ret += pre[i];
        return ret;
    };

    int t; fin >> t;
    while (t--) {
        BigInt n; fin >> n;
        int c = (n / 20)[0], r = n % 20;
        fout << (sum(20) * c % 10 + sum(r)) % 10 << '\n';
    }

    fout.close();
    return 0;
}