题目:
1295. X的因子链 - AcWing题库
输入样例:
2
3
4
10
100
输出样例:
1 1
1 1
2 1
2 2
4 6
思路:
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;typedef long long LL;const int N = (1 << 20) + 10;int primes[N], cnt;
int minp[N];
bool st[N];//判断是否为质数//求n以内所以质数
void get_primes(int n)
{for (int i = 2; i <= n; i++) {if (!st[i]) //是质数{minp[i] = i;primes[cnt++] = i;}for (int j = 0; primes[j] * i <= n; j++){int t = primes[j] * i;//是质数的倍数st[t] = true;//不是质数,是合数minp[t] = primes[j];//合数t的最小质数if (!t)break;//防止死循环}}
}
int main()
{get_primes(N-1);int num[N];//每种质因子的个数int x;while (scanf("%d", &x) != EOF){int k=0; //质因子种类数量int total = 0;//质因子总数while (x > 1){int p = minp[x];num[k]=0;while (x % p == 0){x /= p;total++;num[k]++;}k++;}LL res = 1;for (int i = 1; i <= total; i++)res *= i;for (int i = 0; i < k; i++)for (int j = 1; j <= num[i]; j++)res /= j;cout << total <<" "<< res << endl;}
}