Cod sursa(job #1333851)

Utilizator Vasile_RotaruVasea Rotaru Vasile_Rotaru Data 3 februarie 2015 17:07:45
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <vector>
#include <cstring>
 
int pow_n(int n)
{
    int ret = 1, x = n;
    while (n) {
        if (n & 1){
            ret *= x;
            ret %= 10;
        }
        n /= 2;
        x *= x;
        x %= 10;
    }
    return ret;
}
 
void precompute(std::vector<int> &v)
{
    int val = 0;
    v.push_back(0);
    for (auto i = 1; i < 100; ++i){
        val += pow_n(i);
        val %= 10;
        v.push_back(val);
    }
 
}
 
int main()
{
    std::vector<int> v;
    std::ifstream f("cifra.in");
    std::ofstream g("cifra.out");
    precompute(v);
    int t;
    char line[201];
    f >> t;
    f.get();
    while (t--)
    {
        f.getline(line, 200);
        auto size = strlen(line);
        auto num = line[size - 1] - '0';
        if (size > 1)
            num += (line[size - 2] - '0') * 10;
        g << v[num] << "\n";
    }
    f.close();
    g.close();
    return 0;
}