POJ 3421 X-factor Chains (约数枚举)

X-factor Chains
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5605   Accepted: 1770

Description

Given a positive integer X, an X-factor chain of length m is a sequence of integers,

1 = X0X1X2, …, 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

枚举约数 然后dp即可

AC代码如下:

//
//	POJ 3421 X-factor Chains
//
//  Created by TaoSama on 2015-03-31
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, len[1500], dp[1500];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(cin >> n) {
        vector<int> factor;
        for(int i = 1; i * i <= n; ++i) {
            if(n % i == 0) {
                factor.push_back(i);
                if(i != n / i)  factor.push_back(n / i);
            }
        }
        sort(factor.begin(), factor.end());
        int sz = factor.size();
        for(int i = 0; i < sz; ++i) {
            len[i] = dp[i] = 1;
            for(int j = 0; j < i; ++j) {
                if(factor[i] % factor[j] == 0) {
                    if(len[i] < len[j] + 1) {
                        len[i] = len[j] + 1;
                        dp[i] = dp[j];
                    } else if(len[i] == len[j] + 1)
                        dp[i] += dp[j];
                }
            }
        }
        cout << len[sz - 1] - 1 << ' ' << dp[sz - 1] << endl;
    }
    return 0;
}


你可能感兴趣的:(POJ 3421 X-factor Chains (约数枚举))