A
。
#include "bits/stdc++.h"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int x = (n + 9) / 10;
if (k <= x) {
cout << "Gold Medal\n";
} else if (k <= 3 * x) {
cout << "Silver Medal\n";
} else if (k <= 6 * x) {
cout << "Bronze Medal\n";
} else {
cout << "Da Tie\n";
}
return 0;
}
B
至少为 。
。
#include "bits/stdc++.h"
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << max(n, *max_element(a.begin(), a.end())) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C
最多输出 。
。
#include "bits/stdc++.h"
using namespace std;
void solve() {
string s;
cin >> s;
int n = s.size();
auto s0 = s.substr(0, n / 2);
reverse(s0.begin(), s0.end());
if (s0 == s.substr((n + 1) / 2, n / 2) || s.find('1') == -1 || s.find('0') == -1) {
cout << "1\n";
return;
}
cout << "2\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
D
题目变为在 个数中选出
个,使得这
个数的按位与尽可能大。考虑从高到低按位贪心,若满足条件的数有
个,则保留该位。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
void solve() {
int n;
cin >> n;
int m = n + n;
vector<i64> a(m);
for (int i = 0; i < m; i++) {
cin >> a[i];
}
i64 ans = 0;
for (int b = 60; b >= 0; b--) {
i64 c = ans | (1LL << b);
int cnt = 0;
for (int i = 0; i < m; i++) {
if ((a[i] & c) == c) {
cnt++;
}
}
if (cnt >= n) {
ans = c;
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
E
正难则反。
记录 为数组中
的数量。
为数组中
的倍数的数量。
对于某个 ,任意一个全部由
的倍数组成且至少包含一个等于
的元素的非空子序列,其
必然等于
。记这类子序列个数为
(从所有
个倍数中选子集,减去不选任何等于
的子集)。
所有非空子序列总数为 。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
constexpr int P = 998244353;
constexpr int N = 2E5;
i64 pw2[N + 1];
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<int> cnt(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
vector<int> m(n + 1);
for (int g = 1; g <= n; g++) {
for (int j = g; j <= n; j += g) {
m[g] += cnt[j];
}
}
i64 ans = (pw2[n] - 1 + P) % P;
for (int g = 1; g <= n; g++) {
ans = ((ans + pw2[m[g] - cnt[g]]) % P - pw2[m[g]] + P) % P;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
pw2[0] = 1;
for (int i = 1; i <= N; i++) {
pw2[i] = pw2[i - 1] * 2 % P;
}
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
F
考虑状态压缩 dp。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
constexpr int P = 1000000007;
void solve() {
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
const int N = 1 << n;
vector<i64> g(N);
vector<int> mn(n), sum(n);
for (int i = 0; i < n; i++) {
for (char c : s[i]) {
if (c == '(') {
sum[i]++;
} else {
sum[i]--;
}
mn[i] = min(mn[i], sum[i]);
}
}
for (int mask = 1; mask < N; mask++) {
int lg = __lg(mask);
g[mask] = g[mask ^ (1 << lg)] + sum[lg];
}
if (g[N - 1]) {
cout << "0\n";
return;
}
vector<i64> dp(N);
dp[0] = 1;
for (int mask = 0; mask < N; mask++) {
for (int i = 0; i < n; i++) {
if (!(mask >> i & 1)) {
if (g[mask] + mn[i] >= 0) {
dp[mask | (1 << i)] = (dp[mask | (1 << i)] + dp[mask]) % P;
}
}
}
}
cout << dp[N - 1] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}