CodeForces - 264B Good Sequences(DP思想)

B. Good Sequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.

Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:

  • The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1).
  • No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q).
  • All elements of the sequence are good integers.

Find the length of the longest good sequence.

Input

The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105ai < ai + 1).

Output

Print a single integer — the length of the longest good sequence.

Examples
input
Copy
5
2 3 4 6 9
output
Copy
4
input
Copy
9
1 2 3 5 6 7 8 9 10
output
Copy
4
Note

In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.

题意:给你一个包含n(n<=1e5)个数的序列,求一个最长上升子序列且序列中相邻的两个数GCD大于1。保证输入的序列是递增的。

思路:由于保证序列递增,因此我们只需要满足第二个条件即可。但是怎么满足呢?

若两个数GCD大于1,即这两个数有至少一个大于1的公因子。因此我们先筛选出所有2~1e5的数的因子。每个因子i初值为dp[i]=0;

一旦这个数的某个因子i 的dp[i]+1后成为这个数的所有因子中dp值最大的,那也就意味着挑选这个数可以使满足条件的序列长度最多为dp[i];

因此我们把它的所有因子的dp值都更新成dp[i](即这个数的所有因子中dp值最大的那个),这样下一个数只要和这一个数有任意一个>1的公因子,便可将下一个数加入合法的序列中使合法序列长度增加。于是就完成了状态转移。

最后遍历一遍dp取最大值即可。

注意:答案至少为1,因为k=1时是永远满足条件的!!!(否则就会WA!!!)      估计比赛的时候是个hack点。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int n,m,k,f;
int ans,tmp,cnt;
int a[maxn],c[maxn],dp[maxn];
char s[maxn];
vector<int>vc[maxn];
void init(){
for(int i=0;i<maxn;i++) vc[i].clear();
for(int i=2;i<maxn;i++)
{
    for(int j=i;j<maxn;j+=i)
    {
        vc[j].push_back(i);
    }
}
}
int solve() {
    int top;
    for(int i=0;i<n;i++)
    {
        top=0;
        for(int j=0;j<vc[a[i]].size();j++)
        {
            int v=vc[a[i]][j];
            dp[v]++;
            top=max(top,dp[v]);
        }
        for(int j=0;j<vc[a[i]].size();j++)
        {
            int v=vc[a[i]][j];
            dp[v]=top;
        }
    }
    top=1;
    for(int i=1;i<maxn;i++)
    top=max(top,dp[i]);
    return top;
}
int main()
{
    init();
    int T,cas=1;
   scanf("%d",&n);
    {
        ans=0;
        cnt=0;
        for(int i=0;i<n;i++)
        {
           scanf("%d",&a[i]);
        }
        //  memset(c,0,sizeof(c));
        printf("%d\n",solve());
        //if(flag) puts("Yes");else puts("No");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值