B. Good Sequences
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.

Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:

  • The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1).
  • No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q).
  • All elements of the sequence are good integers.

Find the length of the longest good sequence.

Input

The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105; ai < ai + 1).

Output

Print a single integer — the length of the longest good sequence.

Examples
input
5
2 3 4 6 9
output
4
input
9
1 2 3 5 6 7 8 9 10
output
4
Note

In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.

题目链接

题意 

给一个严格递增的序列,求出最长的相邻元素不互质的递增序列。

分析 

不互质则必有相同的因子,那么只要相邻的有相同的因子,那么就可以一段拼一段,从而拼出最长的。先预处理每个数的因子,对于当前序列,处理每个数的因子出现的次数,用来更新维护dp[i](最后一对元素的公共因子为i的最长长度),即先从该数的所以因子中选出dp值最大的那个,这就说明当前这个数可以拼接上去,形成新的序列,长度+1,此时再更新当前数的所有因子的dp值。最后再遍历一边,找出最大长度。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn = 1e5+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
vector<int> p[maxn];
int n;
void init(){
for(int i=;i<maxn;i++){
for(int j=i;j<maxn;j+=i){
p[j].pb(i);
}
}
}
int a[maxn];
int d[maxn];
int main(){
// freopen("in.txt","r",stdin);
init();
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++){
int maxx=;
for(int j=;j<p[a[i]].size();j++){
// cout<<p[a[i]][j]<<" ";
maxx = max(maxx,d[p[a[i]][j]]);
}
for(int j=;j<p[a[i]].size();j++){
d[p[a[i]][j]]=maxx+;
}
// puts("");
}
int maxx = ;
for(int i=;i<maxn;i++) maxx = max(maxx,d[i]);
cout<<maxx;
return ;
}

Codeforces 264 B. Good Sequences的更多相关文章

  1. codeforces 264 B. Good Sequences(dp+数学的一点思想)

    题目链接:http://codeforces.com/problemset/problem/264/B 题意:给出一个严格递增的一串数字,求最长的相邻两个数的gcd不为1的序列长度 其实这题可以考虑一 ...

  2. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  3. codeforces 446A DZY Loves Sequences

    vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...

  4. codeforces C. DZY Loves Sequences

    http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...

  5. Codeforces 1144G Two Merged Sequences dp

    Two Merged Sequences 感觉是个垃圾题啊, 为什么过的人这么少.. dp[ i ][ 0 ]表示处理完前 i 个, 第 i 个是递增序列序列里的元素,递减序列的最大值. dp[ i ...

  6. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  7. CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)

    Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...

  8. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  9. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

随机推荐

  1. Docker for windows 入门一(下载安装)

    预安装条件,可以查阅官方文档,本人是Win10 x64(必要条件)教育版+开启Hyper-V(Feature特性),具体可参考云栖社区的文章: https://yq.aliyun.com/articl ...

  2. css3 @media 实现响应式布局

    使用css3的@media,可以实现针对不同媒体.不同分辨率的响应式布局. 方法1:根据不同分辨率使用不同css文件 <link rel="stylesheet" media ...

  3. 用Axios Element 实现全局的请求 loading

        Kapture 2018-06-07 at 14.57.40.gif demo in github 背景 业务需求是这样子的,每当发请求到后端时就触发一个全屏的 loading,多个请求合并为 ...

  4. Jquery Jquery对象和DOM对象的微妙联系

    声明变量 var  $variable=             Jquery 对象: var  varibake=             DOM对象: var $cr= $("#id&q ...

  5. python 协程库gevent学习--gevent数据结构及实战(四)

    一不留神已经到第四部分了,这一部分继续总结数据结构和常用的gevent类,废话不多说继续. 1.Timeout错误类 晚上在调试调用第三方接口的时候,发现有些接口耗时非常多,觉得应该有个超时接口来限制 ...

  6. calico实现docker容器内部的网络链接

    calico官网 https://www.projectcalico.org// calico介绍 http://www.sdnlab.com/17161.html calico网络 环境 系统   ...

  7. CentOS常用包安装

    安装rz和sz命令: yum install lrzsz 安装unzip:yum install unzip

  8. Android 访问 Webapi 更新UI

    首先,写一个访问webapi的工具类 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import or ...

  9. hdu 4897 Little Devil I (树链剖分+线段树)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...

  10. day5 列表

    列表 查 索引(下标),默认从0开始 切片 .count 查某个元素的出现次数 .index 根据内容找元素的对应索引位置 增加 .append() 追加在最后 .insert(index,'内容') ...