https://vjudge.net/problem/POJ-3421#author=GPT_zh
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 ≤ 2^20).
Output
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 ≤ 2^20)。
输出
对于每个测试用例,输出最大长度和该长度的 X 因子链的数量。
2
3
4
10
100
1 1
1 1
2 1
2 2
4 6
解答:
一道好题目 可以用多种方案解决
- 分解约数 dp得到最长路径和最长路径数目
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
vector<int> get_divisors(int x)
{
vector<int> res;
for (int i = 1; i <= x / i; i++)
if (x % i == 0)
{
res.push_back(i);
if (i != x / i) res.push_back(x / i);
}
sort(res.begin(), res.end());
return res;
}
int dp[10000][2];
void solve(const vector<int>& v) {
memset(dp, 0, sizeof dp);
dp[0][0] = 0; dp[0][1] = 1;
for (int i = 1; i < v.size(); i++) {
int vala = v[i];
for (int j = 0; j < i; j++) {
int valb = v[j];
if (vala % valb == 0 && dp[j][0] + 1 > dp[i][0]) {
dp[i][0] = dp[j][0] + 1;
dp[i][1] = dp[j][1];
}
else if (vala % valb == 0 && dp[j][0] + 1 == dp[i][0]) {
dp[i][1] += dp[j][1];
}
}
}
cout << dp[v.size() - 1][0] << " " << dp[v.size() - 1][1] << endl;
return;
}
int main()
{
int n;
while (cin >> n) {
vector<int> v = get_divisors(n);
solve(v);
}
return 0;
}
- 分解质因数 dfs获得最长路径的数目
// X-factor Chains POJ - 3421 挑战编程.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
using namespace std;
vector<int> primes;
void divide(int x)
{
for (int i = 2; i <= x / i; i++)
if (x % i == 0)
{
int s = 0;
while (x % i == 0) x /= i, s++;
//cout << i << ' ' << s << endl;
primes.push_back(i); primes.push_back(s);
}
if (x > 1) {
//cout << x << ' ' << 1 << endl;
primes.push_back(x); primes.push_back(1);
}
//cout << endl;
}
long long fac(int x) {
if (x == 1) return 1;
long long ret = 1;
ret *= x*fac(x - 1);
return ret;
}
int ans1 = 0;
long long ans2 = 0;
void dfs(int x) {
if (x >= ans1) {
ans2++;
return;
}
for (int i = 1; i < primes.size(); i += 2) {
if (primes[i] != 0) {
primes[i]--;
dfs(x + 1);
primes[i]++;
}
}
return;
}
int main()
{
int n;
while (cin >> n) {
primes.clear();
divide(n);
ans1 = 0;
// 选择所有素因子就是最长路径 = 素因子的幂和
for (int i = 1; i < primes.size(); i+=2) {
ans1 += primes[i];
}
ans2 = 0;
dfs(0);
cout << ans1 << " " << ans2 << endl;
/*
// 素因子的所有幂和的阶乘就是所有约数路径的组合
// 除以每个素因数的阶乘 也就是取出重复的排列组合
// 最后就是选取所有素因数(最长路径) 的路径数目
ans2 = fac(ans1); //数据范围2^20 最多20! long long 即可
for (int i = 1; i < primes.size(); i+= 2) {
ans2 = ans2 / fac(primes[i]);
}
cout << ans1 << " " << ans2 << endl;
*/
}
return 0;
}
- 分解质因数 使用排列组合直接得到最长路径数目
#include <iostream>
#include <vector>
using namespace std;
vector<int> primes;
void divide(int x)
{
for (int i = 2; i <= x / i; i++)
if (x % i == 0)
{
int s = 0;
while (x % i == 0) x /= i, s++;
//cout << i << ' ' << s << endl;
primes.push_back(i); primes.push_back(s);
}
if (x > 1) {
//cout << x << ' ' << 1 << endl;
primes.push_back(x); primes.push_back(1);
}
//cout << endl;
}
long long fac(int x) {
if (x == 1) return 1;
long long ret = 1;
ret *= x*fac(x - 1);
return ret;
}
int main()
{
int n;
while (cin >> n) {
primes.clear();
divide(n);
int ans1 = 0;
// 选择所有素因子就是最长路径
for (int i = 1; i < primes.size(); i+=2) {
ans1 += primes[i];
}
// 素因子的所有幂和的阶乘就是所有约数路径的组合
// 除以每个素因数的阶乘 也就是取出重复的排列组合
// 最后就是选取所有素因数(最长路径) 的路径数目
long long ans2 = fac(ans1); //数据范围2^20 最多20! long long 即可
for (int i = 1; i < primes.size(); i+= 2) {
ans2 = ans2 / fac(primes[i]);
}
cout << ans1 << " " << ans2 << endl;
}
return 0;
}