【题解】CF264B Good Sequences
【题解】CF264B Good Sequences
具有很明显的无后效性。
考虑\(dp\)。
考虑初始条件,显然是\(dp(0)=0\)
考虑转移,显然是\(dp(t)=max(dp[k])+1\)其中\(gcd(data[t],data[k])>1\)
这样的转移是\(O(n^2)\)的!显然超时。
发现值域\(\le 100000\)那么我们将数拆成它的质因数。
线性筛素数预处理\([1,100000]\)的质数\(O(n)\)的代价。存下来。
然后转移的时候,先直接\(O(\sqrt{n})\)枚举质因数,直接从\(vector\)表里查询能够从谁那里转移。
根据\(dp\)的最优性质,可以知道只需要从\(vector\)的最后一个 \(dp\)值转移过来就好了,一定是最大的。
这样复杂度就是\(O(n\sqrt{n})\)了
#include<bits/stdc++.h>
#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t)
#define ERP(t,a) for(int t=head[a];t;t=e[t].nx)
#define Min(a,b) ((a)<(b)?(a):(b))
#define pushup(x) seg[(x)]=seg[(x)<<1]+seg[(x)<<1|1]
#define midd register int mid=(l+r)>>1
#define chek if(R<l||r<L)return
#define TMP template<class ccf>
#define rgt L,R,mid,r,pos<<1|1
#define lef L,R,l,mid,pos<<1
#define all 1,n,1
using namespace std;typedef long long ll;
TMP inline ccf qr(ccf k){
char c=getchar();
ccf x=0;
int q=1;
while(c<48||c>57)q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
return q==-1?-x:x;
}
const int maxn=100005;
const int maxl=maxn-5;
bool usd[maxn];
int data[maxn];
#define isp(x) (!usd[x])
vector < int > pr;
#define pb push_back
vector < int > p[100001];
int n;
int ans;
int dp[maxn];
inline void gen_pr(){
usd[1]=1;
usd[0]=1;
RP(t,2,maxl){
if(!usd[t])
pr.pb(t);
RP(i,0,pr.size()-1){
if(t*pr[i]>maxn)
break;
usd[t*pr[i]]=1;
if(!(t%pr[i]))
break;
}
}
}
inline int Max(int x,int y){
if(x>y)
return x;
return y;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("good.in","r",stdin);
freopen("good.out","w",stdout);
#endif
gen_pr();
n=qr(1);
RP(t,1,n)
data[t]=qr(1);
int siz=pr.size()-1;
RP(t,1,n){register int sav=data[t];
RP(i,0,siz){
register int now=pr[i];
register int nowcnt=p[now].size();
if(!(sav%now)){
while(!(sav%now))
sav/=now;
if(nowcnt)
dp[t]=Max(dp[t],dp[p[now][nowcnt-1]]+1);
else
dp[t]=Max(dp[t],1);
p[now].pb(t);
}
if(sav<now)
break;
}
}
ans=1;
RP(t,1,n)
ans=Max(ans,dp[t]);
cout<<ans<<endl;
return 0;
}
【题解】CF264B Good Sequences的更多相关文章
- 洛谷CF264B Good Sequences dp
解题报告:dp+数论 解题报告: 传送门! 开始看这题的时候想挂了,,,想了个显然是错解的想法,,,就是,我也不知道我怎么想的,鬼迷心窍地就想开个数组存每个质因数的倍数的出现次数,然后排下序的max就 ...
- 题解 CF1428F Fruit Sequences
\(\texttt{Bullshit}\) 蒟蒻 \(\texttt{7 min}\) 切 \(\texttt{F}\), 挽回了本一定掉分的局面/cy 分竟然还没有别人 5 题高 (本题解为目前 c ...
- CF264B Good Sequences
传送门 Description: 松鼠丽丝特别喜欢n个她称之为“好整数”的整数:a1,a2,……,an.(会输入) 现在,她对“好序列”很感兴趣.如果一个序列x1,x2,...,xk能够满足一下三个条 ...
- Leetcode:Repeated DNA Sequences详细题解
题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- 题解 UVA1608 【不无聊的序列 Non-boring sequences】
思路: 算法很显然: 一.在区间\([l,r]\)找到一个只出现一次的元素P(如果不存在,那么序列\(boring\)) 二.递归处理区间\([l,p-1]\)和区间\([p+1,r]\). 其关键在 ...
- Codeforces Round #631 (Div. 1) B. Dreamoon Likes Sequences 题解(思维+求贡献)
题目链接 题目大意 让你构造一个严格单调上升的数组a满足\(1<=a_1<a_2<....a_n<=d\) 而且要使得这个数组的异或前缀和也满足严格单调上升,求有多少个满足条件 ...
- Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)
题目链接 题目大意 给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q ...
- CF450B Jzzhu and Sequences 题解
Content 有一个长度为 \(n\) 的数列 \(\{a_1,a_2,\dots,a_n\}\),满足如下的递推公式: \(i=1\) 时,\(a_1=x\). \(i=2\) 时,\(a_2=y ...
随机推荐
- luogu P3116 [USACO15JAN]会议时间Meeting Time
题目描述 Bessie and her sister Elsie want to travel from the barn to their favorite field, such that the ...
- iOS 5 does not allow to store downloaded data in Documents directory? ios5.0及以后的版本对于下载的文件存储路径有了改变
I have made an application for my client by keeping target iOS as 4. But since the application still ...
- How To Commit Just One Data Block Changes In Oracle Forms
You have an Oracle Form in which you have multiple data blocks and requirement is to commit just one ...
- UVA 133“The Dole Queue”(循环报数处理技巧)
•参考资料 [1]:紫书P82 •题意(by紫书) 按照被选中的次序输出这 n 个人的编号: 如果A和B选中的是同一个人,输出一个这个人的编号: 输出格式:输出的每个编号占3个字节,不够3个字节在前面 ...
- [置顶]
django快速获取项目所有的URL
django快速获取项目所有的URL django1.10快速获取项目所有的URL列表,可以用于权限控制 函数如下: import re def get_url(urllist , parent='' ...
- CentOS下iptables 配置详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- 基于Wiremock创建Mock Service平台
转载:http://blog.csdn.net/liuchunming033/article/details/52399397 1.Wiremock工具介绍 一般开发项目都会分模块进行,比如都会把前端 ...
- 【oracle ocp知识点一】
1.怎样确定数据库是否启动 su - oracle ps -ef |grep ora_|head -2 两种关系数据库是ora或者是自己主动存储管理的asm开头的, 查看进程能够知道数据库实例至少已经 ...
- Spring2.5学习3.2_编码剖析@Resource注解的实现原理
首先看一下J2EE提供的@Resource注解:该注解默认安照名称进行装配,名称能够通过name属性进行指定, 假设没有指定name属性,当注解写在字段上时,默认取字段名进行依照名称查找,假设注解写在 ...
- 1.1 合用weightSum属性和layout_weight属性
<打造高质量Android应用:Android开发必知的50个诀窍>第1章活用布局,本章将介绍Android布局相关的一些窍门和建议.通过本章,读者不仅可以学习如何从零开始创建特定类型的布 ...