A 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;
}
B 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;
}
C 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;
}