1. 程式人生 > 實用技巧 >AtCoder Regular Contest 106

AtCoder Regular Contest 106

A

讀錯提了, A, B都是從1開始

int main() {
    IOS; ll n;__int128 a = 3; cin >> n;
    for (int i = 1, j = 1; a < n; ++i, a *= 3, j = 1) {
        __int128 b = 5;
        for (; a + b < n; ++j, b *= 5);
        if (a + b == n) {
            cout << i << ' ' << j; return 0;
        }
    }
    cout << -1;
    return 0;
}

B

在連通分量內, 總差值和為0就行

int n, m, _, k;
int f[N];
ll a[N], b[N], s[N];

int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}

void unit(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    f[y] = x, s[x] += s[y];
}

int main() {
    IOS; cin >> n >> m;
    rep (i, 1, n) cin >> a[i], f[i] = i;
    rep (i, 1, n) cin >> b[i], s[i] = a[i] - b[i];
    rep (i, 1, m) {
        int u, v; cin >> u >> v;
        unit(u, v);
    }

    bool g = 1;
    rep (i, 1, n) if (i == f[i] && s[i]) g = 0;
    if (g) cout << "Yes";
    else cout << "No"; 
    return 0;
}

C

分情況討論

int main() {
    IOS; cin >> n >> m;
    if ((m < 0 || m > n - 2) && m != 0) { cout << -1 << '\n'; return 0; }
    if (m) {
        cout << 1 << ' ' << 2 + (m + 1 << 1) << '\n';
        rep (i, 1, m + 1) cout << i * 2 << ' ' << i * 2 + 1 << '\n';
        rep (i, 1, n - m - 2) cout << 1 + (m + 1 << 1) + i * 2 << ' ' << 2 + (m  + 1 << 1) + i * 2 << '\n';
    } else {
        rep (i, 1, n) cout << -1 + i * 2 << ' ' << i * 2 << '\n';
    }
    return 0;
}