SMU Summer 2024 Contest Round 7

SMU Summer 2024 Contest Round 7

Make Equal With Mod

题意

给定一个长度为 \(n\) 的数列 \(a\)。你可以执行若干次操作,每次操作选择一个大于等于 \(2\) 的整数 \(x\),然后对于所有的 \(1\leqslant i\leqslant n\),将 \(a_i\) 替换为 \(a_i\bmod x\)

请判断是否有一种操作序列,使得依次执行完该操作序列中的所有操作之后,数列中的所有数都相等。

思路

最小模数为 2,因此原生的 0 和 1 是无法被消掉的,也就是说,只要存在 1 和 0 ,这个序列就不可能相等,相差为 1 的两个数不管怎么消都会存在一个 0 ,然后再判断下原来有没有 1 即可。

代码

#include<bits/stdc++.h>

using namespace std;

using i64 = long long;

void solve() {

    int n;
    cin >> n;

    int one = 0;
    vector<int>a(n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        one += a[i] == 1;
    }

    sort(a.begin() + 1, a.end());

    for (int i = 2; i <= n; i ++) {
        if (a[i] == a[i - 1] + 1 && one) {
            cout << "NO\n";
            return ;
        }
    }

    cout << "YES\n";

    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Game on Ranges

题意

Alice 和 Bob 在玩一个和区间有关的有趣的游戏。他们手上有一个集合 \(S\),里面一开始只有一个区间 \([1,n]\)。我们定义一个区间 \([a,b]\) 是合法的,当且仅当 \(a\leqslant b\)。在每一步操作中,Alice 可以从集合 \(S\) 中选出一个区间 \([l,r]\),然后让 Bob 在这个区间中选择一个整数 \(d\),Alice 随后会把区间 \([l,r]\) 从集合 \(S\) 里面拿出,放入区间 \([l,d-1]\)\([d+1,r]\) 中所有合法的区间。当集合 \(S\) 为空时,游戏结束。我们不难发现这个游戏进行了恰好 \(n\) 次操作。

游戏之后,Alice 记得所有她取出来的区间 \([l,r]\) 但是不记得取这些区间的先后顺序,而 Bob 完全忘记了他从每个区间中取出的整数 \(d\)。然而众所周知,Bob 是绝顶聪明的,因此他知道他可以根据 Alice 取出的区间来判断他在每个区间中选出了哪个整数 \(d\)。现在,请你帮助 Bob 完成这个工作。

思路

考虑从大区间往小区间判断顺序。

每次只会消掉一个数,也就是说每消掉第 i 个数,那么剩下的区间肯定还覆盖 n-i 个数,对区间用差分前缀和维护覆盖的数,然后每次判断那些数是新被消掉的即可。

代码

#include<bits/stdc++.h>

using namespace std;

using i64 = long long;

void solve() {

    int n;
    cin >> n;

    vector<array<int, 2>> qu(n);
    for (auto &[l, r] : qu) {
        cin >> l >> r;
    }

    sort(qu.begin(), qu.end(), [](auto x, auto y) {
        return x[1] - x[0] > y[1] - y[0];
    });

    set<int> s;
    for (int i = 0; i < n; i ++) {

        auto [lo, ro] = qu[i];

        int len = 0;
        vector<int> f(n + 2);
        for (int j = i + 1; j < n; j ++) {
            auto [l, r] = qu[j];
            len += r - l + 1;
            f[l]++, f[r + 1]--;
        }
        for (int j = 1; j <= n; j ++) {
            f[j] += f[j - 1];
            if (!f[j] && !s.count(j)) {
                s.insert(j);
                cout << lo << ' ' << ro << ' ' << j << '\n';
                break;
            }
        }
    }

    cout << '\n';

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Buy an Integer

题意

\(i=1\sim1e9\) 找到满足 \(A\times i + B\times d(i) \le X\) 的最大数 i,\(d(i)\) 表示 i 的位数。

思路

提示数据范围了,很明显二分。

代码

#include<bits/stdc++.h>

using namespace std;

using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    i64 A, B, X;
    cin >> A >> B >> X;

    i64 l = 0, r = 1e9, ans = 0;
    while (l <= r) {
        i64 mid = l + r >> 1;
        int len = to_string(mid).size();
        if (A * mid + B * len <= X) l = mid + 1, ans = mid;
        else r = mid - 1;
    }

    cout << ans << '\n';

    return 0;
}

String Formation

题意

给你个字符串 \(S\)\(Q\) 次操作,操作 1 翻转字符串,操作 2 根据 F 的值在字符串前后添加一个字符,问最终字符串。

思路

难点在于翻转,考虑翻转对操作 2 的影响其实就是操作 2 的两个操作相反,所以只要记录一个值代表当前字符串是否翻转即可。

代码

#include<bits/stdc++.h>

using namespace std;

using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;

    int q;
    cin >> q;

    int re = 0;
    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            re ^= 1;
        } else {
            int f;
            char c;
            cin >> f >> c;
            if (re) {
                if (f == 1) s += c;
                else s = c + s;
            } else {
                if (f == 2) s += c;
                else s = c + s;
            }
        }
    }

    if (re) reverse(s.begin(), s.end());

    cout << s << '\n';

    return 0;
}

