2023杭电暑假多校7 BDKM 题解 | JorbanS

B. Random Nim Game

题意 n n n 个石子堆,每次随机选择一堆,随机拿走这堆任意个石子,拿走最后一个石子的人获胜,求先手获胜的概率 ( m o d   998244353 ) (mod~998244353) (mod 998244353)

题解 当所有石子堆的石子数量都为 1 1 1 时,只能没人每次拿走一个,则若 n n n 为奇数,则先手胜利,否则后手胜利

其余情况由于完全随机,所以先手胜利的概率为 1 2 \frac 1 2 21

#include <iostream>
using namespace std;
#define FastIO cin.tie(nullptr) -> sync_with_stdio(false);
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define endl '\n'

typedef long long ll;
const int mod = 998244353;

int qpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = (ll)res * a % mod;
        a = (ll)a * a % mod;
        b >>= 1;
    }
    return res;
}

int inv(int a) { return qpow(a, mod - 2); }

int solve() {
    int n; cin >> n;
    bool flag = true;
    for (int i = 0; i < n; i ++) {
        int x; cin >> x;
        if (x != 1) flag = false;
    }
    if (flag) return n & 1;
    return inv(2);
}

int main() {
    FastIO
    Cases
    cout << solve() << endl;
    return 0;
}

D. Medians Strike Back

题意 构造长度为 n n n 的含 1 , 2 , 3 1,2,3 1,2,3 的数列 a a a,输出 m i n ( C ) min(C) min(C)

d e f   A def~A def A A ( x ) A(x) A(x) 表示 x x x 在区间内出现的次数

