742A - Arpas Hard Exam And Mehrdads Naive Cheat

Note that $1378^n \equiv 8^n \mod 10$. Furthermore, for $n >= 1$, you might notice a pattern with $n$ if you list answers for small numbers. They repeat with a period of 4, so just hardcode them.

Time complexity: $O(1)$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, ans[4] = {8, 4, 2, 6};
    cin >> n;
    if (n == 0)
        cout << 1 << endl;
    else
        cout << ans[(n - 1) % 4] << endl;
    return 0;
}