Good Sequences 唯一分解&&dp&&贪心

Good Sequences

在这里插入图片描述

题意:

给你一个数列。满足以下条件
递增即 a [ i ] < a [ i + 1 ] a[i] < a[i+1] a[i]<a[i+1]

找到一个最长的子序列,使得长度最长,但是有下面的限制

  • g c d ( b [ i ] , b [ i + 1 ] ) > 1 gcd(b[i],b[i+1]) > 1 gcd(b[i],b[i+1])>1, 即任意相邻的元素(在子序列中)都要有公因子。

思路:(局部最优解 → \to 全局最优解)

  1. 数据n= 10 5 {10}^5 105 , a ∈ [ 1 , 10 5 ] \in[1,{10}^5] [1,105]
  2. 对于每个位置a,去查看每个质因子 p i p_i pi 之前的最大长度,并由前面的最大长度ma,递推到当前每个质因子的最大长度。

AC

/*
皮卡丘冲鸭!
へ     /|
  /\7    ∠_/
  / │   / /
 │ Z _,< /   /`ヽ
 │     ヽ   /  〉
  Y     `  /  /
 イ● 、 ●  ⊂⊃〈  /
 ()  へ    | \〈
  >ー 、_  ィ  │ //
  / へ   / ノ<| \\
  ヽ_ノ  (_/  │//
  7       |/
  >―r ̄ ̄`ー―_
*/
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;

const int N = 2e5+10;
const int M = 1e5;
const int maxn=1e5+10;
const db eps = 1e-8;
const db pi = acos(-1.0);

template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
int vis[maxn], dp[maxn];
bool st[maxn+1];
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    vector<int>a(n);//,dp(n);
    fori(i,0,n)cin>>a[i];//, dp[a[i]] = vis[a[i]] = 1;
    int ans = 0;//, mx = a[n-1];
    fori(i,0,n){
        vector<int>pp;
        int x = a[i], ma = 0;
        for(int j = 2; j<=x/j; j ++ ){
            if(x%j==0){
                pp.pb(j);
                ma = max(ma,dp[j]);
                while(x%j==0){
                    x/=j;
                }
            }
        }
        if(x>1)pp.pb(x),ma=max(ma,dp[x]);
        ma++;
        for(auto p: pp)dp[p] = ma;
        ans = max(ans,ma);
    }
    cout<<ans<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值