#P8792. [蓝桥杯 2022 国 A] 最大公约数

[蓝桥杯 2022 国 A] 最大公约数

Problem Description

Given an array, in each operation you may choose any two adjacent elements x,yx, y in the array and replace one of them with gcd(x,y)\gcd(x, y), where gcd(x,y)\gcd(x, y) denotes the greatest common divisor of xx and yy. Find the minimum number of operations needed to make the entire array contain only 11.

Input Format

The first line of input contains an integer nn, which denotes the length of the array.

The second line contains nn integers a1,a2,,ana_1, a_2,\dots, a_n, separated by a single space between adjacent integers.

Output Format

Output one line containing one integer, which is the minimum number of operations. If it is impossible to meet the requirement no matter how you operate, output 1-1.

3
4 6 9
4

Hint

【Constraints and Conventions for Test Cases】

  • For 30%30\% of the test cases, n500n \leq 500, ai1000a_i \leq 1000.
  • For 50%50\% of the test cases, n5000n \leq 5000, ai106a_i \leq 10^6.
  • For all test cases, 1n1051 \leq n \leq 10^5, 1ai1091 \leq a_i \leq 10^9.

Lanqiao Cup 2022 National Contest Group A, Problem D.

Translated by ChatGPT 5