【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- poj 1201 Intervals【差分约束+spfa】
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...
- Java实现短信中提取号码
Description 提取一条短信里所有的电话号码,电话号码之间换行打印,短信的内容由用户输入. Input 第一行有个整数n(1≤n≤1000)表示测试用例的个数.其后的每一行中有一条短信,每一条 ...
- c语言—栈区,堆区,全局区,文字常量区,程序代码区 详解
转:http://www.cnblogs.com/xiaowenhui/p/4669684.html 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(sta ...
- HDU 4691 后缀数组+RMQ
思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...
- hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...
- S - Cyclic Components (并查集的理解)
Description You are given an undirected graph consisting of nn vertices and mm edges. Your task is t ...
- 为WebSphere Application Server v8.5安装并配置JDK7
IBM WebSphere Application Server v8.5可以同时支持不同版本的JDK共存,并且可以通过命令设置概要文件所使用的JDK版本.WAS8.5默认安装JDK6,如果要使用JD ...
- USB接口大百科:看完你就分得清充电线了
http://tech.ifeng.com/a/20151116/41507221_0.shtml
- 对数组名取地址&a和 数组首地址a
#include <iostream> using namespace std; ] = {,,,,}; int main() { cout<<a<<" ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...