Pagini recente » Arhiva de probleme | Cod sursa (job #1411542) | Cod sursa (job #1397255) | Istoria paginii runda/branza/clasament | Cod sursa (job #2692617)
#include <vector>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std; // bad practice
uint64_t lastDigitOfPowab(uint64_t a, uint64_t 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
uint64_t exp = (b % 4 == 0) ? 4 : (b % 4);
// Find last digit in 'a' and compute its exponent
uint64_t res = pow(a % 10, exp);
// Return last digit of result
return res % 10;
}
vector<uint64_t> buildVec(uint64_t n) {
vector<uint64_t> v(n);
for (uint64_t i = 0; i < v.size(); i++) {
v[i] = lastDigitOfPowab(i + 1, i + 1);
}
return v;
}
uint64_t sum(const vector<uint64_t>& v) {
uint64_t sum = 0;
for (auto i:v)
sum += i;
return sum;
}
vector<uint64_t> mapf(const vector<uint64_t>& v, function<uint64_t(uint64_t)> f) {
vector<uint64_t> result(v.size());
transform(v.begin(), v.end(), result.begin(), f);
return result;
}
int main() {
ifstream f("cifra.in");
ofstream g("cifra.out");
uint64_t n;
f >> n;
auto v = buildVec(n);
g << sum(v) % 10 << endl;
}