跳到正文

个人记录

题目

Hongify约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年6月2日
收录
2026年9月11日
更新
2026年11月11日
篇幅
约 10 分钟

版本

  1. v1当前

已知正整数a,b,求其在(a*b)的第几组因数分解?

输入:

a与b,用空格隔开

输出:

一个正整数,表示a,b在(a*b)的第几组因数分解

样例输入1:

9 74

样例输出1:

5

样例输入2:

65 10

样例输出2:

9

数据范围:

保证1<=a*b<10^18

AI代码

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <random>
#include <chrono>
#include <cstdlib>
#include <cmath>

using namespace std;

typedef __int128 int128;

long long mul_mod(long long a, long long b, long long mod) {
    return (long long)((int128)a * b % mod);
}

long long pow_mod(long long a, long long d, long long n) {
    long long res = 1;
    while (d) {
        if (d & 1) res = mul_mod(res, a, n);
        a = mul_mod(a, a, n);
        d >>= 1;
    }
    return res;
}

bool is_prime(long long n) {
    if (n < 2) return false;
    if (n == 2 || n == 3) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    int s = 0;
    while (d % 2 == 0) {
        d /= 2;
        s++;
    }
    static const long long bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    for (long long a : bases) {
        if (a % n == 0) continue;
        long long x = pow_mod(a, d, n);
        if (x == 1 || x == n - 1) continue;
        bool composite = true;
        for (int r = 1; r < s; ++r) {
            x = mul_mod(x, x, n);
            if (x == n - 1) {
                composite = false;
                break;
            }
        }
        if (composite) return false;
    }
    return true;
}

long long pollard_rho(long long n) {
    if (n % 2 == 0) return 2;
    if (n % 3 == 0) return 3;
    static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<long long> dist(2, n - 1);
    long long x = dist(rng), y = x, c = dist(rng), d = 1;
    auto f = [&](long long x) { return (mul_mod(x, x, n) + c) % n; };
    while (d == 1) {
        x = f(x);
        y = f(f(y));
        d = __gcd(llabs(x - y), n);
        if (d == n) {
            x = y = dist(rng);
            c = dist(rng);
            d = 1;
        }
    }
    return d;
}

void factorize(long long n, map<long long, int>& factors) {
    if (n == 1) return;
    if (is_prime(n)) {
        factors[n]++;
        return;
    }
    long long d = pollard_rho(n);
    factorize(d, factors);
    factorize(n / d, factors);
}

void gen_factors(const vector<pair<long long, int>>& primes, int idx, long long cur, vector<long long>& result) {
    if (idx == primes.size()) {
        result.push_back(cur);
        return;
    }
    long long p = primes[idx].first;
    int exp = primes[idx].second;
    long long mult = 1;
    for (int i = 0; i <= exp; ++i) {
        gen_factors(primes, idx + 1, cur * mult, result);
        mult *= p;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long a, b;
    cin >> a >> b;
    long long n = a * b;
    if (n == 1) {
        cout << 1 << '\n';
        return 0;
    }
    map<long long, int> fac_map;
    factorize(n, fac_map);
    vector<pair<long long, int>> primes(fac_map.begin(), fac_map.end());
    vector<long long> factors;
    gen_factors(primes, 0, 1, factors);
    sort(factors.begin(), factors.end());
    int pos = lower_bound(factors.begin(), factors.end(), a) - factors.begin();
    cout << pos + 1 << '\n';
    return 0;
}

AI解析

思路阐述

题意可以重新表述为:给定正整数 (a, b),令 (n = a \times b)。
把所有满足 (i \times j = n) 的有序正整数对 ((i, j)) 按照 (i) 从小到大排序,求 ((a, b)) 在其中的序号(从 1 开始)。

由于 (n) 固定,每个有序对 ((i, j)) 由第一个因子 (i) 唯一确定((j = n / i) 必然为整数),因此排序结果就是 (n) 的所有正因子按升序排列,而 ((a, b)) 的序号就是 (a) 在这个升序列表中的位置。

例子验证

  • 样例1:(a=9,\ b=74),(n=666)。
    666 的所有因子排序:1, 2, 3, 6, 9, 18, 37, 74, 111, 222, 333, 666。
    (a=9) 是第 5 个 → 输出 5 ✅

  • 样例2:(a=65,\ b=10),(n=650)。
    650 的因子排序:1, 2, 5, 10, 13, 25, 26, 50, 65, 130, 325, 650。
    (a=65) 是第 9 个 → 输出 9 ✅

因此问题转化为:

给定 (n = a \times b),求正整数 (a) 在 (n) 的所有正因子升序序列中的排名(下标从 1 开始)。

算法步骤

  1. 计算 (n = a \times b)(注意 (n \le 10^{18}),使用 __int128 或 long long 配合快速乘防止溢出)。
  2. 对 (n) 进行质因数分解。
    • 先用试除法去除小质数(如 2, 3, 5, 7, 11, …,一般到 100 即可)。
    • 剩余部分用 Miller–Rabin 判断是否为素数,若不是则用 Pollard–Rho 算法递归分解。
  3. 得到质因数分解形式:(n = p_1^{e_1} p_2^{e_2} \dots p_k^{e_k})。
  4. 通过 DFS 或迭代生成所有因子,存入数组 factors。
  5. 对 factors 排序。
  6. 用二分查找(lower_bound)找到 a 在 factors 中的位置(0‑based),输出 位置 + 1。

复杂度

  • 质因数分解:期望 (O(n^{1/4}))。
  • 因子个数 (d(n)) 在 (n \le 10^{18}) 时不超过约 (10^5)(实际上最大约 (103680))。
  • 生成与排序:(O(d(n) \log d(n))) 完全可行。

注意事项

  • 使用 __int128 进行乘法取模,避免溢出。
  • Miller–Rabin 选取固定基组(对 (< 2^{64}) 的数足够正确)。
  • Pollard–Rho 随机化,确保能分解合数。
  • 若 (n = 1),只有因子 1,输出 1。

代码实现(已随回答给出)

核心部分已在上面的代码中完整实现,包括:

  • mul_mod、pow_mod:快速乘与快速幂。
  • is_prime:Miller–Rabin 素性测试。
  • pollard_rho:Pollard–Rho 算法找因子。
  • factorize:递归分解并存入 map。
  • gen_factors:DFS 生成所有因子。
  • main:读入、分解、生成因子、排序、二分查找并输出排名。
原文