7/14 训练笔记

闲话

数组开小挂分
Kruskal\(m = 9e6\) TLE

问题 D: Card Game

简单猜结论得到答案是 \(2 ^ {n - 1} - 1\),需要快速幂。
代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, n;
int qpow(int x, int y) {
    int res = 1;
    while (y) {
        if (y & 1) (res *= x) %= 998244353;
        (x *= x) %= 998244353;
        y >>= 1;
    }
    return res;
}
signed main() {
    cin.tie(0) -> ios :: sync_with_stdio(false);
    cin >> t;
    while (t--) {
        cin >> n;
        int ans = 1;
        ans = qpow(2, n - 1);
        cout << ans - 1 << "\n";
    }
}

问题 I: String Problem

统计每一个连续相同字符的字符串,然后给答案加上 \(len - 1\)
代码:

#include <bits/stdc++.h>
using namespace std;
int t, ans;
string s;
int main() {
    cin >> t;
    while (t--) {
        ans = 0;
        cin >> s;
        int l = 0, r = 0;
        while (r < s.size()) {
            int cnt = 0;
            while (r < s.size() && s[r] == s[l]) {
                cnt++;
                r++;
            }
            ans += cnt - 1;
            l = r;
        }
        cout << ans << "\n";
    }
}
posted @ 2024-07-14 23:37  IANYEYZ  阅读(6)  评论(1编辑  收藏  举报