Prime factor for depositing all numbers
If two numbers are not coprime, then there must be a prime factor between them.
Dp[i][0] Indicates the maximum number of the number of first I can be obtained in the sequence
DP[I][1] indicates that the number of the first I was selected
Dp[i][0] = max (dp[i-1][0], dp[i-1][1])
DP[I][1] = max (dp[pos][1] + 1, dp[i][1]);
The last position where the prime number factor of the POS bit I is the first occurrence
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace Std;
const int MAXN = 100010;
int dp[maxn][2];
int LAST[MAXN];
int A[MAXN];
int ISP[MAXN];
int n;
Vector<int> VEC[MAXN];
void Set ()
{
memset (ISP, 0, sizeof (ISP));
ISP[1] = 1;
for (int i = 2;i < MAXN; i + = 2)
{
Isp[i] = 1;
Vec[i].push_back (2);
}
for (int i = 3;i < MAXN; i++)
{
if (Isp[i]) continue;
for (int j = I;j < MAXN; j+=i)
{
ISP[J] = 1;
Vec[j].push_back (i);
}
}
}
int main ()
{
Set ();
while (~SCANF ("%d", &n))
{
memset (last, 0, sizeof (last));
memset (DP, 0, sizeof (DP));
for (int i = 1;i <= n;i++)
{
scanf ("%d", &a[i]);
Dp[i][0] = max (dp[i-1][0], dp[i-1][1]);
for (int j = 0;j < Vec[a[i]].size (); j + +)
{
int pos = last[vec[a[i]][j]];
DP[I][1] = max (dp[i][1], dp[pos][1] + 1);
LAST[VEC[A[I]][J]] = i;
}
}
printf ("%d\n", Max (1, Max (dp[n][1], dp[n][0]));
}
return 0;
}
Codeforces Round #162 (Div. 1) B DP