给定序列需满足二个条件:本身是质数,相邻二项之和仍为质数
首先一个偶数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。