#P7810. [JRKSJ R2] Upper

    ID: 8861 远端评测题 1500ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树2021洛谷原创O2优化素数判断,质数,筛法

[JRKSJ R2] Upper

Problem Description

There are nn poker cards. On the ii-th card, there is a positive integer aia_i.

Now you need to divide the cards into several legal consecutive segments. A consecutive segment [l,r][l,r] is “legal” if and only if it satisfies both conditions:

  • al<ara_l < a_r.
  • gcd(al,ar)>1\gcd(a_l,a_r) > 1.

Ask for the maximum number of segments you can divide into. If there is no legal partition plan, output 1-1.

If you do not know what gcd\gcd means, see the “Hint” section.

Input Format

The first line contains an integer nn.
The second line contains nn integers representing the sequence aa.

Output Format

Output one integer, as described in the statement.

5
2 1 8 3 9
2
5
5 4 3 2 1
-1
20
20 9 36 36 40 8 3 10 9 20 18 12 30 20 30 15 8 9 27 45
7

Hint

Constraints

This problem uses bundled testdata.

For 100%100\% of the testdata, 2n1052 \le n \le 10^5, 1ai1091 \le a_i \le 10^9.

Subtask\text{Subtask} nn\le aia_i\le Special property Score
11 55 10910^9 None 55
22 3×1033\times10^3 1515
33 2×1042\times10^4 10610^6 2020
44 2×1042\times 10^4 10910^9 1010
55 10510^5 Random testdata
66 None 4040

Sample Explanation

For Sample 11, there is one and only one partition plan: {2,1,8},{3,9}\{2,1,8\},\{3,9\}.
For Sample 22, there is no legal partition plan.

Hint

For two positive integers a,ba,b, gcd(a,b)\gcd(a,b) is their greatest common divisor, i.e. the largest number that is a divisor of both aa and bb.

Translated by ChatGPT 5