1. 程式人生 > 實用技巧 >Educational Codeforces Round 98 (Rated for Div. 2)

Educational Codeforces Round 98 (Rated for Div. 2)

A - Robot Program

int main() {
	IOS;
	for (cin >> _; _; --_) {
		ll a, b, ans = 0; cin >> a >> b;
                cout << a + b + max(abs(b - a) - 1, 0ll) << '\n';
        }
	return 0;
}

B - Toy Blocks

對每個都允許, 先設最少x, 原先總和為 sum, 則

(sum + x) / (n - 1) 是整數

其次保證 max(ai) >= (sum + x) / (n - 1)

同時 max(ai) * (n - 1) >= sum

答案就有了

int main() {
	IOS;
	for (cin >> _; _; --_) {
		cin >> n; ll mx = -1, s = 0;
                rep (i, 1, n) cin >> a[i], umax(mx, a[i]), s += a[i];
                ll ans = mx * (n - 1ll) - s;
                if (ans < 0) ans = (ans % (n - 1) + (n - 1)) % (n - 1);
                cout << ans << '\n';
        }
	return 0;
}

C - Two Brackets

比b簡單

int main() {
	IOS;
	for (cin >> _; _; --_) {
		string s; cin >> s;
        ll ans = 0, x = 0, y = 0;
        for (auto c : s) 
            if (c == '(') ++x;
            else if (c == '[') ++y;
            else if (c == ')') {
                if (x) ++ans, --x;
            } else if (c == ']') {
                if (y) ++ans, --y;
            }
        cout << ans << '\n';
    }
	return 0;
}

D - Radio Towers

dp, 設 d[i] 為正好包住 i 的方案數量

ll d[N], a[N];
 
ll qpow(ll a, ll b) {
    ll ans = 1;
    for (; b; b >>= 1, a = a * a % mod)
        if (b & 1) ans = ans * a % mod;
    return ans;
}
 
int main() {
	IOS; cin >> n; a[0] = d[0] = 1;
        rep (i, 1, n) a[i] = d[i - 1], d[i] = (a[i] + d[i - 2]) % mod;
        cout << a[n] * qpow(qpow(2, mod - 2), n) % mod;
	return 0;
}