Codeforces题号:#264B

出处: Codeforces

主要算法:DP

难度:4.8

思路分析:

  这题DP太难了……

  最终的解法是,令f[i]表示存在因子i的一个数作为子序列结尾的子序列的最大长度。(定义都好难懂啊……)

  现在想一想怎么转移……首先先预处理出对于每一个数a[i]的所有因数(不含1)。既然相邻的两个数不能是互质的,我们只需要判断相邻两个数的最大公约数是否大于1就好了。

  依次枚举a[i],在这过程中枚举刚才处理好的a[i]的因数。为什么要枚举因数? 为了看看我能够接到哪些数后面。因为所有因数如果在之前的数里出现过,那么当前的a[i]就可以接上去,因为因数大于1,所以肯定不会有互质的这种情况。由于我之前已经记录了这些因数所能够达到的最大长度,那么再加上我自己,长度就又可以+1了——这一点有点像LIS,但又有点不同:选择所有因数中f值最大的去接上去以后,所有因数的f值都可以更新成与其中最大值相同的。因为最后一个数本身就含有这些因数。

  所以最后的答案究竟是什么?我认为是f[2]~f[a[n]中的最大值。然而这样会WA,改成f[1]~f[a[n]]就AC了。这一点一直想不通,是因为本身就会输入1吗?为什么输入1就会影响答案呢?而且我的AC代码对于数据1 1 1 1 1竟然输出了5,不应该是1吗?1和1是互质的!还请大神帮忙解答一下……

代码注意点:

  无

Code

/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
int n,cur,_max,ans;
int a[N],f[N];
vector <int> factor[N];
inline void MadeFactor(int I){
int x = a[I];
int lim = floor(sqrt(x));
factor[I].push_back(x);
for(int i = ; i <= lim; ++i){
if(x % i == ){
factor[I].push_back(i);
if(i!=x/i) factor[I].push_back(x/i);
}
}
}
int main(){
// freopen(".in","r",stdin);
n = r;
for(int i = ; i <= n; ++i){
a[i] = r;
MadeFactor(i);
   }
for(int i = ; i <= n; ++i){
_max = ;
int sz = factor[i].size();
for(int j = ; j < sz; ++j){
cur = factor[i][j];
++f[cur];
_max = Max(_max, f[cur]);
}
for(int j = ; j < sz; ++j){
f[factor[i][j]] = _max;
}
}
for(int i = ; i <= a[n]; ++i){
ans = Max(ans, f[i]);
}
printf("%d",ans);
return ;
}

Codeforces264 B. Good Sequences的更多相关文章

  1. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  3. 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)

    Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...

  4. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  6. Python数据类型之“序列概述与基本序列类型(Basic Sequences)”

    序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...

  7. Extract Fasta Sequences Sub Sets by position

    cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...

  8. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  9. MOOCULUS微积分-2: 数列与级数学习笔记 1. Sequences

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

随机推荐

  1. heb Daz

    Asatras soi bib Daz! gos la haik ri, dewoa gos mi haik quri. soi Fong d cuup va ti Chusan, imps Dabo ...

  2. hybrid App cordova打包webapp PhoneGap

    Hybrid APP基础篇(一)->什么是Hybrid App APP三种开发模式--之--HybridApp解决方案 Hybrid App开发 四大主流平台分析 Hybrid App 开发模式 ...

  3. 【学习总结】Git学习-参考廖雪峰老师教程十-自定义Git

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  4. 【Java基础】求出1-100之间偶数和

    结果:

  5. CentOS7安装Jenkins,使用war方式直接运行或用yum方式安装运行

    jenkins最简单的安装方式呢,就是直接去官网下载jenkins的war包,把war丢到tomcat里运行,直接就能打开了. Jenkins官网:https://jenkins.io/downloa ...

  6. 在layui中使用ajax不起作用

    又是一个坑,坑了我一个下午.在layui插件中使用jquery的ajax请求,一点反应都没有,不管是改成get还是post请求,后台毫无反应,前端谷歌调试也没有报半点错. js代码如下: layui. ...

  7. js获取数组中相同元素数量

    <script> var array = new Array(1,2,5,1,4,4,2,3,5,1,1,5,5,5,6,7,3,9,9,10); var arr = new Array( ...

  8. java学习之—栈匹配字符串符号

    /** * 栈 * Create by Administrator * 2018/6/11 0011 * 上午 10:20 **/ public class StackR { private int ...

  9. Shell 编程和Python编程的那些不同之处(一)

    循环 shell中for循环的表现形式: 1.数字段形式 for i in {1..10};do  echo $i;done 还支持按规定的步数进行跳跃的方式实现列表for循环,例如计算1-100内所 ...

  10. tomcat 与 nginx,apache的区别

    tomcat 与 nginx,apache的有什么区别 回答一: 题主说的Apache,指的应该是Apache软件基金会下的一个项目——Apache HTTP Server Project:Nginx ...