1 条题解

  • 0
    @ 2026-10-5 15:24:09

    A. number 题解

    一、核心思路

    题目要求计算 ABCA^{B^C} 的个位。记 E=BCE=B^C,只需计算

    (A mod 10)E mod 10.(A\bmod 10)^E\bmod 10.

    虽然 EE 可能非常大,但一个数的幂的个位会循环出现。我们不必求出 EE,只需知道它在循环中的位置。

    二、找出个位的循环

    令 d=A mod 10d=A\bmod 10。从指数为 1开始,各个位的循环如下。

    dd 个位循环 循环长度 LL
    0 1
    1
    2 2,4,8,6 4
    3 3,9,7,1
    4 4,6 2
    5 1
    6
    7 7,9,3,1 4
    8 8,4,2,6
    9 9,1 2

    因此只需用快速幂计算

    r=BC mod L.r=B^C\bmod L.
    • 若 r>0r>0,答案是循环中的第 rr 项。
    • 若 r=0r=0,答案是循环中的第 LL 项。

    余数为 0 表示走到了循环末尾,不表示指数为 0。 题目保证 B,CB,C 为正整数,所以真实指数 EE 一定大于 0。

    例如 A=2,B=2,C=2A=2,B=2,C=2 时,E=4E=4,循环为 2,4,8,62,4,8,6。因为 E mod 4=0E\bmod4=0,应取末项 66。

    三、算法步骤

    1. 计算 d=A mod 10d=A\bmod10,取出相应的循环数组。
    2. 设数组长度为 LL,用快速幂计算 r=BC mod Lr=B^C\bmod L。
    3. 若 r=0r=0,令 r=Lr=L。
    4. 输出循环数组下标为 r−1r-1 的元素。

    当 L=1L=1 时,快速幂的结果为 0,按同一规则处理即可。

    四、正确性证明

    设个位 dd 的循环长度为 LL。循环表从指数 1 开始,故对任意正整数 EE,dEd^E 的个位是循环中第

    ((E−1) mod L)+1((E-1)\bmod L)+1

    项。

    快速幂求出的 rr 等于 E mod LE\bmod L。当 r>0r>0 时,该位置为 rr;当 r=0r=0 时,该位置为 LL。算法恰好输出对应元素,因此答案正确。

    五、复杂度

    • 每组时间复杂度:O(log⁡C)O(\log C)。
    • 总时间复杂度:O(Tlog⁡Cmax⁡)O(T\log C_{\max})。
    • 额外空间复杂度:O(1)O(1),循环表大小为常数。

    六、易错点

    • 幂的运算顺序是 A(BC)A^{(B^C)},不是 (AB)C(A^B)^C。
    • 不要直接求出 BCB^C,也不要用浮点函数 pow 计算它。
    • B=1B=1 时,真实指数恒为 1。
    • 循环余数为 0 时必须取末项。
    • 这里依据的是个位循环。不能在 AA 与 10 不互质时直接套用欧拉定理。

    七、参考代码

    #include <bits/stdc++.h>
    using namespace std;
    
    using int64 = long long;
    
    int64 powerMod(int64 base, int64 exponent, int mod) {
        int64 result = 1 % mod;
        base %= mod;
    
        while (exponent > 0) {
            if (exponent & 1) {
                result = result * base % mod;
            }
            base = base * base % mod;
            exponent >>= 1;
        }
        return result;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        const vector<vector<int>> cycle = {
            {0}, {1}, {2, 4, 8, 6}, {3, 9, 7, 1}, {4, 6},
            {5}, {6}, {7, 9, 3, 1}, {8, 4, 2, 6}, {9, 1}
        };
    
        int T;
        cin >> T;
    
        while (T--) {
            int64 A, B, C;
            cin >> A >> B >> C;
    
            const vector<int>& digits = cycle[A % 10];
            int length = static_cast<int>(digits.size());
            int position = static_cast<int>(powerMod(B, C, length));
    
            if (position == 0) {
                position = length;
            }
            cout << digits[position - 1] << '\n';
        }
        return 0;
    }
    
    • 1

    信息

    ID
    2348
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    76
    已通过
    9
    上传者