POJ 3421
阿新 • • 發佈:2019-02-13
因為題目裡要求Xi | Xi+1,而Xm又限定為X,所以我們可以想到Xm-1是X除以其某個約數得到的,Xm-1也是一樣。由此我們可以知道“X-factor Chains”是通過不斷乘以X的約數得到的,為了長度最大,所以約數必須是素數。通過記錄有哪些素因數,以及素因子的數量,我們就可以得到鏈的長度。
而長度最大的鏈的數量則是這些數的排列數,公式為素因子個數之和的階乘/每個素因子個數的階乘。
程式碼(G++):
#include <iostream>
#include <map>
using namespace std;
map<int , int> res;
map<int, int>::iterator mi;
long long fact(int n)
{
long long v;
if(n == 0 || n == 1) return 1;
else{
v = n;
for(int i=2; i<n; i++)
{
v *= i;
}
return v;
}
}
int main()
{
int x, sum;
long long ans;
while (cin>>x)
{
sum = 0;
res.clear();
for(int i=2; i*i<=x; i++)
{
while(x%i == 0)
{
x /= i;
++res[i];
++sum;
}
}
if(x != 1)
{
++res[x];
++sum;
}
ans = fact(sum);
for (mi=res.begin(); mi!=res.end(); mi++)
{
ans /= fact(mi->second);
}
cout<<sum<<' '<<ans<<endl;
}
return 0;
}
題目:
X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
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