1409D - Decrease Sum Digits

Click to show code.


using namespace std;
using ll = long long;
int dsum(ll n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
ll make_zero(ll n, int i)
{
    int j = i;
    while (i--)
    {
        n /= 10;
    }
    n += 1;
    while (j--)
    {
        n *= 10;
    }
    return n;
}
ll solve(ll n, ll s)
{
    ll ans = 0, i = 1;
    while (dsum(n) > s)
    {
        ll cur = make_zero(n, i);
        ans += (cur - n);
        n = cur;
        ++i;
    }
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        cout << solve(n, s) << endl;
    }
    return 0;
}


1409C - Yet Another Array Restoration

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, x, y;
        cin >> n >> x >> y;
        ll bmax = 1e9 + 7, bdiff;
        for (int between = 0; between <= n - 2; ++between)
        {
            if ((y - x) % (between + 1) != 0)
                continue;
            ll diff = (y - x) / (between + 1);
            ll cmin = x, cnt = n - 2 - between;
            while ((cmin - diff) >= 1 and cnt > 0)
            {
                cnt--;
                cmin -= diff;
            }
            ll cmax = cmin + (n - 1) * diff;
            if (cmax < bmax)
            {
                bmax = cmax;
                bdiff = diff;
            }
        }
        for (int i = 0; i < n; ++i)
            cout << bmax - bdiff * i << " ";
        cout << endl;
    }
    return 0;
}


1409B - Minimum Product

Click to show code.


using namespace std;
using ll = long long;
pair<ll, ll> op(ll a, ll x, ll n)
{
    ll p = max(a - n, x);
    n = n - (a - p);
    return {p, n};
}
ll simulate(ll a, ll b, ll x, ll y, ll n)
{
    auto [ap, np1] = op(a, x, n);
    a = ap, n = np1;
    auto [bp, np2] = op(b, y, n);
    b = bp, n = np2;
    return a * b;
}
int main(void)
{
    ll t;
    cin >> t;
    while (t--)
    {
        ll a, b, x, y, n;
        cin >> a >> b >> x >> y >> n;
        ll ans0 = simulate(a, b, x, y, n);
        ll ans1 = simulate(b, a, y, x, n);
        cout << min(ans0, ans1) << endl;
    }
    return 0;
}


1409A - Yet Another Two Integers

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll a, b;
        cin >> a >> b;
        ll diff = abs(b - a);
        ll ans = diff / 10 + ((diff % 10) > 0);
        cout << ans << endl;
    }
    return 0;
}


1408C - Discrete Acceleration

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using predicate = function<bool(double)>;
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));
}
ll n, L;
vi a, ar;
double T(double d, vi const &flag)
{
    int i = 0, flag_prev = 0;
    double tans = 0.0;
    double cur_speed = 1.0;
    while (i < n and flag[i] < d)
    {
        tans += (flag[i] - flag_prev) / cur_speed;
        flag_prev = flag[i];
        cur_speed++;
        ++i;
    }
    tans += (d - flag_prev) / cur_speed;
    return tans;
}
double bs(double l, double r, predicate p)
{
    for (int iter = 0; iter < 1000; ++iter)
    {
        double m = l + (r - l) / 2.0;
        if (p(m))
            r = m;
        else
            l = m;
    }
    return T(l, a);
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> L;
        a.resize(n), ar.resize(n);
        read_n(a.begin(), n);
        for (int i = 0; i < n; ++i)
            ar[n - i - 1] = L - a[i];
        cout << fixed << setprecision(9) << bs(0.0, double(L), [](double d) {
            return T(d, a) - T(L - d, ar) >= 0.0;
        }) << endl;
    }
    return 0;
}


1408B - Arrays Sum

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 t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vi a(n);
        read_n(a.begin(), n);
        int nu = distance(a.begin(), unique(a.begin(), a.end()));
        if (k == 1 and nu > 1)
            cout << -1 << endl;
        else
        {
            int ans = 0;
            do
            {
                nu -= min(k, nu);
                nu++;
                ans++;
            } while (nu != 1);
            cout << ans << endl;
        }
    }
    return 0;
}


1408A - Circle Coloring

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 t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi abc[3], ans(n);
        for (int j = 0; j < 3; ++j)
        {
            abc[j].resize(n);
            read_n(begin(abc[j]), n);
        }
        ans[0] = abc[0][0];
        for (int i = 1; i < n - 1; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                if (abc[j][i] != ans[i - 1])
                {
                    ans[i] = abc[j][i];
                    break;
                }
            }
        }
        for (int j = 0; j < 3; ++j)
        {
            if (abc[j][n - 1] != ans[n - 2] and abc[j][n - 1] != ans[0])
            {
                ans[n - 1] = abc[j][n - 1];
                break;
            }
        }
        write(ans.begin(), ans.end(), " "), cout << endl;
    }
    return 0;
}


1407C - Chocolate Bunny

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;
    queue<int> qix;
    vi ans(n);
    for (int i = 1; i <= n; ++i)
        qix.push(i);
    while ((int)qix.size() > 1)
    {
        int ans0, ans1;
        int ix0 = qix.front();
        qix.pop();
        int ix1 = qix.front();
        qix.pop();
        cout << "? " << ix0 << " " << ix1 << endl;
        cout.flush();
        cin >> ans0;
        cout << "? " << ix1 << " " << ix0 << endl;
        cout.flush();
        cin >> ans1;
        if (ans0 > ans1)
        {
            ans[ix0 - 1] = ans0;
            qix.push(ix1);
        }
        else
        {
            ans[ix1 - 1] = ans1;
            qix.push(ix0);
        }
    }
    ans[qix.front() - 1] = n;
    cout << "! ";
    for (auto x : ans)
        cout << x << " ";
    cout << endl;
    return 0;
}


1407B - Big Vova

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n);
        for (auto &ai : a)
            cin >> ai;
        sort(a.begin(), a.end(), greater<int>());
        vi ans = {a[0]};
        vector<bool> visited(n, false);
        int i = 1;
        while (i < n and a[i] == a[0])
        {
            visited[i] = true;
            ans.push_back(a[i]);
            ++i;
        }
        int agcd = a[0];
        while ((int)ans.size() < n)
        {
            int ix, g = 1;
            for (int j = i; j < n; ++j)
            {
                if (visited[j])
                    continue;
                if (gcd(agcd, a[j]) > g)
                {
                    g = gcd(agcd, a[j]);
                    ix = j;
                }
            }
            if (g == 1)
            {
                for (int j = i; j < n; ++j)
                {
                    if (not visited[j])
                    {
                        ans.push_back(a[j]);
                        visited[j] = true;
                    }
                }
            }
            else
            {
                visited[ix] = true;
                ans.push_back(a[ix]);
                agcd = gcd(agcd, g);
            }
        }
        for (auto x : ans)
            cout << x << " ";
        cout << endl;
    }
    return 0;
}


1407A - Ahahahahahahahaha

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int ones = 0, zeros = 0;
        for (int i = 0; i < n; ++i)
        {
            char c;
            cin >> c;
            if (c == '0')
                zeros++;
            else
                ones++;
        }
        int m = n / 2;
        if (zeros >= ones)
        {
            cout << m << endl;
            for (int i = 0; i < m; ++i)
                cout << '0' << " ";
            cout << endl;
        }
        else
        {
            if (m % 2 == 1)
                m++;
            cout << m << endl;
            for (int i = 0; i < m; ++i)
                cout << '1' << " ";
            cout << endl;
        }
    }
    return 0;
}