Bouquet

题意

你有 n 种不同的花,每种花只有一朵,问你选出的方案数其种类数量不为 a 或 b 的方案数。

思路

根据题意,即求 :

\[2^n-C_n^a-C_n^b \]

n 大到 1e9 ,直接求肯定不现实,考虑转化 \(C_n^a=\frac{n\times(n-1)\times\dots\times(n-a+1)}{A_a^a}\),b 同理。

代码

#include<bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 mod = 1e9 + 7;

i64 ksm(i64 x, i64 y) {
    i64 res = 1;
    while (y) {
        if (y & 1) res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}

i64 A(i64 n, i64 m) {
    i64 res = 1;
    for (int i = m; i >= 1; i--) {
        res = res * n % mod;
        n--;
    }
    return res % mod;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    i64 n, a, b;
    cin >> n >> a >> b;

    i64 x = 1, y = 1;
    for (int i = 0; i < a; i ++) {
        x = x * (n - i) % mod;
    }

    x = x * ksm(A(a, a), mod - 2) % mod;

    for (int i = 0; i < b; i ++) {
        y = y * (n - i) % mod;
    }
    
    y = y * ksm(A(b, b), mod - 2) % mod;

    cout << (ksm(2, n) - x - y - 1 + mod + mod) % mod << '\n';


    return 0;
}

Permutation

题意

第一行给定 \(n\)\(m\)。后 \(m\) 行,每行给定一个规则。

  • 规则:后 \(m\) 行每行给出三个整数 \(X_i,Y_i,Z_i\),表示在排列的前 \(X_i\) 个数字中最多只能有 \(Z_i\) 个数字小于等于 \(Y_i\)

构造长度为 \(n\) 的排列,求最多可以构造多少个满足所有规则的排列。

思路

考虑状压。

以 i 为状态,每个位置上有 1 则说明选了一个数,用 __builtin_popcount这个函数计算出二进制中的 1 ,当选的个数小于 \(X_j\) 时,用 \(i \& ((1<<Y_j)-1)\) 判断这个状态在前 \(Y_j\) 的位置上放置的个数有没有大于 \(Z_j\) ,存在则说明该状态不合法,跳过即可。

之后就是判断每一位是否为 1 ,是就加上把不放这个数,也就是把这个位置置为 0 的方案数。

代码

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<array<int, 3>> a(m);
    for (auto &[x, y, z] : a)
        cin >> x >> y >> z;

    vector<i64> dp(1 << n);
    dp[0] = 1;
    for (int i = 0; i < (1 << n); i ++) {
        bool f = 1;
        for (int j = 0; j < m; j ++) {
            auto [x, y, z] = a[j];
            if (__builtin_popcount(i) <= x && __builtin_popcount(i & ((1 << y) - 1)) > z)
                f = 0;
        }

        if (!f) continue;

        for (int j = 0; j < n; j ++) {
            if (i >> j & 1)
                dp[i] += dp[i - (1 << j)];
        }
    }

    cout << dp[(1 << n) - 1] << '\n';

    return 0;
}

String Cards

题意

给出 N 个串,请你在其中选出 K 个串,使得这 K 个串前后拼接形成的串字典序最小。

思路

乍一看是简单题,可能认为排序而已,但是2 2 b ba 该数据就可以卡掉。

考虑按照 \(s_i+s_{i+1}>s_{i+1}+s_i\) 这样排序使得其相对拼接后更小,然后采用 dp 的方式滚掉第一维倒序选取 k 个字符串。

代码

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;

    vector<string> s(n + 1);
    for (int i = 1; i <= n; i ++)
        cin >> s[i];

    sort(s.begin() + 1, s.end(), [](auto x, auto y) {
        return x + y > y + x;
    });

    vector<string> dp(k + 1, string(1, 'z' + 1));

    dp[0] = "";
    for (int i = 1; i <= n; i ++) {
        for (int j = min(i, k); j >= 1; j --) {
            dp[j] = min(dp[j], s[i] + dp[j - 1]);
        }
    }

    cout << dp[k] << '\n';

    return 0;
}
posted @ 2024-07-26 21:31  Ke_scholar  阅读(11)  评论(0编辑  收藏  举报