2023牛客暑期多校训练营3

D. Ama no Jaku

D-Ama no Jaku_2023牛客暑期多校训练营3 (nowcoder.com)

题意

给定一个01矩阵,可以对行或列进行翻转操作。求最小的操作次数,使得行的最小二进制数大于等于列的最小二进制数,如果不能则输出-1。

思路

只有当矩阵全为0或1时才满足条件。无论怎么变化,每行每列总是相等或相反的。操作次数就可取第一行和第一列中0或1数量较少的个数相加。

代码

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

  	int n;
  	cin >> n;

  	vector<string> v(n);
  	for(int i = 0; i < n; i++) {
  		cin >> v[i];
  	}

  	for(int i = 1; i < n; i++) {
  		for(int j = 0, f; j < n; j++) {
  			if(j == 0) {
  				f = (v[0][j] - '0') ^ (v[i][j] - '0'); 
  			} else if((v[0][j] - '0') ^ (v[i][j]  - '0') != f) {
  				cout << -1 << "\n";
  				return 0;
  			}
  		}
  	}

  	for(int i = 1; i < n; i++) {
  		for(int j = 0, f; j < n; j++) {
  			if(j == 0) {
  				f = (v[j][0] - '0') ^ (v[j][i] - '0'); 
  			} else if((v[j][0] - '0') ^ (v[j][i] - '0') != f) {
  				cout << -1 << "\n";
  				return 0;
  			}
  		}
  	}

  	int ans = 0, a = 0, b = 0;
  	for(int i = 0; i < n; i++) {
  		v[0][i] == '1' ? a++ : b++;
  	}
  	ans += min(a, b);
  	a = 0, b = 0;
  	for(int i = 0; i < n; i++) {
  		v[i][0] == '1' ? a++ : b++;
  	}
  	ans += min(a, b);

  	cout << ans << "\n";

	return 0;
}

H. Until the Blue Moon Rises

H-Until the Blue Moon Rises_2023牛客暑期多校训练营3 (nowcoder.com)

题意

给定\(n\)个数,每次操作可以选择两个不同下标的元素,一个加1,另一个减1。求能否使整个序列全部变为质数。

思路

利用哥德巴赫猜想,大于2的偶数可以拆为两个质数,和弱哥德巴赫猜想,大于5的奇数可猜为三个质数,就可得出一个结论,当序列和大于等于\(2n\)时,可以让整个序列变为质数。要注意特判\(n\)为1和2的情况。

代码

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

bool isprime(ll n) {
	if(n <= 1) return false;
	for(ll i = 2; i * i <= n; i++) {
		if(n % i == 0) return false;
	}
	return true;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	ll sum = 0;
	for(int i = 0, x; i < n; i++) {
		cin >> x;
		sum += x;
	}

	if(n == 1) {
		cout << (isprime(sum) ? "Yes\n" : "No\n");
	} else if(n == 2) {
		if(isprime(sum - 2) || (sum > 2) && (sum % 2 == 0)) {
			cout << "Yes\n";
		} else {
			cout << "No\n";
		}
	} else {
		cout << ((sum >= 2 * n) ? "Yes\n" : "No\n");
	}

	return 0;
}
posted @ 2023-07-27 02:39  wuyoudexian  阅读(53)  评论(0)    收藏  举报