Codeforces264 B. Good Sequences
出处: 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的更多相关文章
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)
Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
- 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 ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- Python数据类型之“序列概述与基本序列类型(Basic Sequences)”
序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...
- 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 440 Solved: 16 ...
- MOOCULUS微积分-2: 数列与级数学习笔记 1. Sequences
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
随机推荐
- T-shirt buying CodeForces - 799B (小根堆+STL)
题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...
- Oracle和Elasticsearch数据同步
Python编写Oracle和Elasticsearch数据同步脚本 标签: elasticsearchoraclecx_Oraclepython数据同步 Python知识库 一.版本 Pyth ...
- Of Study
Bacon Reading maketh a full man; conference a ready man; and writing an exact man. And therefore, if ...
- Django之ORM操作(聚合 分组、F Q)
Django之ORM操作(聚合 分组.F Q) 聚合 aggregate()是QuerySet的一个终止子句,也就是说,他返回一个包含一些键值对的字典,在它的后面不可以再进行点(.)操作. 键的名 ...
- 转:MD5(Message-Digest Algorithm 一种哈希算法)
什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...
- Linux启动/停止/重启Mysql数据库
1.查看mysql版本 1)status; 2)select version(); 2.Mysql启动 1)使用 service 启动: service mysqld start (5.0版本是mys ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
- vue-resources&axios
vue-resource vue-resource是Vue.js的一款插件,它可以通过XMLHttpRequest或JSONP发起请求并处理响应. vue-resource特点: 体积小 vue-re ...
- leetcode资料整理
注:借鉴了 http://m.blog.csdn.net/blog/lsg32/18712353 在Github上提供leetcode有: 1.https://github.com/soulmachi ...
- flask 下载本地文件
下载本地文件就是找到文件路径 调用flask自带的send_file(路径)下载, 并返回 flask: # 下载文件 from flask import send_file@task_mgm.ro ...