Cod sursa(job #2613928)

Utilizator cosmin_paunicaCosmin Paunica cosmin_paunica Data 10 mai 2020 20:56:12
Problema Cifra Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

// M[j % 10][(j - 1) % M[j].size()] == ultima cifra a lui j^j, pentru 1 <= j <= 100
vector<vector<int>> M = {
	{0},
	{1},
	{2, 4, 8, 6},
	{3, 9, 7, 1},
	{4, 6},
	{5},
	{6},
	{7, 9, 3, 1},
	{8, 4, 2, 6},
	{9, 1}
};

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

	unsigned T;
	fin >> T;
	for (unsigned i = 1; i <= T; i++) {
		char N[101];
		fin >> N;
		if (strlen(N) > 1)
			if (N[strlen(N) - 2] == '0')
				strcpy(N, N + strlen(N) - 1);
			else
				strcpy(N, N + strlen(N) - 2);
		unsigned result = 0;
		unsigned n = atoi(N);
		for (unsigned j = 1; j <= n; j++)
			result = (result + M[j % 10][(j - 1) % M[j % 10].size()]) % 10;
		fout << N << ": " << result << endl;
	}

	return 0;
}