1392A - Omkar Password

Click to show code.


using namespace std;
int main(void)
{
    int t, n, ai;
    cin >> t;
    while (t--)
    {
        cin >> n;
        set<int> s;
        for (int i = 0; i < n; ++i)
        {
            cin >> ai;
            s.insert(ai);
        }
        if (s.size() == 1)
            cout << n << endl;
        else
            cout << 1 << endl;
    }
    return 0;
}


1391C - Cyclic Permutations

Click to show code.


using namespace std;
using ll = long long;
int const MOD = 1e9 + 7;
int const NMAX = 1e6 + 11;
ll fact[NMAX], inv[NMAX];
void precompute()
{
    fact[0] = 1;
    for (int i = 1; i < NMAX; i++)
        fact[i] = fact[i - 1] * i % MOD;
    inv[1] = 1;
    for (int i = 2; i < NMAX; ++i)
        inv[i] = (MOD - (MOD / i) * inv[MOD % i] % MOD) % MOD;
}
int extended_gcd(int a, int b, int &x, int &y)
{
    x = 1, y = 0;
    int x1 = 0, y1 = 1, a1 = a, b1 = b;
    while (b1)
    {
        int q = a1 / b1;
        tie(x, x1) = make_tuple(x1, x - q * x1);
        tie(y, y1) = make_tuple(y1, y - q * y1);
        tie(a1, b1) = make_tuple(b1, a1 - q * b1);
    }
    return a1;
}
ll inverse(int a)
{
    if (a < NMAX)
        return inv[a];
    int x, y;
    int mod = MOD;
    int g = extended_gcd(a, mod, x, y);
    if (g != 1)
    {
        return 0;
    }
    else
    {
        x = (x % MOD + MOD) % MOD;
        return x;
    }
}
ll nck(int n, int k)
{
    return fact[n] * inverse(fact[k]) % MOD * inverse(fact[n - k]) % MOD;
}
ll mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
ll sub(ll a, ll b) { return ((a % MOD) - (b % MOD) + MOD) % MOD; }
int main(void)
{
    int n;
    cin >> n;
    precompute();
    ll ans = fact[n];
    ll cur = 1;
    for (int k = 0; k < n - 1; ++k)
    {
        cur = mult(2, cur);
    }
    cout << sub(ans, cur);
    return 0;
}


1391B - Fix You

Click to show code.


using namespace std;
int main(void)
{
    int t, n, m, ans;
    char cij;
    cin >> t;
    while (t--)
    {
        ans = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                cin >> cij;
                if (i == n - 1 and j == m - 1)
                    continue;
                if (j == m - 1 and cij != 'D')
                    ++ans;
                else if (i == n - 1 and cij != 'R')
                    ++ans;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


1391A - Suborrays

Click to show code.


using namespace std;
int main(void)
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cout << i << " ";
        cout << endl;
    }
    return 0;
}


1389A - Lcm Problem

Click to show code.


using namespace std;
using ii = pair<int, int>;
ii solve(int l, int r)
{
    if (2 * l <= r)
        return {l, 2 * l};
    else
        return {-1, -1};
}
int main(void)
{
    int t, l, r;
    cin >> t;
    while (t--)
    {
        cin >> l >> r;
        auto [a, b] = solve(l, r);
        cout << a << " " << b << endl;
    }
    return 0;
}


1385D - A Good String

Click to show code.


using namespace std;
int n;
string s;
int cost(int l, int r, char c)
{
    return count_if(
        s.begin() + l, s.begin() + r + 1, [c](char d) { return d != c; });
}
int solve(int l, int r, char c)
{
    if (r - l == 0)
        return c != s[l];
    else
    {
        int m = l + (r - l) / 2;
        return min(cost(l, m, c) + solve(m + 1, r, c + 1),
                   solve(l, m, c + 1) + cost(m + 1, r, c));
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> s;
        cout << solve(0, n - 1, 'a') << endl;
    }
    return 0;
}


1385B - Restore Permutation Merger

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int t, n, ai;
    vi cnt;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cnt.assign(n + 1, 0);
        n *= 2;
        while (n--)
        {
            cin >> ai;
            cnt[ai]++;
            if (cnt[ai] == 2)
                cout << ai << " ";
        }
        cout << endl;
    }
    return 0;
}


1374E1 - Reading Books

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
int solve(int k, vector<ii> tab)
{
    sort(tab.begin(), tab.end());
    vector<int> choices;
    queue<int> queued[2];
    for (auto [t, sign] : tab)
    {
        if (sign == 2)
            choices.push_back(t);
        else
        {
            queued[sign].push(t);
            if (not queued[0].empty() and not queued[1].empty())
            {
                choices.push_back(queued[0].front() + queued[1].front());
                queued[0].pop(), queued[1].pop();
            }
        }
    }
    sort(choices.begin(), choices.end());
    if ((int)choices.size() < k)
        return -1;
    else
        return accumulate(choices.begin(), choices.begin() + k, 0);
}
int main(void)
{
    int n, k;
    cin >> n >> k;
    vector<ii> tab;
    for (int i = 0; i < n; ++i)
    {
        int ti, ai, bi;
        cin >> ti >> ai >> bi;
        if (not ai and not bi)
            continue;
        else
            tab.push_back({ti, (ai and bi ? 2 : ai)});
    }
    cout << solve(k, tab) << endl;
    return 0;
}


1370C - Number Game

Click to show code.


using namespace std;
using ll = long long;
bool is_prime(int n)
{
    if (n < 2)
        return false;
    for (int x = 2; x * x <= n; x++)
    {
        if (n % x == 0)
            return false;
    }
    return true;
};
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t, n, m;
    cin >> t;
    while (t--)
    {
        cin >> n;
        m = 1;
        if (n % 2 == 1 and n != 1)
            m = 0;
        else if (n == 2)
            m = 0;
        else
        {
            int cnt = 0;
            while (n % 2 == 0)
            {
                n /= 2;
                ++cnt;
            }
            if ((n != 1 and cnt > 1) or (cnt == 1 && !is_prime(n)))
                m = 0;
        }
        cout << (m == 0 ? "Ashishgup" : "FastestFinger") << endl;
    }
    return 0;
}


1370B - Gcd Compression

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 1e3 + 11;
int n, a[2 * NMAX];
void solve(void)
{
    vi rem[2];
    for (int i = 0; i < 2 * n; ++i)
        rem[a[i] % 2].push_back(i);
    if ((int)rem[0].size() % 2 == 1)
    {
        rem[0].pop_back();
        rem[1].pop_back();
    }
    else
    {
        int j = 0;
        if ((int)rem[j].size() < (int)rem[1 - j].size())
            j = 1 - j;
        rem[j].pop_back();
        rem[j].pop_back();
    }
    for (int r = 0; r < 2; ++r)
    {
        for (int i = 0; i < (int)rem[r].size(); ++i)
        {
            cout << rem[r][i] + 1 << " ";
            if (i % 2 == 1)
                cout << endl;
        }
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < 2 * n; ++i)
            cin >> a[i];
        solve();
    }
    return 0;
}