Cod sursa(job #2768053)

Utilizator Darius_CDarius Chitu Darius_C Data 9 august 2021 11:33:32
Problema Cifra Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
// Cifra.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#pragma warning(disable: 4996)
#define DIM 101
#define MAXLEN 666
using namespace std;
// query(n+100) = query(100) + query(n) = query(n), so query(n)= query(n%100)

FILE* fin, * fout;

int query[DIM];

int pow(int a, int b, int mod)
{
	if (b == 0)
		return 1;
	else {
		int x = pow(a, b / 2, mod);
		if (b % 2 == 0)
			return (x * x) % mod;
		else
			return (((x * x) % mod) * a) % mod;
	}
}

static inline void Precalculation()
{
	query[0] = 0;
	for (int i = 1; i < DIM; i++)
		query[i] = (query[i - 1] + pow(i, i, 10)) % 10;
}

static inline void Solve()
{
	int T;
	char N[MAXLEN];
	scanf("%d", &T);
	for (; T; T--)
	{
		scanf("%s", &N);
		int cif = strlen(N) - 1;
		int last2dig = N[cif] - '0';
		if (cif >= 1)
			last2dig = (N[cif - 1] - '0') * 10 + (N[cif] - '0');
		printf("%d\n", query[last2dig]);
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	freopen("cifra.in", "r", stdin);
	freopen("cifra.out", "w", stdout);
	Precalculation();
	Solve();
	fclose(stdin);
	fclose(stdout);
	return 0;
}