Cod sursa(job #1171254)

Utilizator Paula-ElenaPaula-Elena Margarit Paula-Elena Data 15 aprilie 2014 14:28:36
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <cstring>
#include <fstream>
using namespace std;

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

const int MAXN = 103;

int T, N, c[11][5], cif;
char v[MAXN];

void precalc()
{
    for (int i = 1; i < 10; ++i)
    {
        int prod = 1;
        for (int j = 1; j <= 4; ++j)
        {
            prod *= i;
            c[i][j%4] = prod % 10;
        }
    }

    for (int i = 1; i < 10; ++i)
        cif += c[i][i%4];
    cif %= 10;
}

inline int solve()
{
    int u_cif = v[N-1] - '0', aux = 1, sol = 0;

    for (int i = 1; i < N; ++i)
    {
        for (int j = 1; j < 10; ++j)
        {
            sol += c[j][aux];
            aux = (aux + 1) % 4;
        }
        aux = (aux + 1) % 4;
    }

    for (int i = 1; i <= u_cif; ++i)
    {
        sol += c[i][aux];
        aux = (aux + 1) % 4;
    }

    return sol%10;
}

void read()
{
    fin >> T >> ws;
    for ( ; T; --T)
    {
        fin.getline(v, MAXN);
        N = strlen(v);
        fout << solve() << "\n";
    }
}

int main()
{
    precalc();
    read();

    fin.close();
    fout.close();

    return 0;
}