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 ...
随机推荐
- 77 Linux commands and utilities you'll actually use
https://searchdatacenter.techtarget.com/tutorial/77-Linux-commands-and-utilities-youll-actually-use
- Delphi/XE2 使用TIdHttp控件下载Https协议服务器文件[转]
之前的一篇博文详细描述了使用TIdhttp控件下载http协议的文件,在我项目的使用过程中发现对于下载Https协议中的文件与Http协议的文件不同,毕竟Https在HTTP协议基础上增加了SSL协议 ...
- SQLSERVER 2014 SP1 的服务器 日志文件无法收缩的处理
1. 公司一台服务器 日子会文件到了 100g+ 但是无法收缩 2. 根据同事的经验进行验证 dbcc loginfo 单独看改数据库的 dbcc loginfo("CWBASEGS60&q ...
- Linux查看版本
最简单的命令 lsb_release -a 查看机器名 hostname 查看内核版本 uname -r 红帽 centos 查看版本 cat /etc/redhat-release ubuntu 查 ...
- bat脚本的写法
当你每次都要输入相同的命令时,可以把这么多命令存为一个批处理,从此以后,只要运行这个批处理,就相当于打了几行.几十行命令.下面以Nginx服务的停止脚本为例写一个bat批处理文件: 1.新建nginx ...
- entity framework 缓存干扰的数据不一致问题
本来EF的设计上DbContext就应该是一个短暂存在的对象,所以它默认才开了缓存和自动检测对象修改之类的功能,而且不是线程安全的.理想的生命周期应该是处理一次请求使用一个context,全局使用会带 ...
- BZOJ2017[USACO 2009 Nov Silver 1.A Coin Game]——DP+博弈论
题目描述 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 <= N <= 2,000)枚硬币的堆栈放在地上,从堆顶数起的第I枚硬币的 ...
- Sightseeing tour HDU - 1956(混合欧拉回路)
题意: 有n个点,m条边,其中有单向边和双向边,求是否存在欧拉回路 解析: 刚开始想...判断一下每个点的度数不就好了...emm..还是年轻啊.. 判断是解决不了问题的,因为可能会有某两个点冲突,比 ...
- MT【57】2017联赛一试解答倒数第二题:一道不等式的最值
注:康拓诺维奇不等式的应用
- 【 HDU 4936 】Rainbow Island (hash + 高斯消元)
BUPT2017 wintertraining(15) #5B HDU - 4936 2014 Multi-University Training Contest 7 F 题意 直接看官方的题意和题解 ...