题意:
求小于等于 n 的数的所有质因子的个数
思路:
n只有 100,得100以内的所有数进行质因子分解(打表处理),最后以 优先队列储存然后输出
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000000 + 7, maxd = (1<<18)-1, mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
int n;
int p[111][111] = {0};
priority_queue<int, vector<int>, greater<int> > que;
vector<int> prim;
bool vis[111];
void init() {
for(int i = 4; i < 111; i += 2)
vis[i] = 1;
for(int i = 3; i < 111; i += 2) {
if(!vis[i]) {
for(int j = i*i; j < 111; j += 2*i)
vis[j] = 1;
}
}
prim.push_back(2);
for(int i = 3; i < 111; ++i)
if(!vis[i]) {//cout << i << endl;
prim.push_back(i);}
for(int i = 2; i <= 100; ++i) {
int n = i;
for(int j = 0; j < prim.size(); ++j) {
if(prim[j] == 0) { p[i][prim[j]]++; break; }
if(n % prim[j] == 0) {
while(n % prim[j] == 0) {
p[i][prim[j]]++; n /= prim[j];
}
}
}
}
}
void solve() {
while(!que.empty()) que.pop();
for(int i = n; i >= 2; --i) {
for(int j = 0; j < prim.size(); ++j) {
for(int k = 0; k < p[i][prim[j]]; ++k)
que.push(prim[j]);
}
}
while(!que.empty()) {
int x = que.top(), cnt = 0;
while(!que.empty() && que.top() == x) { cnt++; que.pop(); }
printf("%d (%d)", x, cnt);
if(!que.empty()) printf(" * ");
else puts("");
}
}
int main() {
init();
int T;
scanf("%d", &T);
for(int tt = 1; tt <= T; ++tt) {
scanf("%d", &n);
printf("Case %d: %d = ", tt, n);
solve();
}
return 0;
}