分解(n!)的质因数,输出相应的(p_i)和(c_i)。
其中(1leq nleq 10^6)。
考虑每一个质因子 (p) 在 (n!) 中出现的次数。显然,(1)~(n) 中包含 (p) 的个数为 (lfloorfrac{n}{p}
floor),包含 (p^2) 的个数为 (lfloorfrac{n}{p^2}
floor)...最后累加起来就行了。复杂度(O(nlogn))。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
ll n;
ll vis[N],p[N],c[N];
ll cnt ;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(ll i=2;i<=n;i++){
if(!vis[i]){
ll num = 0;
for(ll j=i;j<=n;j*=i) num+=n/j;
p[++cnt]=i;c[cnt]=num;
for(ll j=i*i;j<=n;j+=i) vis[j]=1;
}
}
for(int i=1;i<=cnt;i++) cout<<p[i]<<" "<<c[i]<<'
';
return 0 ;
}