唯一分解定理

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值