VK Cup 2015 - Finals, online mirror

F. Clique in the Divisibility Graph

题目传送:Clique in the Divisibility Graph

解法:筛法+DP

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 1000005;

int dp[maxn];//dp[i]表示以i为结尾的数能够产生的最大size,状态转移是在筛法中进行的 
int n, x;

int main() {
    cin >> n;
    for(int i = 0; i < n; i ++) {
        scanf("%d", &x);
        dp[x] = 1;
    }

    for(int i = 1; i < maxn; i ++) {
        if(dp[i] != 0)
        for(int j = 2 * i; j < maxn; j += i) {
            if(dp[j] != 0) {
                dp[j] = max(dp[j], dp[i] + 1);
            }
        }
    }

    int ans = 0;
    for(int i = 0;  i < maxn; i ++) {
        ans = max(ans, dp[i]);
    }

    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(ACM,codeforces,VKcup)