Pagini recente » Cod sursa (job #1743630) | Cod sursa (job #1488588) | Istoria paginii runda/oni11 | Cod sursa (job #2257314) | Cod sursa (job #2692674)
#include <array>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std; // bad practice
int lastDigitOfPow(int a, int b ) {
// compute the last digit of a ^ b
// if a and b both are 0
if (a == 0 && b == 0)
return 1;
// if exponent is 0
if (b == 0)
return 1;
// if base is 0
if (a == 0)
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10.
// if exponent is not divisible by 4 that means last
// digit will be pow(a, b%4) % 10
int exp = (b % 4 == 0) ? 4 : (b % 4);
// Find last digit in 'a' and compute its exponent
int res = static_cast<int>(pow(a % 10, exp));
// Return last digit of result
return res % 10;
}
int main() {
array<int, 101> sums;
sums.fill(0);
for (unsigned int i = 1; i < sums.size(); i++) {
sums[i] = (sums[i - 1] + lastDigitOfPow(i, i)) % 10;
}
ifstream f("cifra.in");
ofstream g("cifra.out");
int n;
f >> n;
for (int i = 0; i < n; i++) {
int x;
f >> x;
if (x <= 100) {
g << sums[x];
}
}
}