Codeforces 264B Good Sequences(DP+素数筛)
题目链接:http://codeforces.com/problemset/problem/264/B
题目大意:
给出n个单调递增的数,让你找出最长的好序列,好序列是一种单调递增的并且相邻元素的最大公因数>1的子序列.
解题思路:
设dp[x]表示以x为结尾的好序列的最长长度。
d[i]表示以i为因子的x中最大的dp[x]值。
于是得到状态转移方程dp[x]=max(dp[x],d[i]+1) (i是x的因子)
每次求出dp[x]还要顺便更新d[i],即d[i]=dp[x].
代码:
#include<bits/stdc++.h>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=1e5+;
const int INF=0x3f3f3f3f;
const double eps=1e-; vector<int>fat[N];
int a[N],d[N],dp[N]; int main(){
FAST_IO;
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
}
//筛法求因子
for(int i=;i<=a[n];i++){
for(int j=i;j<=a[n];j+=i){
fat[j].push_back(i);
}
} int ans=;
for(int i=;i<=n;i++){
//遍历因子
for(int j=;j<fat[a[i]].size();j++){
int t=fat[a[i]][j];
dp[i]=max(dp[i],d[t]+);
}
for(int j=;j<fat[a[i]].size();j++){
int t=fat[a[i]][j];
d[t]=dp[i];
}
ans=max(ans,dp[i]);
}
//只有1的时候ans为0,实际为1
cout<<max(ans,)<<endl;
return ;
}
Codeforces 264B Good Sequences(DP+素数筛)的更多相关文章
- codeforces 822 D. My pretty girl Noora(dp+素数筛)
题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...
- POJ 3132 DP+素数筛
Sum of Different Primes Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3684 Accepted ...
- codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces J. Soldier and Number Game(素数筛)
题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- Soldier and Number Game CodeForces - 546D 素因子数打表(素数筛的改造)
题意: 输入 a 和 b(a>b),求a! / b!的结果最多能被第二个士兵给的数字除多少次. 思路: a! / b!肯定能把b!约分掉,只留下b+1~a的数字相乘,所以我们求b+1 ~ a的所 ...
随机推荐
- HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...
- MySQL 第四篇:数据操作
一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...
- logger.debug的用处
原文:https://www.cnblogs.com/xiangkejin/p/6426761.html logger.debug的用处 简单的说,就是配合log的等级过滤输出 根据你log4j的配置 ...
- 802.11 ------ Beacon帧、Beacon Interval、TBTT、Listen Interval、TIM、DTIM
Beacon帧:Beacon的实际发送一般都是采用最低速率的,其包含两个原因,1)beacon帧是一个广播帧,其没有ACK反馈,所以无法设置重传机制,2)beacon帧目的是广播AP的基本信息,所以希 ...
- P2684 搞清洁
P2684 搞清洁 给定一段区间及若干个线段, 求使区间被完全覆盖所需的最少线段数 错误日志: 菜 Solution 补一下贪心吧 这题最小线段覆盖 首先按照左端点排序 现在对于所有左区间到达目前已覆 ...
- P1171 售货员的难题
P1171 售货员的难题 题目描述 某乡有nn个村庄(1<n \le 201<n≤20),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)s(0< ...
- LINUX 线程
1.使用进程技术的优势(1)CPU时分复用,单核心CPU可以实现宏观上的并行(2)实现多任务系统需求(多任务的需求是客观的)2.进程技术的劣势(1)进程间切换开销大(2)进程间通信麻烦而且效率低3.解 ...
- 自动化工具制作PASCAL VOC 数据集
自动化工具制作PASCAL VOC 数据集 1. VOC的格式 VOC主要有三个重要的文件夹:Annotations.ImageSets和JPEGImages JPEGImages 文件夹 该文件 ...
- 用nginx搭建简单的文件下载服务器
server { listen 80; #端口 server_name localhost; #服务名 charset utf-8; # ...
- appium 使用过程问题踩坑-笔记
问题1:虚拟设备选用问题 运行脚本抛出异常,创建session对象失败 排查过程:在进入cmd模式下: ①adb devices --ok ②appium-doctor --ok ③appium ...