首页 > 代码库 > luogu P2043 质因子分解

luogu P2043 质因子分解

题目描述

对N!进行质因子分解。

输入输出格式

输入格式:

 

输入数据仅有一行包含一个正整数N,N<=10000。

 

输出格式:

 

输出数据包含若干行,每行两个正整数p,a,中间用一个空格隔开。表示N!包含a个质因子p,要求按p的值从小到大输出。

 

输入输出样例

输入样例#1:
10
输出样例#1:
2 83 45 27 1

说明

10!=3628800=(2^8)*(3^4)*(5^2)*7

 质因数分解..
#include<cstdio>const int maxn=100006;int n;int cnt[maxn];int f(int x){    int k=2;    while(k*k<=x)    {        while(x%k==0)         {            cnt[k]++;            x/=k;        }        k++;    }    if(x>1)cnt[x]++;}int  main(){    scanf("%d",&n);    for(int i=2;i<=n;i++)        f(i);    for(int i=2;i<=n;i++)        if(cnt[i])            printf("%d %d\n",i,cnt[i]);    return 0;}

 

luogu P2043 质因子分解