Cod sursa(job #2692685)

Utilizator catalin69420Gogu Popescu catalin69420 Data 3 ianuarie 2021 14:50:45
Problema Cifra Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <array>
#include <iostream>
#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;
	}

	for (auto i:sums)
		cout << i << " ";
	cout << endl;

	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] << endl;
		}
	}
}