傻傻的胡桃

· · 个人记录

通过观察可以发现 3^1 = 3, 3^2 = 9,3^3=27,3^4=81,3^5=243,3^6=729,末尾数字的出现是有周期性的,周期为 4

所以只需要判断 n 是周期里的第几个就可以了。

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
int ans[] = { 1,3,9,7 };
void solve()
{
    int n;
    cin >> n;
    cout << ans[n % 4] << "\n";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--)solve();
    return 0;
}