Codeforces 566F Portal
As you are must know, the maximum clique problem in a arbitrary graph is np-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind-a clique in a non-directed graph is a subset of the vertices of a graph, such this an Y. Vertices of this subset is connected by an edge. In particular, an empty set of vertexes and a set consisting of a vertex, is cliques.
Let's define a divisibility graph for a set of positive integers a?=? {A1,?A2,?...,? an} as follows. The vertices of the given graph is numbers from set A, and the numbers AI and AJ (I?≠?J) is connected by an edge if and Only if either AI was divisible by AJ, or AJ is divisible by AI.
You is given a set of non-negative integers a. Determine the size of a maximum clique in a divisibility graph for set a.
Input
The first line contains an integer n (1?≤?n?≤?106), that sets the size of set A.
The second line contains n distinct positive integers a1,?a2,?...,? an (1?≤?ai?≤?106)-elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number-the maximum size of a clique in a divisibility graph for set a.
Sample Test (s)
Input
8
3 4 6 8 10 18 21 24
Output
3
The main idea: to ask for an array sequence that requires the longest multiplying sequence such as 3 6 18.
Problem-Solving ideas: DP value,, see the code:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>Const intN =1e6+Ten;using namespace STD;intDp[n], A;intMain () {intN, MAXN; while(~scanf("%d", &n)) {memset(DP,0,sizeof(DP)); MAXN =0; for(inti =0; I < n; i++) {scanf("%d", &a); dp[a]++; MAXN = Max (MAXN, Dp[a]); for(intj = A *2; J <= N; J + = a) dp[j] = max (Dp[j], dp[a]); }printf("%d\n", MAXN); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 566 F. Clique in the divisibility Graph