#P7810. Maximum Valid Segments Partitioning

    ID: 20995 Type: Default 1000ms 256MiB

Maximum Valid Segments Partitioning

Maximum Valid Segments Partitioning

You are given n playing cards, where the i-th card has a positive integer ai.

You need to partition the cards into several contiguous segments such that each segment [l, r] is valid if and only if it satisfies both of the following conditions:

  • al < ar
  • \(\gcd(a_l,a_r)>1\) (i.e. the greatest common divisor of al and ar is greater than 1, where the \(\gcd\) function is defined in the hint section if you are not familiar with it)

Your task is to determine the maximum number of segments into which the cards can be partitioned. The partition must cover the entire sequence of cards. If it is impossible to partition the cards into valid segments, output -1.

Note: Each segment must contain at least two cards since the conditions are defined on the first and last card of the segment.

inputFormat

The first line contains an integer n (the number of cards).

The second line contains n positive integers a1, a2, ..., an separated by spaces.

outputFormat

Output a single integer representing the maximum number of valid segments into which the cards can be partitioned. If no valid segmentation exists, output -1.

sample

5
2 3 4 6 8
2

</p>