408 - Uniform Generator

Click to show code.


using namespace std;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int main(void)
{
    int a, b;
    string ans;
    while (cin >> a >> b)
    {
        if (gcd(a, b) == 1)
            ans = "Good Choice";
        else
            ans = "Bad Choice";
        cout << setw(10) << right << a << setw(10) << b << setw(4) << " "
             << left << ans << endl
             << endl;
    }
}


405A - Gravity Flip

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int n;
    vi v;
    cin >> n;
    v.resize(n);
    for (auto &x : v)
        cin >> x;
    sort(v.begin(), v.end());
    for (auto x : v)
        cout << x << " ";
    return 0;
}


381A - Sereja Dina

Click to show code.


using namespace std;
using di = deque<int>;
using vi = vector<int>;
int main(void)
{
    int n;
    cin >> n;
    di cards(n);
    vi ans(2, 0);
    for (auto &x : cards)
        cin >> x;
    for (int i = 0; i < n; ++i)
    {
        if (cards.front() < cards.back())
        {
            ans[i % 2] += cards.back();
            cards.pop_back();
        }
        else
        {
            ans[i % 2] += cards.front();
            cards.pop_front();
        }
    }
    cout << ans[0] << " " << ans[1] << endl;
    return 0;
}


380C - Sereja And Brackets

Click to show code.


using namespace std;
using ii = pair<int, int>;
using vii = vector<ii>;
struct segtree
{
    int n;
    vii t;
    segtree(int _n) : n(_n) { t.resize(4 * _n); }
    segtree(const string &s) : segtree(s.size()) { build(s, 1, 0, n - 1); }
    void build(const string &s, int v, int tl, int tr)
    {
        if (tl == tr)
        {
            if (s[tl] == '(')
                t[v] = {1, 0};
            else
                t[v] = {0, 1};
        }
        else
        {
            int tm = (tl + tr) / 2;
            build(s, 2 * v, tl, tm);
            build(s, 2 * v + 1, tm + 1, tr);
            t[v] = merge(t[2 * v], t[2 * v + 1]);
        }
    }
    int accumulate_pair(ii a) const { return a.first + a.second; }
    ii merge(ii a, ii b) const
    {
        return {b.first + max(0, a.first - b.second),
                a.second + max(0, b.second - a.first)};
    }
    int query(int ql, int qr) const
    {
        int unmatched = accumulate_pair((_query(1, 0, n - 1, ql, qr)));
        return (qr - ql + 1 - unmatched);
    }
    ii _query(int v, int tl, int tr, int ql, int qr) const
    {
        if (ql > qr)
            return {0, 0};
        else if (tl == ql and tr == qr)
            return t[v];
        int tm = (tl + tr) / 2;
        return merge(_query(2 * v, tl, tm, ql, min(tm, qr)),
                     _query(2 * v + 1, tm + 1, tr, max(ql, tm + 1), qr));
    }
};
int main(void)
{
    int m, l, r;
    string s;
    cin >> s >> m;
    segtree st(s);
    while (m--)
    {
        cin >> l >> r;
        cout << st.query(l - 1, r - 1) << endl;
    }
    return 0;
}


357 - Let Me Count Ways

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 3e4 + 11;
int n, coins[5] = {1, 5, 10, 25, 50};
ll dp[NMAX];
ll solve(void)
{
    dp[0] = 1;
    for (int i = 0; i < 5; ++i)
        for (int j = coins[i]; j <= n; ++j)
            dp[j] += dp[j - coins[i]];
    return dp[n];
}
int main(void)
{
    ll m;
    while (cin >> n)
    {
        memset(dp, 0, sizeof(dp));
        m = solve();
        if (m == 1)
            cout << "There is only 1 way to produce ";
        else
            cout << "There are " << m << " ways to produce ";
        cout << n << " cents change." << endl;
    }
    return 0;
}


346A - Alice Bob

Click to show code.


