日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mike and gcd problem(思维)

發(fā)布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mike and gcd problem(思维) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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.
題意:給定一組序列,通過若干次變化是指成為一個美麗的序列。美麗序列的定義是,數(shù)組中所有數(shù)字的最大公因數(shù)大于1。問是否能轉(zhuǎn)換成這樣的一個序列,并且最少的變換次數(shù)是多少。
思路:一定可以轉(zhuǎn)換成。
如果這個序列一開始就是最大公因數(shù)大于1,那么就不需要變換。如果不是的話,就有這樣的幾種情況。奇奇,偶偶,奇偶,偶奇。對于偶偶來說,不用管。對于其他三種情況來說
①奇奇:轉(zhuǎn)換成偶偶,只需要一步。
②奇偶,偶奇:轉(zhuǎn)換成偶偶需要兩步。
因為求最少的步數(shù),所以我們應該應該多轉(zhuǎn)換奇奇,其次是奇偶,偶奇。
所以我們先找數(shù)組中的奇奇,然后再找奇偶,或者偶奇。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; int a[maxx]; int n;int main() {scanf("%d",&n);scanf("%d",&a[1]);int cnt=a[1];for(int i=2;i<=n;i++) {scanf("%d",&a[i]);cnt=__gcd(cnt,a[i]);}ll sum=0;if(cnt>1) {cout<<"YES"<<endl<<sum<<endl;return 0;}for(int i=1;i<n;i++){if((a[i]&1)&&(a[i+1]&1)) sum++,a[i]+=1,a[i+1]+=1;}for(int i=1;i<n;i++){if((a[i]+a[i+1])&1) sum+=2,a[i]=a[i+1]=2;//都轉(zhuǎn)換成偶數(shù)。。}cout<<"YES"<<endl<<sum<<endl; }

努力加油a啊,(o)/~

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Mike and gcd problem(思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。