Codeforces Round #162 (Div. 1) B DP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.