传送门

Description:

松鼠丽丝特别喜欢n个她称之为“好整数”的整数:a1,a2,……,an。(会输入)

现在,她对“好序列”很感兴趣。如果一个序列x1,x2,...,xk能够满足一下三个条件,那就是一个“好序列”:

1.该序列是严格上升的,即x[i]<x[i+1](1<=i<=k-1)

2.任意两个相邻的元素是非互质的,即gcd(x[i],x[i+1])>1 (1<=i<=k-1) (gcd即最大公约数)

3.所有的数字都是“好整数”

现在,请你找出长度最长的“好序列”

暴力DP解法:(当然不是AC做法)

  就是很像导弹拦截

  dp[i]表示以编号为i的好数结尾的最长好序列

  枚举判断转移就OK啦

 #include<iostream>
#include<cstdio>
#include<algorithm>
#define R register
#define go(i,a,b) for(R int i=a;i<=b;i++)
#define ll long long
#define M 100000
using namespace std;
int read()
{
int x=,y=;char c=getchar();
while(c<''||c>'') {if(c=='-') y=-;c=getchar();}
while(c>=''&&c<='') {x=(x<<)+(x<<)+c-'';c=getchar();}
return x*y;
}
int n,a[M],dp[M],ans;
int gcd(int x,int y)
{
return x%y==?y:gcd(y,x%y);
}
int main()
{
n=read();
go(i,,n) a[i]=read(),dp[i]=;
sort(a+,a+n+);
go(i,,n)
{
go(j,,i-) if(gcd(a[i],a[j])>) dp[i]=max(dp[i],dp[j]+);
ans=max(ans,dp[i]);
}
printf("%d",ans);
}

暴力DP Code

正解:

  之前的n2转移是枚举判断两个数是否互质

  如果两个数之间不互质其实就是有除了1以外的公因数

  我们发现a[i]是小于等于105

  所以可以记录b[i]为以i为因数的好数结尾的好序列的最大长度

  具体见代码 很好懂的啦

CODE:

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define R register
#define go(i,a,b) for(R int i=a;i<=b;i++)
#define ll long long
#define M 100000+10
using namespace std;
int read()
{
int x=,y=;char c=getchar();
while(c<''||c>'') {if(c=='-') y=-;c=getchar();}
while(c>=''&&c<='') {x=(x<<)+(x<<)+c-'';c=getchar();}
return x*y;
}
int n,a[M],b[M],dp[M],ans;
int main()
{
n=read();
go(i,,n) a[i]=read(),dp[i]=;
sort(a+,a+n+);
go(i,,n)
{
int x=a[i],maxn=sqrt(a[i])+;
go(j,,maxn)
{
if(x%j==) dp[i]=max(dp[i],b[j]+);
while(x%j==) x/=j;
}
if(x>) dp[i]=max(dp[i],b[x]+);
x=a[i];
go(j,,maxn)
{
if(x%j==) b[j]=dp[i];
while(x%j==) x/=j;
}
if(x>) b[x]=dp[i];
ans=max(ans,dp[i]);
}
printf("%d",ans);
}

CF264B Good Sequences的更多相关文章

  1. 【题解】CF264B Good Sequences

    [题解]CF264B Good Sequences 具有很明显的无后效性. 考虑\(dp\). 考虑初始条件,显然是\(dp(0)=0\) 考虑转移,显然是\(dp(t)=max(dp[k])+1\) ...

  2. 洛谷CF264B Good Sequences dp

    解题报告:dp+数论 解题报告: 传送门! 开始看这题的时候想挂了,,,想了个显然是错解的想法,,,就是,我也不知道我怎么想的,鬼迷心窍地就想开个数组存每个质因数的倍数的出现次数,然后排下序的max就 ...

  3. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)

    Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...

  6. 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 ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  8. Python数据类型之“序列概述与基本序列类型(Basic Sequences)”

    序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...

  9. Extract Fasta Sequences Sub Sets by position

    cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...

随机推荐

  1. SQLSERVER2017 最新补丁发布方式

    SQLSERVER2017 开始已经没有 SP service pack 包了. 取而代之的是CU 包 cumulative update 见: https://support.microsoft.c ...

  2. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  3. 深入理解JAVA虚拟机阅读笔记2——垃圾回收

    线程私有的程序计数器.虚拟机栈和本地方法栈随线程而生,随线程而灭.栈中的栈帧随方法的进入和退出有条不紊的入栈和出栈. 而Java堆和方法区因为需要多大内存.创建多少对象都是不确定的,因此这两个区域是垃 ...

  4. STM32标准外设库、 HAL库、LL库

    工作以来一直使用ST的STM32系列芯片,ST为开发者提供了非常方便的开发库.到目前为止,有标准外设库(STD库).HAL库.LL库 三种.前两者都是常用的库,后面的LL库是ST最近才添加,目前支持的 ...

  5. php 关于文件的一些封装好的函数

    <?php //Bytes/Kb/MB/GB/TB/EB /** * 转换字节大小 * @param number $size * @return number */ function tran ...

  6. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  7. 洛谷 P4091 [HEOI2016/TJOI2016]求和 解题报告

    P4091 [HEOI2016/TJOI2016]求和 题目描述 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: \[ f(n)=\sum_{i=0}^n\ ...

  8. 洛谷 P2731 骑马修栅栏 Riding the Fences 解题报告

    P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...

  9. linux内核分析 第五周 扒开系统调用的三层皮(下)

    rm menu -rf 强制删除原menu文件 git clone http://github.com/mengning/menu.git 从github中克隆 cd menu 在test.c中增加上 ...

  10. elasticsearch5使用snapshot接口备份索引

    数据备份是一个必须要考虑的问题,官网提供了 snapshot 接口来备份和恢复数据. 先来看看官方说明: 如果ES是集群,那么需要使用共享存储,支持的存储有: a.shared file system ...