题目:http://codeforces.com/problemset/problem/264/B

题意:给你一个递增序列,然后找出满足两点要求的最长子序列

第一点是a[i]>a[i-1]

第二点 gcd(a[i],a[i-1])>1 也就是说两个数不能互质

找出最长的子序列长度

思路:首先想互质问题,如果两个数互质说明两个数之间没有素因子相同,我们想以每个素因子结尾的最大长度是多少

然后比如样例 2 3 4 6 9

第一个数 2      2结尾 1

第二个数 3      3结尾 1

第三个数 4       2结尾 2

第四个数6        拆分因子有   2,3      2结尾 3, 3结尾2        ,但是这个时候我以6结尾,那么其实3这个位置长度也是3了,

(所以,我们要找出最大的那个长度,再重新赋值到这个数的每个素因子上) 这个时候就能解出来了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iostream>
#include<map>
#define mod 1000007
#define maxn 200001
using namespace std;
typedef long long ll;
ll a[maxn];
ll dp[maxn];
ll n;
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
}
for(int i=;i<n;i++){
ll z=a[i];
map<ll,ll> mp;
vector<int> q;
while(z!=){
ll t=(ll)sqrt((double)z);//必须要加,不然会超时
ll j=;
for(;j<=t;j++){
if(z%j==){
if(mp[j]==) q.push_back(j);
mp[j]++;
z/=j;
break;
}
}
if(j==t+){
if(mp[z]==) q.push_back(z);//判断素数情况
break;
}
}
ll mx=-;
for(int j=;j<q.size();j++){//找出最大长度
dp[q[j]]++;
if(mx==-) mx=dp[q[j]];
else mx=max(mx,dp[q[j]]);
}
for(int j=;j<q.size();j++){//重新赋到每一个位置
dp[q[j]]=mx;
}
}
ll x=dp[];
for(int i=;i<maxn;i++){
x=max(x,dp[i]);
}
cout<<max((ll),x);
}

Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)的更多相关文章

  1. 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. Codeforces Round #162 (Div. 2)

    A. Colorful Stones (Simplified Edition) 模拟. B. Roadside Trees (Simplified Edition) 每次转移时,只需要爬到\(min( ...

  4. Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. Codeforces Round #162 (Div. 2) A~D 题解

    A. Colorful Stones (Simplified Edition) time limit per test 2 seconds memory limit per test 256 mega ...

  6. Codeforces Round #450 (Div. 2) D.Unusual Sequences (数学)

    题目链接: http://codeforces.com/contest/900/problem/D 题意: 给你 \(x\) 和 \(y\),让你求同时满足这两个条件的序列的个数: \(a_1, a_ ...

  7. Divide by Zero 2021 and Codeforces Round #714 (Div. 2) B. AND Sequences思维,位运算 难度1400

    题目链接: Problem - B - Codeforces 题目 Example input 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 output 6 ...

  8. Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)

    题目链接 题目大意 给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q ...

  9. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

随机推荐

  1. Qt551.窗口滚动条

    1.代码的方式来创建 ScrollArea,然后使用 倒是 正常(有滚动条显示),但是此方式太麻烦 不如直接拖控件来的方便直观快捷. 但是,直接拖控件的方式 ScrollArea中无法显示出 滚动条, ...

  2. js对象跟数组多层嵌套,检测没有此数据就添加有则不添加以及超过限制条件删除操作

    例如你需要这样格式的数据: [{"name":"合肥市","arrey":[{"lat":"31.862323 ...

  3. python - xml转excel

    xml转excel,可以用xml.etree.ElementTree去解析xml文件,然后用xlwt写入excel 示例:ConvConfig.xml <Conveyor_Channel_1&g ...

  4. LRU算法的Java实现

    LRU全称是Least Recently Used,即最近最久未使用的意思. LRU算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已 ...

  5. hbase的api操作之scan

    扫描器缓存----------------    面向行级别的.    @Test    public void getScanCache() throws IOException { Configu ...

  6. Click event doesn't work on dynamically generated elements

    I couldn't get live or delegate to work on a div in a lightbox (tinybox). I used setTimeout successf ...

  7. Dividing the numbers CodeForces - 899C (构造)

    大意: 求将[1,n]划分成两个集合, 且两集合的和的差尽量小. 和/2为偶数最小差一定为0, 和/2为奇数一定为1. 显然可以通过某个前缀和删去一个数得到. #include <iostrea ...

  8. cron 配置

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...

  9. 通讯录管理系统(C语言)

    /* * 对通讯录进行插入.删除.排序.查找.单个显示功能 */ #include <stdio.h> #include <malloc.h> #include <str ...

  10. fastjson对象转为json字符串日期格式变为时间戳问题

    今天尝试将map集合转为json对象时遇到一个问题.map中的value为日期格式如"2019-03-01",在使用JSONObject.toJSON(map).toString( ...