1 条题解
-
0
题意分析
-
问题目标:
- 给定两个正整数和,找到一个最小的质数集合,使得和都可以表示为中质数的幂的乘积。
- 在由定义的维空间中,计算和对应的点之间的曼哈顿距离。
-
关键点:
- 最小空间维度是和的所有质因数的并集的大小。
- 距离是和在每个质因数维度上的指数差的绝对值之和。
解题思路
-
质因数分解:
- 对和分别进行质因数分解,得到它们的质因数及其指数。
- 例如,,。
-
合并质因数集合:
- 将和的质因数合并,得到唯一的质数集合。
- 对于和,,因此。
-
计算距离:
- 对于中的每个质数,计算和在的指数上的差的绝对值。
- 将所有差的绝对值相加,得到曼哈顿距离。
- 例如:
- ()
- ()
- ()
- 距离。
-
处理其他测试用例:
- 对于和:
- ,。
- 距离计算:
- ()
- ()
- ()
- ()
- ()
- 距离。
- 对于和:
C++代码实现
#include <iostream> #include <set> using namespace std; set<int>sum; void get(int x){ for (int i = 2; i*i <= x; i++){ while (x%i == 0){ sum.insert(i); x /= i; } } if (x > 1){ sum.insert(x); } } int main() { int n, m, kase = 1; while (cin >> n >> m&&n&&m){ sum.clear(); get(n); get(m); int ans = 0; set<int>::iterator it = sum.begin(); for (; it != sum.end(); it++){ int a = 0, b = 0; int temp = *it; while (n%temp == 0)a++, n /= temp; while (m%temp == 0)b++, m /= temp; ans += (a > b ? a - b : b - a); } cout << kase++ << ". " << sum.size() << ":" << ans << endl; } }
-
- 1
信息
- ID
- 2974
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者