CF264B Good Sequences

题意

给定一个单调上升序列 aa,求最多在这个序列中选多个数(不要求相邻),满足相邻两个数不互质。

解法

显然有一个 O(n2logn)O(n^2 \log n) 的 DP,会超时。

考虑 gcd(x,y)>1\gcd(x,y) > 1 这个式子,事实上就是 x,yx, y 有共同公因数,那么对于每一个 ii,只需要把 aia_i 分解质因数,在所有质因数里找一个相对应 dpdp 中最大的,+1+1 即可。

代码:

#pragma GCC optimize("-Ofast,inline")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1e5 + 5;

int a[N], n, dp[N], ans = 0;


inline vector<int> calc(register int x)
{
	vector<int> res;
    int rem = x;
	for (register int i = 2; i * i <= rem; ++i)
	{
		bool f = false;
		while (x % i == 0)
		{
			x /= i;
			f = true;
		}
		if (f) res.push_back(i);
	}
	if (x != 1)
	{
		res.push_back(x);
	}
	return res;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++)
	{
		vector<int> x = calc(a[i]);
		int s = x.size() - 1, maxn = 0;
		for (int i = 0; i <= s; i++)
		{
			maxn = max(maxn, dp[x[i]]);
		}
		for (int i = 0; i <= s; i++)
		{
			dp[x[i]] = max(dp[x[i]], maxn + 1);
		}
		ans = max(ans, maxn + 1);
	}
	printf("%d\n", ans);
	return 0;
}
posted @   HappyBobb  阅读(4)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示