X-factor Chains
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5326 | Accepted: 1683 |
Description
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.
Input
The input consists of several test cases. Each contains a positive integer X (X ≤ 220).
Output
For each test case, output the maximum length and the number of such X-factors chains.
Sample Input
2 3 4 10 100
Sample Output
1 1 1 1 2 1 2 2 4 6
Source
POJ Monthly--2007.10.06, ailyanlu@zsu
分解质因数,然后求一个组合数就成。
s=p1^a1*p2^a2*p3^a3.....pn^an
组合数为
:(a1+a2...an)!/(a1!*a2!...*an!)
/*
Problem: 3421 User: motefly
Memory: 3040K Time: 344MS
Language: G++ Result: Accepted
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=1048600;
int prime[maxn+5];
bool is_prime[maxn+6];
bool hash[maxn+5];
#define ll long long
int sieve(int n)
{
int p=0;
for(int i=0;i<=n;i++)
is_prime[i]=true;
is_prime[0]=is_prime[1]=0;
for(int i=2;i<=n;i++)
{
if(is_prime[i])
{
prime[p++]=i;
hash[i]=1;
for(int j=2*i;j<=n;j+=i)
is_prime[j]=0;
}
}
hash[1]=1;
return p;
}
ll f[21];
int a[21];
void init()
{
sieve(maxn);
f[1]=1;
for(int i=2;i<=20;i++)
f[i]=f[i-1]*i;
}
int main()
{
init();
ll n;
while(~scanf("%lld",&n)){
int k=0;
if(hash[n]==1)
{
printf("1 1\n");
continue;
}
for(int i=0;prime[i]<=n;i++)
{
if(n==1)
break;
a[k]=0;
if(n%prime[i]==0)
{
while(n%prime[i]==0)
{
n/=prime[i];
a[k]++;
}
k++;
}
}
int sum1=0;
ll sum2=1;
for(int i=0;i<k;i++)
{
sum1+=a[i];
sum2*=f[a[i]];
}
printf("%d %lld\n",sum1,f[sum1]/sum2);
}
}