此处 是Python代码,思路与本代码相同,但是TLE
#include <iostream>
#include <vector>
using namespace std;
// 判断一个数是否为质数
bool prime(long long n) {
for (int x = 2; x*x <= n; x++) {
if (n % x == 0) {
return false;
}
}
return true;
}
int main() {
long long a;
cin >> a;
vector<long long> list;
if(prime(a)==1){
cout<<a;
return 0;
}
for (int i = 2; i <= a; i++) {
if (prime(i)) {
while (a % i == 0) {
list.push_back(i);
a /= i;
}
}
}
long long i = 0;
while (i!= list.size()) {
int count = 0;
int num = list[i];
for (int j = i; j < list.size(); j++) {
if (list[j] == num) {
count++;
}
}
if (count!= 1) {
cout << num << "^" << count;
}
else {
cout << num;
}
i += count;
if (i!= list.size()) {
cout << " * ";
}
}
return 0;
}
提交记录
代码对质数判断进行了进一步修改,将指质数判断部分改成了 O(n) 的时间复杂度,补充了long long
仅质数特判时能通过,40分
之前提交过一份Python代码,同样TLE
问:有没有更加快的算法?还是只能通过加速输入输出通过本题?
这里是全部提交记录