POJ3421(质因数分解)
X-factor Chains
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6501 | Accepted: 2023 |
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
思路:将x进行质因数分解,X-factor Chains中(第因一项永为1,所以不计)第i项就是将质因数进行排列后前i项的积。所以length为质因数的个数。numbeu为质因数全排列的种数。相同质因数之间的排列要去重。
#include <iostream> #include <map> using namespace std; typedef unsigned long long ull; map<int,int> prime_factor(int n)//质因数分解 { map<int,int> res; for(int i=2;i*i<=n;i++) { while(n%i==0) { res[i]++; n/=i; } } if(n!=1) { res[n]++; } return res; } ull fact(int n) { ull res=1; for(int i=2;i<=n;i++) { res*=i; } return res; } int x; int main() { while(cin>>x) { map<int,int> res=prime_factor(x); int len=0; for(map<int,int>::const_iterator it=res.begin();it!=res.end();it++) { len+=it->second; } ull cnt=fact(len); for(map<int,int>::const_iterator it=res.begin();it!=res.end();it++) { cnt/=fact(it->second);//去重 } cout<<len<<" "<<cnt<<endl; } return 0; }