using namespace std;
int n;
int a[110];
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int solve(void)
{
    sort(a, a + n);
    int ngcd = a[0];
    for (int i = 1; i < n; ++i)
    {
        ngcd = gcd(a[i], ngcd);
        if (ngcd == 1)
            break;
    }
    return ((a[n - 1] / ngcd) - n) % 2;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    cout << (solve() ? "Alice" : "Bob") << endl;
    return 0;
}


344A - Magnets

Click to show code.


using namespace std;
int main(void)
{
    int n, last, ai, ans = 1;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> ai;
        if (i != 0 and ai != last)
            ans += 1;
        last = ai;
    }
    cout << ans << endl;
    return 0;
}


339D - Xenia Bit Operations

Click to show code.


using namespace std;
const int NMAX = (1L << 17) + 11;
int n, height, a[NMAX], seg[4 * NMAX];
int f(int x, int y, int h) { return (h % 2 == 0 ? x ^ y : x | y); }
void build(int a[], int v, int tl, int tr)
{
    if (tl == tr)
        seg[v] = a[tl];
    else
    {
        int tm = (tl + tr) / 2;
        build(a, v * 2, tl, tm);
        build(a, v * 2 + 1, tm + 1, tr);
        seg[v] = f(seg[v * 2], seg[v * 2 + 1], height - (int)log2(v));
    }
}
void update(int v, int tl, int tr, int pos, int x)
{
    if (pos < tl or pos > tr)
        return;
    if (tl == tr and tl == pos)
        seg[v] = x;
    else
    {
        int tm = (tl + tr) / 2;
        update(v * 2, tl, tm, pos, x);
        update(v * 2 + 1, tm + 1, tr, pos, x);
        seg[v] = f(seg[v * 2], seg[v * 2 + 1], height - (int)log2(v));
    }
}
int main(void)
{
    int m, p, b;
    cin >> n >> m;
    n = 1L << n;
    height = log2(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    build(a, 1, 0, n - 1);
    while (m--)
    {
        cin >> p >> b;
        update(1, 0, n - 1, p - 1, b);
        cout << seg[1] << endl;
    }
    return 0;
}


277A - Learning Languages

Click to show code.


using namespace std;
int n, m;
int parent[2 * 110];
int siz[2 * 110];
void make_set(int v)
{
    parent[v] = v;
    siz[v] = 1;
}
int find_set(int v)
{
    if (parent[v] == v)
        return v;
    return find_set(parent[v]);
}
void union_sets(int u, int v)
{
    u = find_set(u);
    v = find_set(v);
    if (u != v)
    {
        parent[v] = u;
        siz[u] += siz[v];
    }
}
int main(void)
{
    int ki, aij, count = 0;
    bool isolated = true;
    cin >> n >> m;
    for (int i = 0, len = n + m; i < len; ++i)
        make_set(i);
    for (int i = 0; i < n; ++i)
    {
        cin >> ki;
        while (ki--)
        {
            cin >> aij;
            union_sets(i, aij - 1 + n);
        }
    }
    for (int i = 0; i < n; ++i)
    {
        if (siz[i] != 1)
            isolated = false;
        if (parent[i] == i)
            ++count;
    }
    cout << count - not(isolated) << endl;
    return 0;
}


276D - Little Girl Maximum Xor

Click to show code.


using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
inline bool different_bits(ll x, ll y, int i)
{
    return ((x >> i) ^ (y >> i)) & 1LL;
}
ll solve(ll l, ll r)
{
    ll ans = 0, bit_period = 1, diff = r - l;
    int i = 0;
    while (diff >= bit_period)
    {
        ans += bit_period;
        bit_period *= 2;
        i++;
    }
    while (r >= bit_period)
    {
        if (different_bits(l, r, i))
            ans += bit_period;
        bit_period *= 2;
        i++;
    }
    return ans;
}
int main(void)
{
    ll l, r;
    cin >> l >> r;
    cout << solve(l, r) << endl;
    return 0;
}