【Codeforces 264B】Good Sequences
【链接】 我是链接,点我呀:)
【题意】
让你在一个递增数组中选择一个最长子序列使得gcd(a[i],a[i+1])>1
【题解】
设f[i]表示以一个"含有素因子i的数字"作为序列的结尾的最长序列的长度
显然更新的时候
假设枚举到了a[i]
先求出它所有的素因子p[]
因为要和前面一个数字不互质
那么只能找结尾数字有这些素因子p[]的数字
因此我们求出f[p[1~cnt]]中的最大值Ma,他们最大值对应的序列再加上一个a[i]的话
长度就变成Ma+1了
然后f[p[1~cnt]]就能用Ma+1的值来尝试更新了
因为a[i]含有这些因子
注意只有一个数字1的情况,这种时候只选一个1是不和任何数字互质的,也是满足条件的。
抓住不互质就是有共同素因子这一点很重要。
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)1e5;
static class Task{
int n;
int a[] = new int[N+10];
int f[] = new int[N+10];
int p[] = new int[N+10];
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();
for (int i = 1;i <= n;i++) a[i] = in.nextInt();
int ans = 0;
for (int i = 1;i <= n;i++) {
int cnt = 0;
int x = a[i];
for (int j = 2;j*j<=x;j++) {
if (x%j==0){
p[++cnt] = j;
while (x%j==0) {
x/=j;
}
}
}
if (x>1) p[++cnt] = x;
int ma = 0;
for (int j = 1;j <= cnt;j++)
ma = Math.max(ma, f[p[j]]+1);
if (a[i]==1) ma = 1;
ans = Math.max(ma, ans);
for (int j = 1;j <= cnt;j++)
f[p[j]] = Math.max(f[p[j]], ma);
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 264B】Good Sequences的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 761D】Dasha and Very Difficult Problem
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- E20170602-ts
questionnaire n. 调查问卷; 调查表; アンケート不是英语 collection n. 征收; 收集,采集; 收藏品; 募捐; association n. 联想; 协会, ...
- Java调用ssl异常(javax.net.ssl.SSLHandshakeException: No appropriate protocol)
今天做升级用了jdk1.8发现java调用SSL的时候,突然一下抛出一个异常 经过一阵瞎搞,最后才发现是因为jdk1.8版本导致SSL调用权限上有问题. 解决办法:找到jdk 1.8安装目录,找到C: ...
- 乐搏讲自动化测试- Python自动化测试前景怎么样(4)
Python言语还能够写爬虫,但仅仅只是爬虫的入门罢了.通过Python入门爬虫比较简略易学,不需要在一开始把握太多太根底太底层的常识就能够很快上手,而且很快能够做出成果,十分合适小白一开始想做出点看 ...
- laravel学习一
Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...
- [APIO2007]动物园
题目描述 新建的圆形动物园是亚太地区的骄傲.圆形动物园坐落于太平洋的一个小岛上,包含一大圈围栏,每个围栏里有一种动物. 你是动物园的公共主管.你要做的是,让每个来动物园的人都尽可能高兴.今天有一群小朋 ...
- RHEL5.6更新yum源
RHEL5.6更新yum源记录,2017年2月20日 root用户切换目录至:/etc/yum.repos.d/ [root@localhost yum.repos.d]# pwd /etc/yum. ...
- 发生在升级OS X Yosemite后:修复各种开发环境
本博文最初发布于我的个人博客<Jerry的乐园> 终于还是忍不住升级了,促使我升级的原动力居然是Alfred的Yosemite theme居然比初始theme好看很多!在升级前就预想到我的 ...
- 【原创】利用doxygen来管理项目文档或注释
一.doxygen应用场景: doxygen可以用来管理目前主流的编程语言的注释而形成文档系统.(包括C, C++, C#, Objective-C, IDL, Java, VHDL, PHP, Py ...
- STL容器迭代过程中删除元素技巧(转)
1.连续内存序列容器(vector,string,deque) 序列容器的erase方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素. vector<in ...
- 用纯函数式思维在Java8下写的一段奇葩程序
首先说一下什么是纯函数式.在我的理解,"纯函数式"用一句话就可以描述:Anything is value.--我的理解不一定准确,但我就是这么理解的. 就是所有的东西都是值--没有 ...