1 条题解

  • 0
    @ 1996-5-27 10:02:37

    练29.2 末两位数

    解题思路

    末两位等于 1992^n 模 100。用乘积初值 1,每次乘 92 后模 100,重复 n 次。

    正确性说明

    按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。

    易错点

    逐次取模避免完整幂溢出;按两位输出结果。

    复杂度

    时间 O(n),额外空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        int ans = 1;
        for (int i = 0; i < n; i++)
            ans = ans * 92 % 100;
        cout << setfill('0') << setw(2) << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    203
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者