G07 分解质因数 唯一分解定理 试除法

视频链接:https://www.bilibili.com/video/BV1md4y1u7do

 Luogu P2043 质因子分解

复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n;
int a[10001]; //质因子的个数

void decompose(int x){ //分解质因数
  for(int i=2; i*i<=x; i++)
    while(x%i==0) a[i]++, x/=i;
  if(x>1) a[x]++;
}
int main(){
  cin >> n;
  for(int i=2; i<=n; i++) decompose(i);
    for(int i=1;i<=n;i++)
    if(a[i]) cout<<i<<" "<<a[i]<<endl;
  return 0;
}
复制代码

 

posted @   董晓  阅读(824)  评论(4编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示