1 条题解

  • 0
    @ 2025-5-22 19:48:01

    题意分析

    1. 问题目标

      • 给定两个正整数AABB,找到一个最小的质数集合SS,使得AABB都可以表示为SS中质数的幂的乘积。
      • 在由SS定义的S|S|维空间中,计算AABB对应的点之间的曼哈顿距离。
    2. 关键点

      • 最小空间维度XXAABB的所有质因数的并集的大小。
      • 距离DDAABB在每个质因数维度上的指数差的绝对值之和。

    解题思路

    1. 质因数分解

      • AABB分别进行质因数分解,得到它们的质因数及其指数。
      • 例如,168=23×31×71168 = 2^3 \times 3^1 \times 7^1882=21×32×72882 = 2^1 \times 3^2 \times 7^2
    2. 合并质因数集合

      • AABB的质因数合并,得到唯一的质数集合SS
      • 对于168168882882S={2,3,7}S = \{2, 3, 7\},因此X=3X = 3
    3. 计算距离

      • 对于SS中的每个质数pp,计算AABBpp的指数上的差的绝对值。
      • 将所有差的绝对值相加,得到曼哈顿距离DD
      • 例如:
        • 31=2|3 - 1| = 2p=2p = 2
        • 12=1|1 - 2| = 1p=3p = 3
        • 12=1|1 - 2| = 1p=7p = 7
        • 距离D=2+1+1=4D = 2 + 1 + 1 = 4
    4. 处理其他测试用例

      • 对于770770792792
        • 770=21×51×71×111770 = 2^1 \times 5^1 \times 7^1 \times 11^1
        • 792=23×32×111792 = 2^3 \times 3^2 \times 11^1
        • S={2,3,5,7,11}S = \{2, 3, 5, 7, 11\}X=5X = 5
        • 距离计算:
          • 13=2|1 - 3| = 2p=2p = 2
          • 02=2|0 - 2| = 2p=3p = 3
          • 10=1|1 - 0| = 1p=5p = 5
          • 10=1|1 - 0| = 1p=7p = 7
          • 11=0|1 - 1| = 0p=11p = 11
          • 距离D=2+2+1+1+0=6D = 2 + 2 + 1 + 1 + 0 = 6

    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
    上传者