#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e8 + 10;
const int maxp = 1e7 + 10;
int vis[maxn];
int p_cnt;
int prime[maxp];
void Eratosthenes(int n){
int m = sqrt(n + 0.5);
memset(vis,0,sizeof(vis));
for(int i = 2; i <= m; i++) if(!vis[i])
for(int j = i*i; j <= n; j+=i) vis[j] = 1;
p_cnt = 0;
for(int i = 2; i <= n; i++)
if(!vis[i])
prime[p_cnt++] = i;
}
int count_[maxp];
int FTA(int n){///这里的返回类型int是为了main函数里面检验方便,可以写成void型;
int tmp = n;
int cnt = 0;
while(tmp != 1){
while(!(tmp % prime[cnt])){
tmp /= prime[cnt];
count_[cnt]++;
}
cnt++;
}
return cnt;///返回的值cnt是表示用到了第几个素数;
}
int main()
{
///Eratosthenes说明了只能分解小于1e7的数;
Eratosthenes(1e7+10);///这个一定要出现在主函数中,是个预处理,不可少;
int n;
while(scanf("%d",&n) != EOF){
memset(count_,0,sizeof(count_));
int cnt = FTA(n);///调用FTA对n进行唯一分解;
///检验是否分解的正确;
for(int i = 0;i <= cnt; i++){
if(count_[i]){
printf("%d^%d ",prime[i],count_[i]);
}
}
printf("\n");
}
return 0;
}
唯一分解定理
最新推荐文章于 2023-04-03 10:17:12 发布