Codeforces 264 B. Good Sequences
2 seconds
256 megabytes
standard input
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.
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).
Print a single integer — the length of the longest good sequence.
5
2 3 4 6 9
4
9
1 2 3 5 6 7 8 9 10
4
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的更多相关文章
- codeforces 264 B. Good Sequences(dp+数学的一点思想)
题目链接:http://codeforces.com/problemset/problem/264/B 题意:给出一个严格递增的一串数字,求最长的相邻两个数的gcd不为1的序列长度 其实这题可以考虑一 ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- codeforces C. DZY Loves Sequences
http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...
- Codeforces 1144G Two Merged Sequences dp
Two Merged Sequences 感觉是个垃圾题啊, 为什么过的人这么少.. dp[ i ][ 0 ]表示处理完前 i 个, 第 i 个是递增序列序列里的元素,递减序列的最大值. dp[ i ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)
Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
随机推荐
- Delphi7通过superobject解析JSON
1.通过delphi程序访问PHP事先写好的webservice(查询功能),webservice返回json格式数据. 2.通过superobject读取json数据 得到效果如下: //深层级的访 ...
- Vue 组件化
根实例└─ TodoList ├─ TodoItem │ ├─ DeleteTodoButton │ └─ EditTodoButton └─ TodoListFooter ├─ ClearTodos ...
- python2.7.x的字符串编码到底什么鬼?(中文和英文的处理)
一直以来我其实一直对python的编码弄得非常晕,能正常编码,也能处理一些情况.但是始终不明白有些问题究竟为何出,原因是什么,为什么要这样用. 今天晚上正好好好研究了一番解答了自己心中的困惑. Q:p ...
- logstash 使用kafka范例
写入到kafka input { stdin { } } output { kafka { bootstrap_servers => "10.0.0.200:9092" to ...
- Git提交代码失败: empty ident name (for <>) not allowed
使用git提交代码,报错如下: 下午2:56 Commit failed with error 0 files committed, 1 file failed to commit: 升级 empty ...
- 如果filename的value有值 说明支持存储
如果filename的value有值 说明支持存储
- CF1045G
CF1045G 看了下题解,动态开点线段树,好像挺难的 #include <map> #include <cstdio> #include <algorithm> ...
- BZOJ4771七彩树——可持久化线段树+set+树链的并+LCA
给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节 点的颜色为c[i].如果c[i]=c[j],那么我们认为点i和点j拥有相同的颜色.定义dept ...
- BZOJ3590 SNOI2013Quare(状压dp)
可能作为最优解的边双都可以这样生成:初始时边双内只有一个点,每次选取边双内部两点(可以相同)和一个当前不在边双内的点集,以该两点为起止点找一条链(当然如果两点相同就是个环)将点集串起来,加入边双.状压 ...
- 最大获利 HYSBZ - 1497 (最大权闭合图)
最大权闭合图: 有向图,每个点有点权,点权可正可负.对于任意一条有向边i和j,选择了点i就必须选择点j,你需要选择一些点使得得到权值最大. 解决方法: 网络流 对于任意点i,如果i权值为正,s向i连容 ...