http://codeforces.com/contest/566/problem/F

F. Clique in the Divisibility Graph
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.

Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are 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 are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.

You are 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 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.

Examples
input
8
3 4 6 8 10 18 21 24
output
3
Note

In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.

设DP[i]表示第i个数字结尾的最大合法情况。

那么递推过来的话,要在前i - 1个数字中,是a[i]约数的,才能递推过来dp[i],那么需要把a[i]分解因子。这样要sqrtn复杂度。超时。

可以考虑以第a[i]个数递推去后面,就是去更新[i + 1,以后的数字。

那么只有k * a[i]的才能递推过去。

但是有些会重复,

3 4 24这样,24既可以从3递推过来,也可以从4递推过来。那么娶个max即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + ;
int a[maxn];
int add[maxn];
int calc(int val) {
int en = (int)sqrt(val * 1.0);
int res = ;
for (int i = ; i <= en; ++i) {
if (val % i == ) {
res = max(res, add[i]);
res = max(res, add[val / i]);
}
}
return res;
}
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
int ans = ;
for (int i = ; i <= n; ++i) {
add[a[i]] += ;
for (int j = * a[i]; j <= maxn - ; j += a[i]) {
add[j] = max(add[j], add[a[i]]);
}
ans = max(ans, add[a[i]]);
}
// for (int i = 1; i <= n; ++i) {
// printf("%d ", add[a[i]]);
// }
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

F. Clique in the Divisibility Graph DP的更多相关文章

  1. Codeforces.566F.Clique in the Divisibility Graph(DP)

    题目链接 \(Description\) 给定集合\(S=\{a_1,a_2,\ldots,a_n\}\),集合中两点之间有边当且仅当\(a_i|a_j\)或\(a_j|a_i\). 求\(S\)最大 ...

  2. 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏

    Clique in the Divisibility Graph time limit per test1 second memory limit per test256 megabytes inpu ...

  3. Codeforces 566F Clique in the Divisibility Graph

    http://codeforces.com/problemset/problem/566/F 题目大意: 有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 (a[i] mod a ...

  4. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

  5. UVA11324 The Largest Clique[强连通分量 缩点 DP]

    UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...

  6. POJ 1745 Divisibility (线性dp)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10598   Accepted: 3787 Des ...

  7. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  8. AGC 016 F - Games on DAG(状压dp)

    题意 给你一个有 \(n\) 个点 \(m\) 条边 DAG 图,点的标号和拓扑序一致. 现在有两个人进行博弈,有两个棋子分别在 \(1, 2\) 号点上,需要不断移动到它指向的点上. 如果当前两个点 ...

  9. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) F - Uniformly Branched Trees 无根树->有根树+dp

    F - Uniformly Branched Trees #include<bits/stdc++.h> #define LL long long #define fi first #de ...

随机推荐

  1. linux 进程学习笔记-进程状态

    task_struct的state字段记录的进程的状态,可分为如下几种: #define TASK_RUNNING 0 可运行状态.这是 “进程正在被CPU运行” 和 “进程正在可运行队列中等待被CP ...

  2. inteliji ---idea 如何创建scala程序配置

    首先创建一个新的工程: #####################################################################################

  3. OGG 进程清除、重建

    背景描述:有时候OGG进程同步出现问题,症状某些进程起不来,而且问题一时半会儿解决不了.最简单的办法是:用数据泵初始化数据,OGG进程重建. 1.查看源端.目的端的进程状态. (作者OGG进程是正常的 ...

  4. 2012年浙大:Head of a Gang

    题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  5. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  6. Struts&nbsp;result&nbsp;param详细设置

    <</span>result type="json"> <</span>param name="root">da ...

  7. 使用API修改控制台输出的颜色(前景色和背景色)

    1.api原型 SetConsoleTextAttribute BOOL WINAPI SetConsoleTextAttribute( _In_ HANDLE hConsoleOutput, // ...

  8. socket函数的使用方法(参数详解)

    socket函数的使用方法如下: int socket(int domain, int type, int protocol); 在参数表中,domain指定使用何种的地址类型,比较常用的有: PF_ ...

  9. HRBUST - 1819 石子合并问题--圆形版(区间dp+环形+四边形优化)

    石子合并问题--圆形版 在圆形操场上摆放着一行共n堆的石子.现要将石子有序地合并成一堆.规定每次只能选相邻的两堆合并成新的一堆,并将新的一堆石子数记为该次合并的得分.请编辑计算出将n堆石子合并成一堆的 ...

  10. Sharepoint2013搜索学习笔记之修改搜索拓扑(三)

    搜索服务新建好之后可以从管理中心,应用程序管理页面,进入搜索服务的管理页面,进入管理页面之后可以看到当前sharepoint场的搜索拓扑结构. 如果sharepoint场内有多台服务器,需要将搜索组件 ...