Codeforces Round 948 (Div. 2) A~D

A. Little Nikita (思维)

题意:

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

分析:

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

代码:

#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 (思维)

题意:

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

  • 1≤n≤321 \le n \le 321n32 ,
  • aia_iai111000−1-11
  • x=∑i=0n−1ai⋅2ix = \displaystyle{\sum_{i=0}^{n - 1}{a_i \cdot 2^i}}x=i=0n1ai2i ,
  • 不存在同时满足 ai≠0a_{i} \neq 0ai=0ai+1≠0a_{i + 1} \neq 0ai+1=0 的下标iii

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

分析:

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

代码:

#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(数学)

题意:

假设有一个长度为 nnn 的整数数组 aaa 。如果数组的一个子序列的最小公倍数 (LCM)(LCM)(LCM)不包含在 aaa 中,称该子序列为特殊序列。空子序列的 LCMLCMLCM 等于 000
询问 aaa 的最长特殊子序列的长度是多少?

分析:

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

代码:

#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 (字符串)

题意:

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

分析:

我们发现对于每一列,若它们对答案有贡献,一共有nnn种情况,且这nnn种情况一定各不相同,那么我们把各列的这些情况全部记录下来,同时记录出现次数,记作cntcntcnt
我们将字符串记作哈希值,那么答案就是 cntcntcnt 中的最大值。这样我们同时也知道答案对应的字符串哈希值numnumnum,我们重新枚举各列对答案的贡献tmptmptmp,当tmp=numtmp=numtmp=num 即找到对应的字符串。

代码:

#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,赛后大家可以一起交流做题思路,分享做题技巧,欢迎大家的加入。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值