题目描述
Given a positive integer X, an X-factor chain of length m is a sequence of integers,
1 = X0, X1, X2, …, Xm = X
satisfying
Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.
Now we are interested in the maximum length of X-factor chains and the number of chains of such length.
输入
The input consists of several test cases. Each contains a positive integer X (X ≤ 220).
输出
For each test case, output the maximum length and the number of such X-factors chains.
样例输入
2 3 4 10 100
样例输出
1 1 1 1 2 1 2 2 4 6
翻译
给定正整数 X,长度为 m 的 X 因子链是整数序列,1 = X0, X1, X2, …, Xm = X 满足 Xi < Xi+1 且 Xi |Xi+1 其中 a |b 表示 a 完全分为 b。现在我们感兴趣的是 X 因子链的最大长度以及该长度的链的数量。输入 输入由多个测试用例组成。每个都包含一个正整数 X (X ≤ 220)。输出 对于每个测试用例,输出此类 X 因子链的最大长度和数量。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int INF_INT=0x3f3f3f3f;
const LL INF_LL=0x3f3f3f3f3f3f3f3f;
LL factorial[21];
bool is_prime[10000];
int prime[10000];
int cnt;
int num[10000];
int k;
void pre()
{
factorial[0]=factorial[1]=1;
for(int i=2;i<=20;i++) factorial[i]=factorial[i-1]*i;
for(int i=2;i<=9000;i++) is_prime[i]=true;
for(int i=2;i<=9000;i++)
{
if(is_prime[i])
{
prime[cnt++]=i;
for(int j=(i<<1);j<=9000;j+=i) is_prime[j]=false;
}
}
}
void prime_factor(int x)
{
k=0,num[k]=0;
LL sum=0;
for(int i=0;i<cnt;i++)
{
while(!(x%prime[i])) x/=prime[i],num[k]++;
sum+=num[k];
k++,num[k]=0;
}
if(x!=1) sum++;
printf("%lld ",sum);
sum=factorial[sum];
for(int i=0;i<k;i++) sum/=factorial[num[i]];
printf("%lld\n",sum);
}
int main()
{
pre();
int x;
while(~scanf("%d",&x)) prime_factor(x);
return 0;
}