Mike and gcd problem(思维)
Mike has a sequence A?=?[a1,?a2,?…,?an] of length n. He considers the sequence B?=?[b1,?b2,?…,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1?≤?i?<?n), delete numbers ai,?ai?+?1 and put numbers ai?-?ai?+?1,?ai?+?ai?+?1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it’s possible, or tell him that it is impossible to do so.
is the biggest non-negative number d such that d divides bi for every i (1?≤?i?≤?n).
Input
The first line contains a single integer n (2?≤?n?≤?100?000) — length of sequence A.
The second line contains n space-separated integers a1,?a2,?…,?an (1?≤?ai?≤?109) — elements of sequence A.
Output
Output on the first line “YES” (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and “NO” (without quotes) otherwise.
If the answer was “YES”, output the minimal number of moves needed to make sequence A beautiful.
Examples
Input
2
1 1
Output
YES
1
Input
3
6 2 4
Output
YES
0
Input
2
1 3
Output
YES
1
Note
In the first example you can simply make one move to obtain sequence [0,?2] with .
In the second example the gcd of the sequence is already greater than 1.
題意:給定一組序列,通過若干次變化是指成為一個美麗的序列。美麗序列的定義是,數組中所有數字的最大公因數大于1。問是否能轉換成這樣的一個序列,并且最少的變換次數是多少。
思路:一定可以轉換成。
如果這個序列一開始就是最大公因數大于1,那么就不需要變換。如果不是的話,就有這樣的幾種情況。奇奇,偶偶,奇偶,偶奇。對于偶偶來說,不用管。對于其他三種情況來說
①奇奇:轉換成偶偶,只需要一步。
②奇偶,偶奇:轉換成偶偶需要兩步。
因為求最少的步數,所以我們應該應該多轉換奇奇,其次是奇偶,偶奇。
所以我們先找數組中的奇奇,然后再找奇偶,或者偶奇。
代碼如下:
努力加油a啊,(o)/~
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Mike and gcd problem(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Destroying Array(并查集
- 下一篇: The Trip On Abandone