跳到正文

算法·理论

逆天 Pollard Rho

沉石鱼惊旋约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年4月25日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 2 分钟
获赞
5

版本

  1. v1当前

https://www.luogu.com.cn/record/156977790

对于题解中的给出的调用系统函数思路进行了 cpp 风格的封装。

原题解:https://www.luogu.com/article/y2bnvunz

大致思路就是调用 Linux 的 factor 函数,取出输出内容,然后格式化一下。所有因数会从小到大存在 pr(n, v) 的 vv 中。

C++
typedef long long ll;
void execCMD(const char *cmd, string &result)
{
    char buf_ps[1024];
    char ps[1024] = {0};
    FILE *ptr;
    strcpy(ps, cmd);
    if ((ptr = popen(ps, "r")) != NULL)
    {
        while (fgets(buf_ps, 1024, ptr) != NULL)
            result += buf_ps;
        pclose(ptr);
        ptr = NULL;
    }
}
void pr(ll n, auto &v)
{
    string s = to_string(n);
    string cmd = "factor " + s;
    v.clear();
    string t = "";
    execCMD(cmd.c_str(), t);
    t.erase(0, s.size() + 2);
    ll d = 0;
    for (char c : t)
    {
        if (isdigit(c))
            (d *= 10) += c ^ '0';
        else
            v.push_back(d), d = 0;
    }
}
原文
存档评论3 条抓取于