题解 | #Prime Land#

Prime Land

https://ac.nowcoder.com/acm/contest/21094/D

【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;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 13:35
虽然不怎么光彩,经过这件事,可能我真的要去认同“面试八股文早该淘汰!不会用AI作弊的程序员=新时代文盲!”这句话了
HellowordX:Ai的出现是解放劳动力的,不是用来破坏公平竞争环境的,这样下去,轻则取消所有线上面试,严重了会影响整个行业对所有人产生影响,企业会拉高入职考核各种离谱考核会层出不穷
你找工作的时候用AI吗?
点赞 评论 收藏
分享
认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务