【Codeforces】Codeforces Round 868 (Div. 2) (ABC)(+104!)

A-characteristic

题目大意:

解题思路:

将n分成两段,从每一段选两个的方法数(组合数)和为k,枚举每段即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
ll T, n, k;
ll c[105][105];
ll C(ll a, ll b) {
	for (int i = 0; i <= a; ++i) {
		for (int j = 0; j <= b && j <= i; ++j) {
			if (!j) c[i][j] = 1;
			else  c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
		}
	}
	return c[a][b];
}
void solve() {
	cin >> n >> k;
	if (n == 1 && k == 0) {
		cout << "YES" << endl;
		cout << 1 << endl;
		return;
	}
	if (n == 2 && k == 0) {
		cout << "YES" << endl;
		cout << 1 << ' ' << -1 << endl;
		return;
	}
	int ans = -1;
	for (int i = 2; i <= n; i++) {
		if (C(i, 2) + C(n - i, 2) == k) {
			ans = i;
			break;
		}
	}
	if (ans == -1) cout << "NO" << endl;
	else {
		cout << "YES" << endl;
		for (int i = 1; i <= ans; i++) {
			cout << 1 << ' ';
		}
		for (int i = ans + 1; i <= n; i++) {
			cout << -1 << ' ';
		}
		cout << endl;
	}
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Sort with Step

题目大意:

解题思路:

观察每一个数离正确位置的距离,如果距离为k的倍数那么可以换过去

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T, n, k;
int a[N];
int x;
void solve() {
	cin >> n >> k;
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		cin >> x;
		a[i] = abs(x - i);
		if (a[i] % k != 0) cnt++;
	}
	if (cnt == 0) cout << 0 << endl;
	else if (cnt == 2) cout << 1 << endl;
	else cout << -1 << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Strongly Composite

题目大意:

解题思路:

题目要求强复合数,其实可以把a数组的乘积用唯一分解,优先选单个数的平方,其次选3个不同的数

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
const int M = 1e7;
ll T, n, x;
ll resh[M + 5];//i的最小质因子
void init() {
	for (ll i = 2; i <= M; i++) {
		if (resh[i]) continue;
		for (ll j = i; j <= M; j += i) {
			if (!resh[j]) resh[j] = i;
		}
	}
}
unordered_map<ll, ll> mp;
void solve() {
	cin >> n;
	mp.clear();
	for (ll i = 0; i < n; i++) {
		cin >> x;
		while (x > 1) {
			ll pr = resh[x];
			mp[pr]++;
			x /= pr;
		}
	}
	ll ans = 0;
	ll cnt = 0;
	for (auto i : mp) {
		ans += i.second / 2;
		cnt += i.second & 1;
	}
	ans += cnt / 3;
	cout << ans << endl;
}
int main() {
	init();
	cin >> T;
	while (T--) solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值