1. 程式人生 > >luogu P2043 質因子分解

luogu P2043 質因子分解

code turn while 進行 輸入 left bsp sca main

題目描述

對N!進行質因子分解。

輸入輸出格式

輸入格式:

輸入數據僅有一行包含一個正整數N,N<=10000。

輸出格式:

輸出數據包含若幹行,每行兩個正整數p,a,中間用一個空格隔開。表示N!包含a個質因子p,要求按p的值從小到大輸出。

輸入輸出樣例

輸入樣例#1:
10
輸出樣例#1:
2 8
3 4
5 2
7 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 質因子分解