2020 Winter Holiday [gmoj1599] [GDKOI2004] [Camphor camphor tree] [The longest non-declining subsequence + optimization]

Title description

The camphor tree, known as one of the four famous trees in Jiangnan, is very distinctive. Its bark is rough, but its texture is very uniform. It has never been mottled by poplars and no tumor nodules of willow trees; The two parts are divided into four and one goes long, and will not cut corners or add to the snake; the shape of the tree crown is spherical, drawing beautiful curves in the sky. In addition to the above advantages, camphor tree has a secret weapon. That is ... It won the favor of the little fox with its simple and heavy excellent character! ! ! It was said that one day, the little fox was walking by the lake, and suddenly a gust of wind blew, and she quickly closed her eyes. When she opened her eyes again, she found a neat row of camphor trees on the beautiful lake. The little fox was very excited. She observed every tree and counted their leaves. She feels that if the number of leaves of two adjacent trees is relatively unharmonious. Therefore, the little fox wants to select several trees from a row of camphor trees and make as many trees as possible under the condition that the number of leaves of two adjacent trees is not prime.

Input

A positive integer n in the first line indicates that there are n camphor trees. In the second line, there are n positive integers, and the i-th number represents the number of leaves of the i-th camphor tree.

Output

A positive integer indicating how many trees can be selected at most.

Sample input

6
6 2 3 15 8 5

Sample output

4

Data range limitation

For 60% of data n <= 1000     
For 100% of data n <= 100000, the number of leaves <= 100000
Note: The selected tree cannot change its position, that is, if the (t1, t2, t3 ... tn) Tree, where t1 <t2 <t3 <...... <tn thinks that ti is adjacent to ti + 1.

prompt

Select the 1st, 3rd, 4th and 6th trees

analysis

In fact, it is similar to the longest non-descending subsequence! ! ! It's just that the "not falling" condition changes, and the greatest common factor of a [i] and a [j] is greater than 1, instead of a [i]> a [j]. But timeout 60pts.
How to optimize? metaphysics! !
As long as the 1 to i−1 of j is changed to i−log (i) ∗ 2 to i−1, it will not time out ... Magic qwq, I do n’t know why. .

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100010],n,f[100010],ans;
int zdgys(int x,int y)
{
     if(x%y==0) return y;
     else return zdgys(y,x%y);//辗转相除 
}
int log(int x)
{
    int t=0;
    while(x>0)
    {
	    x=x/2;
	    t++;
    }
    return t;
}
int main()
{
    freopen("camphor.in","r",stdin);
    freopen("camphor.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
	    cin>>a[i];
	    f[i]=1;
    }
    for(int i=2;i<=n;i++)
    {
		for(int j=i-log(i)*2;j<=i-1;j++)
		{
			if(j>0)
			{
				if(zdgys(a[i],a[j])>1)
				{
					f[i]=max(f[i],f[j]+1);
				}
			}	    
		}	
    }
    for(int i=1;i<=n;i++)
    {
    	if(f[i]>ans) ans=f[i];
	}
    cout<<ans;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · views 8012

Guess you like

Origin blog.csdn.net/dglyr/article/details/105080775