Cod sursa(job #1595355)

Utilizator ArceyGeorge Cioroiu Arcey Data 10 februarie 2016 11:04:31
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>
#include <algorithm>
#include <list>
#include <vector>
#include <utility>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <string>

using namespace std;

int modExp(int base, int exp, int mod) {
    if (mod == 1)
        return 0;

    int ans = 1;
    base %= mod;

    while (exp > 0) {
        if (exp % 2 == 1)
            ans = (ans * base) % mod;

        exp >>= 1;
        base = (base * base) % mod;
    }

    return ans;
}

int main() {
   // freopen("tt.txt", "r", stdin);
    freopen("cifra.in", "r", stdin);
    freopen("cifra.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(0);

    int s = 0, ans[100];
    ans[0] = 0;
    for (int i = 1; i < 100; i++) {
        s += modExp(i, i, 10);
        s %= 10;
        ans[i] = s;
    }

    int totalTest;
    cin >> totalTest;
    for (int test = 0; test < totalTest; test++) {
        string line;
        cin >> line;

        int number = line[line.size() - 1] - '0';
        if (line.size() > 1) {
            number += line[line.size() - 2] - '0' * 10;
        }

        cout << ans[number] << "\n";
    }

    return 0;
}