【Prime Land】

先求出x=pieix=\prod p_i^{e_i},然后对x1x-1分解质因数并输出。

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 3e4 + 10;

int power(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res *= a;
        a *= a;
        b >>= 1;
    }
    return res;
}

int T, k, p[N], e[N];
int x, total;
signed main() {
    cin >> T;
    while (T--) {
        cin >> k;
        x = 1;
        for (int i = 1; i <= k; i++) {
            cin >> p[i] >> e[i];
            x *= power(p[i], e[i]);
        }
        x--;
        total = 0;
        int tmp = x;
        for (int i = 2; i * i <= x; i++) {
            int cnt = 0;
            while (tmp % i == 0) {
                tmp /= i;
                cnt++;
            }
            if (cnt) {
                p[++total] = i;
                e[total] = cnt;
            }
        }
        if (tmp > 1) {  // 自己是质数的情况
            p[++total] = tmp;
            e[total] = 1;
        }
        for (int i = total; i >= 1; i--)
            cout << p[i] << " " << e[i] << " \n"[i == 1];
    }

    return 0;
}