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 → 全局最优解)
- 数据n= 10 5 {10}^5 105 , a ∈ [ 1 , 10 5 ] \in[1,{10}^5] ∈[1,105]
- 对于每个位置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;
}