給定序列需滿足二個條件:本身是質數,相鄰二項之和仍為質數
首先一個偶數2*n不能通過+2*k(k取整數)得到質數。
奇數2*n-1+2*k=2*(n+k)-1,可能得到質數
那么若序列中存在偶數,一定不滿足第一個條件(特判0,2)。
兩個質數相加相當于兩個奇數相加結果為偶數,一定不為質數
當我知道0,1不是質數
所有的偶數變成最小的偶數質數2,所有的奇數變成最小的奇數質數3。
單獨2可以
單獨3可以
2與2相鄰不行
2與3相鄰可以
3與3相鄰不行
所以只要含有相鄰的奇數對或者偶數對就是NO
#include<iostream>
#include<vector>
#include<algorithm>using namespace std;
#define endl '\n'
void solve() {int n; cin >> n;vector<int> a(n, 0);for (int i = 0; i < n; i++) cin >> a[i];for (int i = 1; i < n; i++) {if (a[i] & 1 && a[i - 1] & 1) {cout << "NO" << endl;return;}if (!(a[i] & 1) && !(a[i - 1] & 1)) {cout << "NO" << endl;return;}}cout << "YES" << endl;return;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int t;cin >> t;while (t--) {solve();}return 0;
}
一開始遇到了很奇怪的bug,把判斷寫在了輸入的過程中,存在中間判斷return了但是本輪數據還沒有輸入完,直接導致tle。