1. 程式人生 > 其它 >Educational Codeforces Round 116 (Rated for Div. 2)ABCE題解

Educational Codeforces Round 116 (Rated for Div. 2)ABCE題解

比賽地址Educational Codeforces Round 116 (Rated for Div. 2)

A. AB Balance

容易看出,直接判斷首尾字母是否相同即可,因為最後要化成 aba...a 或者 bab...b 形式才行

#include <bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
const int INF = 0x3f3f3f3f, N = 1e5 + 10;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1);
inline int lowbit(int x) {return x & (-x);}

inline void solve() {
    string s; cin >> s;
    s[0] = s[s.size() - 1];
    cout << s << endl;
}
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    auto now = clock();
#endif
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(2);
    int T; cin >> T;
    while (T -- )
        solve();
#ifdef DEBUG
    cout << "============================" << endl;
    cout << "Program run for " << (clock() - now) / (double)CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
    return 0;
}

B. Update Files

根據題意模擬即可

#include <bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
const int INF = 0x3f3f3f3f, N = 1e5 + 10;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1);
inline int lowbit(int x) {return x & (-x);}

inline void solve() {
    LL n, k; cin >> n >> k;
    LL now = 1, ans = 0, tag = 1;
    while (now < n) {
        ans ++;
        now += tag;
        tag <<= 1ll;
        if (tag > k) {
            ans += (n - now + k - 1) / k;
            break;
        }
    }
    cout << ans << endl;
}
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    auto now = clock();
#endif
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(2);
    int T; cin >> T;
    while (T -- )
        solve();
#ifdef DEBUG
    cout << "============================" << endl;
    cout << "Program run for " << (clock() - now) / (double)CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
    return 0;
}

C. Banknotes

貪心即可

容易看出,比能拼的數多出 \(1\) 即可。

#include <bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
const int INF = 0x3f3f3f3f, N = 1e5 + 10;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1);
inline int lowbit(int x) {return x & (-x);}

inline void solve() {
    LL n, k; cin >> n >> k;
    vector<LL> a(n + 1);
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i];
        a[i] = (LL)pow(10, a[i]);
    }
    k ++;
    LL res = 0;
    for (int i = 1; i < n; i ++ ) {
        LL d = a[i + 1] / a[i] - 1;
        if (k >= d) {
            res += a[i] * d;
            k -= d;
        } else {
            res += a[i] * k;
            k = 0;
        }
    }
    if (!k) cout << res << endl;
    else {
        res += a[n] * k;
        cout << res << endl;
    }
}
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    auto now = clock();
#endif
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(2);
    int T; cin >> T;
    while (T -- )
        solve();
#ifdef DEBUG
    cout << "============================" << endl;
    cout << "Program run for " << (clock() - now) / (double)CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
    return 0;
}

D. Red-Blue Matrix

待補

E. Arena

考慮dp

狀態設計 \(dp[i][j]\) 表示有 \(i\) 個人,血量範圍是 \(j\) 的方案數

首先考慮最簡單的,當每個人第一輪被淘汰的情況,容易看出只能在 \(1到n - 1\) 取值,對於 \(n\) 個人,每人有 \(n - 1\) 種取法,同時考慮到其血量範圍,故取 \(dp[n][x] = min(n - 1, x) ^ {n}\)

如果 \(x < n - 1\) 那麼只可能是第一輪就被淘汰完,否則繼續考慮

考慮每輪的淘汰人數,由於不能存在最後勝利者,所以我們取每輪淘汰的人數為 \(k\)\(k \in [0, n - 2]\)

然後直接轉移即可

\(dp[n][x] = \sum_{k=0}^{n - 2}(n - 1)^k * dp[n - k][x - n + 1] * C_n^i\)

稍微解釋一下,由於本輪淘汰了 \(k\) 人,並且本輪所有人扣的血量為 \(n - 1\),所以我們只能從 \(dp[n - k][x - n + 1]\) 中轉移,相當於 \(k\) 個人可以分配到 \(1到n - 1\) 的血量,所以是 \((n - 1)^k\),在算上從 \(n\) 個人中選擇 \(i\) 個人淘汰 \(C_n^i\),得到轉移式。

用記憶化搜尋寫即可

#include <bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
const int INF = 0x3f3f3f3f, N = 550;
const int MOD = 998244353;
const double eps = 1e-6;
const double PI = acos(-1);
inline int lowbit(int x) {return x & (-x);}

LL dp[N][N], c[N][N];

LL q_pow(LL a, LL b) {
    LL res = 1;
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
    }
    return res;
}

LL dfs(int n, int x) {
    if (dp[n][x]) return dp[n][x];
    dp[n][x] = q_pow(min(x, n - 1), n);
    if (x <= n - 1) return dp[n][x];
    for (int i = 0; i <= n - 2; i ++ ) dp[n][x] = (dp[n][x] + q_pow(n - 1, i) * dfs(n - i, x - n + 1) % MOD * c[n][i] % MOD) % MOD;
    return dp[n][x];
}

inline void solve() {
    int n, x; cin >> n >> x;
    for (int i = 0; i <= 500; i ++ ) {
        for (int j = 0; j <= i; j ++ ) {
            if (i == 0 || j == 0) c[i][j] = 1;
            else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
        }
    }
    cout << dfs(n, x) << endl;
}
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    auto now = clock();
#endif
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(2);
//    int T; cin >> T;
//    while (T -- )
        solve();
#ifdef DEBUG
    cout << "============================" << endl;
    cout << "Program run for " << (clock() - now) / (double)CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
    return 0;
}