agc002_b - Box And Ball

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int n, m, xi, yi;
    vi red, cnt;
    cin >> n >> m;
    cnt.resize(n, 1), red.resize(n, false);
    red[0] = true;
    for (int i = 0; i < m; ++i)
    {
        cin >> xi >> yi, xi--, yi--;
        red[yi] |= red[xi];
        cnt[xi] -= 1;
        cnt[yi] += 1;
        if (cnt[xi] == 0)
            red[xi] = false;
    }
    cout << accumulate(red.begin(), red.end(), 0) << endl;
    return 0;
}


abc183_c - Travel

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector<vi> t(n, vi(n));
    for (auto &ti : t)
        read_n(ti.begin(), n);
    vi a(n);
    for (int i = 0; i < n; ++i)
        a[i] = i;
    int ans = 0;
    do
    {
        int cur = 0;
        for (int i = 0; i < n; ++i)
            cur += t[a[i]][a[(i + 1) % n]];
        ans += cur == k;
    } while (next_permutation(a.begin() + 1, a.end()));
    cout << ans << endl;
    return 0;
}


abc180_d - Takahashi Unevolved

Click to show code.


using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
ll iceil(ll a, ll b) { return (a + b - 1) / b; }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    ll x, y, a, b;
    cin >> x >> y >> a >> b;
    ll xp = 0;
    while (x * a <= x + b and iceil(y, x) > a)
    {
        x *= a;
        xp++;
    }
    ll d = (y - x) % b == 0 ? (y - x - 1) / b : (y - x) / b;
    xp += d;
    cout << xp << endl;
    return 0;
}


abc179_c - Axb+C

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using mii = map<int, int>;
const int NMAX = 1e7 + 11;
bitset<NMAX> is_prime;
vector<int> primes;
mii prime_factors(ll n)
{
    mii factors;
    ll i = 0, pf = primes[i];
    while (pf * pf <= n)
    {
        while (n % pf == 0)
        {
            ++factors[pf];
            n = n / pf;
        }
        pf = primes[++i];
    }
    if (n != 1)
        ++factors[n];
    return factors;
}
void sieve(ll sieve_size)
{
    is_prime.set();
    is_prime[0] = is_prime[1] = 0;
    for (ll i = 2; i <= sieve_size; i++)
        if (is_prime[i])
        {
            for (ll j = i * i; j <= sieve_size; j += i)
                is_prime[j] = 0;
            primes.push_back((int)i);
        }
}
int main(void)
{
    int n;
    cin >> n;
    sieve(NMAX);
    ll ans = 0;
    for (int x = 1; x < n; ++x)
    {
        ll cur = 1;
        auto factors = prime_factors(x);
        for (auto [k, v] : factors)
            cur *= v + 1;
        ans += cur;
    }
    cout << ans << endl;
    return 0;
}


abc179_b - Go To Jail

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int n;
    cin >> n;
    vector<ii> d(n);
    for (auto &[f, s] : d)
        cin >> f >> s;
    int cnt = 0;
    for (auto [f, s] : d)
    {
        if (f == s)
        {
            cnt++;
        }
        else
        {
            if (cnt >= 3)
            {
                cout << "Yes" << endl;
                return 0;
            }
            cnt = 0;
        }
    }
    if (cnt >= 3)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}


abc179_a - Plural Form

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    string s;
    cin >> s;
    if (s.back() == 's')
        cout << s << "es" << endl;
    else
        cout << s << "s" << endl;
    return 0;
}


abc178_f - Contrast

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int n;
    cin >> n;
    vi a(n), b(n);
    for (auto &ai : a)
        cin >> ai;
    for (auto &bi : b)
        cin >> bi;
    sort(b.begin(), b.end(), greater<int>());
    int l = 0, r = n - 1;
    for (int i = 0; i < n; ++i)
    {
        if (b[i] == a[i])
        {
            if (a[l] != b[i])
            {
                swap(b[l], b[i]);
                ++l;
            }
        }
    }
    for (int i = n - 1; i >= 0; --i)
    {
        if (b[i] == a[i])
        {
            if (a[r] != b[i])
            {
                swap(b[r], b[i]);
                --r;
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        if (b[i] == a[i])
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i)
    {
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}


abc178_d - Redistribution

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
int const MOD = 1e9 + 7;
int const SMAX = 2000 + 11;
ll mem[SMAX];
bool vis[SMAX];
ll add(ll a, ll b) { return ((a + MOD % MOD) + (b + MOD % MOD)) % MOD; }
ll dp(int s)
{
    if (s == 0)
        return 1;
    if (vis[s])
        return mem[s];
    ll &ans = mem[s];
    vis[s] = true;
    for (int d = 3; d <= s; ++d)
        ans = add(ans, dp(s - d));
    return ans;
}
int main(void)
{
    int s;
    cin >> s;
    cout << dp(s) << endl;
    return 0;
}


abc178_c - Ubiquity

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
ll const MOD = 1e9 + 7;
ll binpow(ll a, ll b)
{
    a %= MOD;
    ll ans = 1;
    while (b > 0)
    {
        if (b & 1)
            ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}
ll sub(ll a, ll b) { return ((a + MOD % MOD) - (b + MOD % MOD) + MOD) % MOD; }
ll add(ll a, ll b) { return ((a + MOD % MOD) + (b + MOD % MOD)) % MOD; }
int main(void)
{
    ll n;
    cin >> n;
    ll U = binpow(10, n);
    ll Z = binpow(9, n);
    ll N = binpow(9, n);
    ll ZuN = binpow(8, n);
    ll ans = sub(U, sub(add(Z, N), ZuN));
    cout << ans << endl;
    return 0;
}


abc178_b - Product Max

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    ll ans = max({a * c, a * d, b * c, b * d});
    cout << ans << endl;
    return 0;
}