Pagini recente » Cod sursa (job #173983) | Cod sursa (job #2967930) | Cod sursa (job #2440712) | Cod sursa (job #311110) | Cod sursa (job #2692632)
#include <vector>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std; // bad practice
int lastDigitOfPowab(int a, int 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 solve(int n) {
int sum = 0;
for (int i = 1; i <= n; i++)
sum += lastDigitOfPowab(i, i);
return sum % 10;
}
int main() {
ifstream f("cifra.in");
ofstream g("cifra.out");
int n;
f >> n;
for (int i = 0; i < n; i++) {
int num;
f >> num;
g << solve(num) << endl;
}
}