1059. Prime Factors (25)解题报告

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;

struct node {
    long long factor;
    int exp;
};

int main(void) {
    queue<node> q;
    long long num, nu, max;
    scanf("%lld", &num);
    nu = num;
    max = sqrt(num);
    bool *prime = new bool[max + 1];
    memset(prime, 0, sizeof(bool) * (max + 1));
    for (long long i = 2; i < max + 1; i++) {
        for (long long j = 2; j*i < max + 1; j++) {
            prime[j*i] = true;
        }
    }
    long long i = 2;
    while (num > 1) {
        while (i < max + 1 && prime[i]) {
            i++;
        }
        if (i == max + 1) {
            break;
        }
        if (!(num % i)) {
            node n;
            n.factor = i;
            n.exp = 0;
            while (!(num % i)) {
                n.exp++;
                num /= i;
            }
            q.push(n);
        }
        i++;
    }
    if (i == max + 1 && num >1) {
        node n;
        n.factor = num;
        n.exp = 1;
        q.push(n);
    }
    if (!q.size()) {
        printf("%lld=%d", num, num);
        return 0;
    }
    node tmp = q.front();
    q.pop();
    printf("%lld=", nu);
    if (tmp.exp > 1) {
        printf("%lld^%d", tmp.factor, tmp.exp);
    }
    else {
        printf("%lld", tmp.factor);
    }
    while (!q.empty()) {
        tmp = q.front();
        q.pop();
        if (tmp.exp > 1) {
            printf("*%lld^%d", tmp.factor, tmp.exp);
        }
        else {
            printf("*%lld", tmp.factor);
        }
    }
    putchar('\n');
    delete[] prime;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值