3461. 判断操作后字符串中的数字是否相等 I

3461. 判断操作后字符串中的数字是否相等 I


题目链接:3461. 判断操作后字符串中的数字是否相等 I

代码如下:

cpp 复制代码
class Solution {
public:
    bool hasSameDigits(string s) {
        int n = s.size();

        // 预处理 2 和 5 的幂次
        int P2[n + 1], P5[n + 1];
        P2[0] = P5[0] = 1;
        for (int i = 1; i <= n; i++) P2[i] = P2[i - 1] * 2 % 10, P5[i] = P5[i - 1] * 5 % 10;

        // 扩展欧几里得算法
        auto exgcd = [&](this auto &&self, int a, int b, int &x, int &y) -> void {
            if (b == 0) {
                x = 1; y = 0;
                return;
            }
            self(b, a % b, y, x);
            y -= a / b * x;
        };

        // 求 s[l] 到 s[r] 合并起来的结果
        auto calc = [&](int l, int r) {
            int n = r - l;
            // c:抛掉因数 2 和 5 后,组合数 mod 10 的结果
            // two:组合数里因数 2 有几个
            // five:组合数里因数 5 有几个
            int c = 1, two = 0, five = 0, sm = 0;
            for (int i = l, j = 0; ; i++, j++) {
                // 按公式求和
                sm = (sm + (s[i] - '0') * P2[two] * P5[five] * c) % 10;
                if (i == r) break;
                // 组合数递推式,先乘 (n - m)
                int t = n - j;
                while (t % 2 == 0) two++, t /= 2;
                while (t % 5 == 0) five++, t /= 5;
                c = c * t % 10;
                // 组合数递推式,再除 (m + 1)
                t = j + 1;
                while (t % 2 == 0) two--, t /= 2;
                while (t % 5 == 0) five--, t /= 5;
                // 扩展欧几里得算法求逆元
                int x, y;
                exgcd(t, 10, x, y);
                c = c * (x % 10 + 10) % 10;
            }
            return sm;
        };

        return calc(0, n - 2) == calc(1, n - 1);
    }
};
相关推荐
liulilittle3 小时前
LwIP协议栈MPA多进程架构
服务器·开发语言·网络·c++·架构·lwip·通信
艾莉丝努力练剑3 小时前
【C++:继承】面向对象编程精要:C++继承机制深度解析与最佳实践
开发语言·c++·人工智能·继承·c++进阶
penguin_bark3 小时前
C++ 异步编程(future、promise、packaged_task、async)
java·开发语言·c++
nianniannnn4 小时前
Qt布局管理停靠窗口QDockWidget类
开发语言·数据库·c++·qt·qt5·qt6.3
lightqjx5 小时前
【C++】list 常见使用和模拟实现
开发语言·c++
无聊的小坏坏5 小时前
从零开始:C++ 多进程 TCP 服务器实战(续篇)
服务器·c++·tcp/ip
ceclar1235 小时前
C++容器queue
开发语言·c++
启诚科技5 小时前
树上二分(树的重心)
c++·算法·二分·树的重心
读书读傻了哟6 小时前
Windows 10 下 VS Code 配置 C++ 开发环境(MinGW)
c++·windows·mingw