d e f   B def~B def B:升序排列下, B = { A ( a n + 1 2 ) , n 为奇 m a x ( A ( a n 2 ) , A ( a n 2 + 1 ) , n 为偶 B=\begin{cases}A(a_{\frac{n+1}2}),n为奇\\max(A(a_{\frac n2}),A(a_{\frac n2+1}),n为偶\end{cases} B={A(a2n+1),n为奇max(A(a2n),A(a2n+1),n为偶

d e f   C def~C def C a a a 的所有连续子序列中 B B B 的最大值

打表 c o d e code code

#include <iostream>
#include <algorithm>
#define FastIO cin.tie(nullptr) -> sync_with_stdio(false);
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define endl '\n'
using namespace std;
const int N = 15;
int res, a[N], b[N], ans[N] = {0, 1};

int cal(int l, int r) {
    int n = r - l + 1;
    for (int i = l, j = 0; i <= r; i ++, j ++) b[j] = a[i];
    sort(b, b + n);
    int x = b[n >> 1];
    int cnt = upper_bound(b, b + n, x) - lower_bound(b, b + n, x);
    if (n & 1 ^ 1) {
        x = (n >> 1) + 1;
        cnt = max(cnt, (int)(upper_bound(b, b + n, x) - lower_bound(b, b + n, x)));
    }
    return cnt;
}

void dfs(int n, int cur) {
    if (cur == n) {
        int cnt = 1;
        for (int i = 1; i < n; i ++)
            for (int j = i + 1; j <= n; j ++)
                cnt = max(cnt, cal(i, j));
        if (cnt < res) {
            res = cnt;
            for (int i = 1; i <= n; i ++) ans[i] = a[i];
        }
        return;
    }
    for (int i = 1; i <= 3; i ++) {
        a[cur + 1] = i;
        dfs(n, cur + 1);
    }
}

void solve(int n) {
    res = n;
    dfs(n, 0);
    cout << "n = " << n << ", res = " << res << ": ";
    for (int i = 1; i <= n; i ++) cout << ans[i] << ' ';
    cout << endl;
}

int main() {
    FastIO
    Cases
    solve(_);
    return 0;
}

打表结果:

在这里插入图片描述

没找出规律反正,就直接手玩吧,参照官方题解:构造 { 1 , 3 , 1 , 3 , . . . , 1 , 3 , 2 , 2 , 1 , 3 , . . . } \{1,3,1,3,...,1,3,2,2,1,3,...\} {1,3,1,3,...,1,3,2,2,1,3,...},即 x x x 1   3 1~3 1 3 之后 2   2 2~2 2 2 构成一循环。

任何一个长度为 2 x + 2 2x + 2 2x+2 的区间一定至少有 2 2 2 2 2 2,上述构造同时满足 2 2 2 最少,二分 x x x 即可

#include <iostream>
#define FastIO cin.tie(nullptr) -> sync_with_stdio(false);
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define endl '\n'
using namespace std;

int main() {
    FastIO
    Cases
    {
        int n; cin >> n;
        int l = 1, r = n;
        while (l < r) {
            int mid = l + r >> 1;
            int t = n / (mid * 2 + 2);
            int s = t * 2 + max(0, n % (mid * 2 + 2) - mid * 2);
            if (s <= mid) r = mid;
            else l = mid + 1;
        }
        cout << l << endl;
    }
    return 0;
}

K. Three Operations

题意 给定 a , b a,b a,b,对于 x x x 进行如下操作,最少需要多少步能够使 x = 0 x=0 x=0

  • x = x − 1 x=x-1 x=x1
  • x = ⌊ n + a 2 ⌋ x=\left\lfloor\frac{n+a}2\right\rfloor x=2n+a
  • x = ⌊ n + b ⌋ x=\left\lfloor\sqrt{n+b}\right\rfloor x=n+b

题解 如果 x = 0 x=0 x=0 直接退出, r e s = 0 res=0 res=0。其他情况每次比较三种选择,直到只有 x = x − 1 x=x-1 x=x1 这个选择能够使得 x x x 减小时,直接输出 r e s + x res+x res+x 即可

#include <iostream>
#include <cmath>
using namespace std;
#define FastIO cin.tie(nullptr) -> sync_with_stdio(false);
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define endl '\n'
#define aa ((x + a) / 2)
#define bb ((ll)sqrt(x + b))
#define cc (x - 1)

typedef long long ll;

int solve() {
    ll x, a, b; cin >> x >> a >> b;
    ll res = 0;
    while (x) {
        if (aa >= x && bb >= x) return res + x;
        x = min(aa, min(bb, cc));
        res ++;
    }
    return res;
}

int main() {
    FastIO
    Cases
    cout << solve() << endl;
    return 0;
}

M. Minimal and Maximal XOR Sum

题意 n n n 个数的排列 p p p,每次选择一个区间 [ l , r ] [l,r] [l,r] 翻转,操作耗费 r − l + 1 r-l+1 rl+1,最终为升序排列,求操作异或和的最小值和最大值

题解 用冒泡排序求至少需要多少次操作,因为只进行两两互换,因此最终异或和为 0 0 0 2 2 2,判断操作次数的奇偶性即可,但是数据范围达到 1 0 5 10^5 105,因此需要使用归并排序求逆序对

再贪心考虑异或值的最大,首先可以交换一个数 [ l , l ] [l,l] [l,l],有 r e s + + res++ res++

异或值的二进制表示
___ ___ ___ ___ _1/0_ _1_

因为交换 2 2 2 个是不可逆的(通过其他操作不可返回),而翻转连续的 4 , 8 , 16 , 32 , . . . 4,8,16,32,... 4,8,16,32,... 个(对应异或值的二进制位 3 , 4 , 5 , 6 , . . . 3,4,5,6,... 3,4,5,6,...),可以再通过偶数次的冒泡将其翻转回去,因此贪心可以使得除了低 2 2 2 位的其余高位全部为 1 1 1

异或值的二进制表示
_1_ _1_ _1_ _1_ _1/0_ _1_

此时,异或值已为最大

#include <iostream>
using namespace std;
#define FastIO cin.tie(nullptr) -> sync_with_stdio(false);
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define endl '\n'

typedef long long ll;
const int N = 1e5 + 2;
int n, m, a[N], tmp[N];

int merge_sort(int l, int r) {
    if (l >= r) return 0;
    int mid = (l + r) >> 1;
    int res = merge_sort(l, mid) + merge_sort(mid + 1, r);
    int k = 0, i = l, j = mid + 1;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) tmp[k ++] = a[i ++];
        else {
            res += mid - i + 1;
            tmp[k ++] = a[j ++];
        }
    }
    while (i <= mid) tmp[k ++] = a[i ++];
    while (j <= r) tmp[k ++] = a[j ++];
    for (int i = l, k = 0; i <= r; )a[i ++] = tmp[k ++];
    return res & 1;
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    int res = 2 * (merge_sort(1, n) & 1);
    cout << res << ' ';
    int idx = 4;
    while (idx <= n) {
        res += idx;
        idx <<= 1;
    }
    cout << ++ res << endl;
}

int main() {
    FastIO
    Cases
    solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JorbanS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值