【链接】 我是链接,点我呀:)

【题意】

让你在一个递增数组中选择一个最长子序列使得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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【22.70%】【codeforces 591C】 Median Smoothing

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 761D】Dasha and Very Difficult Problem

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. 从缓冲上看阻塞与非阻塞socket在发送接收上的区别(转载)

    转自:http://blog.chinaunix.net/uid-24517549-id-4044877.html   首先socket在默认情况下是阻塞状态的,这就使得发送以及接收操作处于阻塞的状态 ...

  2. golang——随机数(math/rand包与crypto/rand包)

    1.math/rand 包 1.1.math/rand 包实现了伪随机数生成器 1.2.主要方法 (1)func Seed(seed int64) 设置随机种子,不设置则默认Seed(1) (2)fu ...

  3. 洛谷 P1233 木棍加工

    题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间为1分钟: 如果刚处理 ...

  4. c++ strcmp函数

    用法:加头文件 #include <string.h> 功能:比较字符串s1和s2. 一般形式:strcmp(字符串1,字符串2) 返回值: 当s1<s2时,返回值<0 当s1 ...

  5. Install-Package : “XXXX”已拥有为“XXXX”定义的依赖项。

    Install-Package : “AutoMapper”已拥有为“NETStandard.Library”定义的依赖项.所在位置 行:1 字符: 16+ Install-Package <& ...

  6. 如何向expect脚本里面传递参数

    如何向expect脚本里面传递参数   比如下面脚本用来做ssh无密码登陆,自动输入确认yes和密码信息,用户名,密码,hostname通过参数来传递   ssh.exp   Python代码   # ...

  7. can't set blob value on that column

    MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...

  8. Pro ASP.NET Core MVC 第6版 第二章(前半章)

    目录 第二章 第一个MVC 应用程序 学习一个软件开发框架的最好方法是跳进他的内部并使用它.在本章,你将用ASP.NET Core MVC创建一个简单的数据登录应用.我将它一步一步地展示,以便你能看清 ...

  9. thinkphp信息修改和分页

    关联两个数据表,在Model里建立StuModel.class.php: <?php //Belongs_to 关联表示当前模型从属于另外一个父对象 namespace Admin\Model; ...

  10. POJ3107 Godfather (树形DP)

    题意:求树的重心 题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数 再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下 又被卡常了 vector一直tl ...