spoj, czy umiesz potęgować

0

Witam, robię zadanie ze spoja czy umiesz potęgować, mam błędną odpowiedź, wie ktoś o co może chodzić? Oto kod:

const howMany = readline('how many cases?');
for (i = 0; i < howMany; i++) {
    let a = readline('');
    let b = readline('');
    let score;
    if (a % 10 === 1) {
        score = 1;
    } else if (a % 10 === 2) {
        if (b % 4 === 0) score = 6;
        else if (b % 4 === 1) score = 2;
        else if (b % 4 === 2) score = 4;
        else score = 8;
        console.log(score)
    } else if (a % 10 === 3) {
        if (b % 4 === 0) score = 1;
        else if (b % 4 === 1) score = 3;
        else if (b % 4 === 2) score = 9;
        else score = 7;
        console.log(score)
    } else if (a % 10 === 4) {
        if (b % 4 === 1) score = 4;
        else score = 6;
        console.log(score)
    } else if (a % 10 === 5) {
        score = 5;
        console.log(score)
    } else if (a % 10 === 6) {
        score = 6;
        console.log(score)
    } else if (a % 10 === 7) {
        if (b % 4 === 0) score = 1;
        else if (b % 4 === 1) score = 7;
        else if (b % 4 === 2) score = 9;
        else score = 3;
        console.log(score)
    } else if (a % 10 === 8) {
        if (b % 4 === 0) score = 6;
        else if (b % 4 === 1) score = 8;
        else if (b % 4 === 2) score = 4;
        else score = 2;
        console.log(score)
    } else if (a % 10 === 9) {
        if (b % 2 === 0) score = 1;
        else score = 9;
        console.log(score)
    } else if (a % 10 === 0) {
        score = 0;
        console.log(score)
    }

}

Oto link do zadania:https://pl.spoj.com/problems/PA05_POT/

1

To jest nie najlepszy sposób (debugowanie takiej ifologii to masakra), Rzuć okiem na to:
https://stackoverflow.com/questions/37709176/last-digit-in-an-exponent-wrong-answer-c-sharp

function last_digit_exponent(a, b):
    return pow(a % 10, b % 4 + 4) % 10

Gdzie pow to potęgowanie w danym języku. To powinno przejść, z tego co pamietam, to rozwiązanie z potęgowaniem modularnym też przechodziło, chociaż złożoność jest ~logn.

0

zrobiłem tą metodą, ale nie wiem jak w powłoce smonkey użyć math.power, bo pewnie to mam nie tak, a jak robię to pętlą for zmniejszając a i b tak tak w poście powyżej to mam przekroczony limit czasu

1

To Skrobnij potęgowanie w czasie logarytmicznym:
https://en.wikipedia.org/wiki/Exponentiation_by_squaring

1 użytkowników online, w tym zalogowanych: 0, gości: 1