您的位置:首页 > 其它

1059. Prime Factors (25)解题报告

2016-10-30 17:04 423 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: