Codeforces Round 948 (Div. 2) A~D

A. Little Nikita (思维)

题意:

小 A A A决定用一些立方体建一座塔。一开始,塔上没有任何立方体。在一次移动中,小 A A A要么正好把 1 1 1 个立方体放到塔顶,要么正好从塔顶移走 1 1 1 个立方体。存不存在一种可能使得在走了 n n n 步之后,塔顶正好有 m m m 个立方体?

分析:

当 n ≥ m n \ge m n≥m并且 n , m n,m n,m奇偶性一样才符合题意,否则不可以。

代码:

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n, m;
        cin >> n >> m;
        if (n >= m && (n - m) % 2 == 0) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
    return 0;
}

B.Binary Colouring (思维)

题意:

给你一个正整数 x x x 。请找出下列条件成立的任意整数数组 a 0 , a 1 , ... , a n − 1 a_0, a_1, \ldots, a_{n-1} a0,a1,...,an−1 :

  • 1 ≤ n ≤ 32 1 \le n \le 32 1≤n≤32 ,
  • a i a_i ai 是 1 1 1 、 0 0 0 或 − 1 -1 −1
  • x = ∑ i = 0 n − 1 a i ⋅ 2 i x = \displaystyle{\sum_{i=0}^{n - 1}{a_i \cdot 2^i}} x=i=0∑n−1ai⋅2i ,
  • 不存在同时满足 a i ≠ 0 a_{i} \neq 0 ai=0 和 a i + 1 ≠ 0 a_{i + 1} \neq 0 ai+1=0 的下标 i i i

保证存在一个有效的数组。

分析:

如果没有相邻两项必有一项为 0 0 0的限制,那么就是写出 n n n的二进制形式。如果 n n n的二进制有大于等于 2 2 2个 1 1 1相邻,将最低位置为 − 1 -1 −1,最高位的后一项置为 1 1 1,双指针判断即可。

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        vector<int> a(32), b(32);
        for (int i = 0; i <= 31; i++) {
            if (n >> i & 1) {
                a[i] = 1;
                b[i] = 1;
            } else {
                a[i] = 0;
                b[i] = 0;
            }
        }
        for (int i = 0; i < 32;) {
            if (a[i] != 0 && b[i + 1] != 0) {
                int pos = i;
                while (a[i] != 0 && i <= 31) {
                    i++;
                    b[i] = 0;
                }
                a[i] = 1;
                b[pos] = -1;
                b[i] = 1;
                if (i == 32)
                    break;
            } else {
                i++;
                if (i == 32)
                    break;
            }
        }
        cout << 32 << endl;
        for (int i = 0; i < 32; i++) {
            cout << b[i] << " ";
        }
        cout << endl;
    }
    return 0;
}

C.Nikita and LCM(数学)

题意:

假设有一个长度为 n n n 的整数数组 a a a 。如果数组的一个子序列的最小公倍数 ( L C M ) (LCM) (LCM)不包含在 a a a 中,称该子序列为特殊序列。空子序列的 L C M LCM LCM 等于 0 0 0 。

询问 a a a 的最长特殊子序列的长度是多少?

分析:

如果所有数的最小公倍数大于数组中最大值,那么就可以全选。否则数组中所有数一定都是最小公倍数的约数,那么我们就能保证最终选出来的数组的最小公倍数一定也是其约数。那么我们枚举可能的约数然后判断一遍即可。

代码:

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int mod = 998244353;

LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}

LL lcm(LL a, LL b) {
    return a * b / gcd(a, b);
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a.begin(), a.end());
        set<int> tmp;
        for (int i = 0; i < n; i++)
            tmp.insert(a[i]);
        bool ok = 0;
        for (int i = 0; i < n; i++) {
            if (a[n - 1] % a[i])
                ok = 1;
        }
        if (ok) {
            cout << n << endl;
            continue;
        }
        int ans = 0;
        auto check = [&](int x) -> int {
            if (tmp.find(x) != tmp.end())
                return 0;
            int g = 1;
            int cnt = 0;
            for (int i = 0; i < n; i++) {
                int num = lcm(g, a[i]);
                if (x % num)
                    continue;
                cnt++;
                g = num;
            }
            if (g != x)
                return 0;
            return cnt;
        };
        for (int i = 1; i * i <= a[n - 1]; i++) {
            if (a[n - 1] % i)
                continue;
            ans = max(ans, check(i));
            ans = max(ans, check(a[n - 1] / i));
        }
        cout << ans << endl;
    }
    return 0;
}

D.XORificator (字符串)

题意:

给一个仅由 0 0 0和 1 1 1组成 n × m n \times m n×m 矩阵。你可以反转一次所选行中的所有值。规定矩阵中的一列如果正好包含一个 1 1 1,则被视为特殊列。在至多进行一次操作的情况下,请找出最多有多少列可以被特殊化。

分析:

我们发现对于每一列,若它们对答案有贡献,一共有 n n n种情况,且这 n n n种情况一定各不相同,那么我们把各列的这些情况全部记录下来,同时记录出现次数,记作 c n t cnt cnt。

我们将字符串记作哈希值,那么答案就是 c n t cnt cnt 中的最大值。这样我们同时也知道答案对应的字符串哈希值 n u m num num,我们重新枚举各列对答案的贡献 t m p tmp tmp,当 t m p = n u m tmp=num tmp=num 即找到对应的字符串。

代码:

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
using ULL = unsigned long long;
const int maxn = 3e5 + 5;
static const ULL mod = (1ull << 61) - 1;
ULL power[maxn];
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<ULL> dist(mod / 2, mod - 1);
const ULL base = dist(rnd);

static inline ULL add(ULL a, ULL b) {
    a += b;
    if (a >= mod)
        a -= mod;
    return a;
}

static inline ULL mul(ULL a, ULL b) {
    __uint128_t c = __uint128_t(a) * b;
    return add(c >> 61, c & mod);
}

ULL merge(ULL h1, ULL h2, int len2) {
    return add(mul(h1, power[len2]), h2);
}

void init() {
    power[0] = 1;
    for (int i = 1; i < maxn; i++)
        power[i] = mul(power[i - 1], base);
}

ULL query(const vector<ULL> &s, int l, int r) {
    return add(s[r], mod - mul(s[l - 1], power[r - l + 1]));
}

vector<ULL> build(const string &s) {
    int sz = s.size();
    vector<ULL> hashed(sz + 1);
    for (int i = 0; i < sz; i++) {
        hashed[i + 1] = add(mul(hashed[i], base), s[i]);
    }
    return hashed;
}

int main() {
    init();
    int T;
    cin >> T;
    while (T--) {
        int n, m;
        cin >> n >> m;
        vector<string> s(m);
        for (int i = 0; i < m; i++)
            s[i].resize(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> s[j][i];
            }
        }
        unordered_map<ULL, int> mp;
        vector<vector<ULL>> tmp(m, vector<ULL>(n));
        for (int i = 0; i < m; i++) {
            auto hash = build(s[i]);
            for (int j = 1; j <= n; j++) {
                ULL hash1 = query(hash, 1, j - 1);
                ULL hash2 = ULL(s[i][j - 1] ^ 1);
                ULL hash3 = query(hash, j + 1, n);
                tmp[i][j - 1] = merge(merge(hash1, hash2, 1), hash3, n - j);
                mp[tmp[i][j - 1]] += 1;
            }
        }
        int maxval = -1;
        ULL ans = 0;
        for (auto &[x, y]: mp) {
            if (y > maxval) {
                maxval = y;
                ans = x;
            }
        }
        auto get = [&]() {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (tmp[i][j] == ans) {
                        s[i][j] ^= 1;
                        cout << s[i] << endl;
                        return;
                    }
                }
            }
        };
        cout << maxval << endl;
        get();
    }
    return 0;
}

赛后交流

在比赛结束后,会在交流群中给出比赛题解,同学们可以在赛后查看题解进行补题。

群号: 704572101,赛后大家可以一起交流做题思路,分享做题技巧,欢迎大家的加入。

相关推荐
penguin_bark32 分钟前
69. x 的平方根
算法
这可就有点麻烦了42 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊1 小时前
顺序表及其代码实现
数据结构·算法
lin zaixi()1 小时前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.1 小时前
C - Separated Lunch
算法·深度优先
夜雨翦春韭1 小时前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
Kent_J_Truman1 小时前
【平方差 / C】
算法
一直学习永不止步1 小时前
LeetCode题练习与总结:H 指数--274
java·数据结构·算法·leetcode·数组·排序·计数排序
Amor风信子2 小时前
华为OD机试真题---跳房子II
java·数据结构·算法
戊子仲秋2 小时前
【LeetCode】每日一题 2024_10_2 准时到达的列车最小时速(二分答案)
算法·leetcode·职场和发展