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格式教材下载 ...
随机推荐
- 基于node.js的博客搭建
一个博客应当具备哪些功能? 前台展示 点击下一页,可以点击分类导航. 可以点击进入到具体博文页面 下方允许评论.显示发表时间.允许留言分页. 右侧有登录注册界面. 后台管理 管理员账号:登陆后看到页面 ...
- python中变量、函数、类名、模块名等命名方式
摘要:模块名:小写字母,单词之间用_分割ad_stats.py包名:和模块名一样类名:单词首字母大写AdStatsConfigUtil全局变量名(类变量,在java中相当于static变量):大写字母 ...
- group by用法
select * from Table group by id,一定不能是*,而是某一个列或者某个列的聚合函数. 参考:http://www.cnblogs.com/jingfengling/p/59 ...
- Day1 初步认识Python
天气有点阴晴不定~ (截图来自----------金角大王) 1.学习了计算机概论(CPU/Memory/Disk,memory的存在是为了解决信息传输产生的时延) CPU:精简指令集(RISC)-- ...
- Codeforces Round #546 (Div. 2)
http://codeforces.com/contest/1136 A #include <bits/stdc++.h> using namespace std; ; int N, K; ...
- Windows NT 的历史
Windows NT 的版本历史 https://blog.csdn.net/flyingpig2016/article/details/53282895/ 按照自己找到的资料:windows NT ...
- vue嵌套路由
父组件 (注:to="/Flow/moban_a"这里不是文件加路径,是父组件路由+子组件路由) 路由配置
- CLOUD常用表
采购采购订单(t_PUR_POOrder, t_PUR_POOrderEntry)-收料通知单(T_PUR_Receive,T_PUR_ReceiveEntry)-采购入库单(T_STK_INSTOC ...
- 设置SQLServer数据库内存
需要设置SQLServer数据库的内存配置.登录数据库,这里使用的是SQLServer2008,右键点击最上方的服务器名,在弹出的菜单中,点击属性] 打开服务器属性窗口.默认显示的是第一项[常规]内容 ...
- Django的模板层
一 模版简介 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now ...