阶数分解
思路 :
- 先得到\(10^6\)内的素数.
- 对每个素数p, 枚举n含p的几次方.
\(n/p+n/p^2+n/p^3+...\)
\(n/p\)表示1~n中有多少个数能被\(n\)整除, \(n/p^2\)表示1~n中有多少个数能被\(p^2\)整除,如此累加,得到的就是\(n!\)总共含p因子的个数.
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 1e6 + 10;
int p[N], cnt;
bool st[N];
void init() {
for (int i = 2; i < N; ++i) {
if (!st[i]) p[cnt++] = i;
for (int j = 0; p[j] * i < N; ++j) {
st[p[j] * i] = true;
if (i % p[j] == 0) break;
}
}
}
int main() {
//freopen("in.txt", "r", stdin);
IO;
init();
int n;
cin >> n;
for (int i = 0; p[i] <= n; ++i) {
int s = 0, q = p[i];
for (int j = n; j; j /= q) s += j / q;
cout << q << ' ' << s << '\n';
}
return 0;
}