Cod sursa(job #2772657)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 2 septembrie 2021 08:50:31
Problema Cifra Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <iostream>
#include <chrono>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <string>

int process(int n) {
    int final = 0;

    for (int i = 1; i <= n; ++i) {
        long long tmp = i;
        for (int j = 1; j < i; ++j) {
            tmp *= i;
            tmp %= 10;
//            if (tmp == 0) {
//                break;
//            }
        }
        final += (tmp % 10);
        final = final % 10;
    }
    return final % 10;
}

void cifra() {
    std::ifstream f1;
    std::ofstream f2;

    f1.open("../cifra.in");
    f2.open("../cifra.out");

    int number_of_numbers;

    f1 >> number_of_numbers;

    std::vector<int> precompute(111);

    for (int i = 1; i < 111; ++i) {
        int computed_i = process(i);
        precompute[i] = computed_i;
    }

//    {
//        const int MAX = 10000;
//        char buffer[MAX];
//
//        for (int i = 0; i < number_of_numbers; ++i) {
//            f1 >> buffer;
//            int length = strlen(buffer);
//            int res = buffer[length - 1] - '0';
//            numbers.push_back(res);
//        }
//    }

    for (int i = 0; i < number_of_numbers; ++i) {
        std::string actual;

        f1 >> actual;
        int nr = actual.back() - '0';
        if (actual.size() > 1) {
            nr += 10 * (actual[actual.size() - 2] - '0');
        }

        f2 << precompute[nr] << '\n';
    }
}


int main() {
    auto start = std::chrono::high_resolution_clock::now();

    cifra();

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
    std::cout << std::endl << "[time]:" << duration.count() << std::endl;
    return 0;
}