1622 - Creating Strings I

Click to show code.


using namespace std;
int main(void)
{
    string s;
    vector<string> ans;
    cin >> s;
    sort(s.begin(), s.end());
    do
    {
        ans.push_back(s);
    } while (next_permutation(s.begin(), s.end()));
    cout << ans.size() << endl;
    for (auto a : ans)
        cout << a << endl;
    return 0;
}


1621 - Distinct Numbers

Click to show code.


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


1620 - Factory Machines

Click to show code.


using namespace std;
using ll = unsigned long long;
using predicate = function<bool(ll)>;
const ll NMAX = 2e5 + 11;
ll n, nprods, k[NMAX];
bool possible(ll t)
{
    return nprods <= accumulate(k, k + n, (ll)0, [t](ll acc, ll ki) -> ll {
               return acc + t / ki;
           });
}
ll bsearch(ll l, ll r, predicate p)
{
    while (l < r)
    {
        ll mid = l + (r - l) / 2;
        if (p(mid))
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main(void)
{
    cin >> n >> nprods;
    for (ll i = 0; i < n; ++i)
        cin >> k[i];
    cout << bsearch(0, 1e18 + 11, possible) << endl;
    return 0;
}


161D - Distance In Tree

Click to show code.


using namespace std;
using vi = vector<int>;
int const NMAX = 5e4 + 11;
int const KMAX = 5e2 + 11;
int n, k, dp[NMAX][KMAX], cnt;
vi g[NMAX];
void dfs(int u, int p = -1)
{
    dp[u][0] = 1;
    for (auto v : g[u])
    {
        if (v != p)
        {
            dfs(v, u);
            for (int i = 1; i <= k; i++)
                cnt += dp[v][i - 1] * dp[u][k - i];
            for (int i = 1; i <= k; i++)
                dp[u][i] += dp[v][i - 1];
        }
    }
}
int main(void)
{
    cin >> n >> k;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1);
    cout << cnt << endl;
    return 0;
}


1619 - Restaurant Customers

Click to show code.


using namespace std;
using ii = pair<int, int>;
using vii = vector<ii>;
int main(void)
{
    int n, a, b, people = 0, ans = 0;
    cin >> n;
    vii events(2 * n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a >> b;
        events[2 * i] = {a, +1};
        events[2 * i + 1] = {b, -1};
    }
    sort(events.begin(), events.end());
    for (auto [t, s] : events)
    {
        people += s;
        ans = max(ans, people);
    }
    cout << ans << endl;
    return 0;
}


1618 - Trailing Zeros

Click to show code.


using namespace std;
int solve(int n)
{
    if (n == 0)
        return 0;
    return n / 5 + solve(n / 5);
}
int main(void)
{
    int n;
    cin >> n;
    cout << solve(n) << endl;
    return 0;
}


1617 - Bit Strings

Click to show code.


using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
int solve(int n)
{
    ll ans = 1;
    for (int i = 0; i < n; ++i)
        ans = (ans * 2) % MOD;
    return ans;
}
int main(void)
{
    int n;
    cin >> n;
    cout << solve(n) << endl;
    return 0;
}


160 - Factors Factorials

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
using mii = map<int, int>;
const int NMAX = 1e7 + 11;
bitset<NMAX> is_prime;
vector<int> prime;
mii prime_factors(ll n)
{
    mii factors;
    for (int j = 2; j <= n; ++j)
    {
        int ni = j;
        ll i = 0, pf = prime[i];
        while (pf * pf <= ni)
        {
            while (ni % pf == 0)
            {
                ++factors[pf];
                ni = ni / pf;
            }
            pf = prime[++i];
        }
        if (ni != 1)
            ++factors[ni];
    }
    return factors;
}
void sieve(ll maxn)
{
    ll sieve_size = maxn + 1;
    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;
            prime.push_back((int)i);
        }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    sieve(NMAX);
    int n;
    while (cin >> n and n)
    {
        cout << right << setw(3) << n;
        cout << "! =";
        int i = 0;
        for (auto elem : prime_factors(n))
        {
            if (i > 0 && i % 15 == 0)
                cout << endl << "      ";
            cout << right << setw(3) << elem.second;
            ++i;
        }
        cout << endl;
    }
    return 0;
}


14D - Two Paths

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = array<int, 3>;
using vi = vector<int>;
int const NMAX = 200 + 11;
int n;
vi g[NMAX];
vector<ii> edges;
ii forbidden_edge;
bool is_forbidden(int u, int v)
{
    auto [up, vp] = forbidden_edge;
    return (u == up and v == vp) or (u == vp and v == up);
}
ii furthest_node(int u, int p, int d)
{
    ii ans = {d, u};
    for (int v : g[u])
    {
        if (v == p or is_forbidden(u, v))
            continue;
        ans = max(ans, furthest_node(v, u, d + 1));
    }
    return ans;
}
int find_diameter(int root)
{
    auto [d1, u] = furthest_node(root, -1, 0);
    auto [d2, v] = furthest_node(u, -1, 0);
    return d2;
}
int main(void)
{
    cin >> n;
    edges.resize(n);
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        edges[i] = {u, v};
    }
    int ans = 0;
    for (auto [u, v] : edges)
    {
        forbidden_edge = {u, v};
        int a = find_diameter(u), b = find_diameter(v);
        ans = max(ans, a * b);
    }
    cout << ans << endl;
    return 0;
}


1478B - Nezzar And Lucky Number

For a given $d$, we can observe the following:

  1. Each number $x$, $x \geq 10d$ can be built by a combination of an element in the range $10d, 10d + 9$ and zero or more $d$.
  2. Each number $x$, $x < 10d$ has the form: $10 k_1 + d k_2$.

Using these two facts, we can can brute_force $x < 10d$ and answer right away $x \geq 10d$.

Time complexity: $O(q \log{n})$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
bool test(int x, int d)
{
    for (int i = d; i <= x; i += d)
        if (x % 10 == i % 10)
            return true;
    while (x)
    {
        if (x % 10 == d)
            return true;
        x /= 10;
    }
    return false;
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int q, d;
        cin >> q >> d;
        while (q--)
        {
            int x;
            cin >> x;
            if (x >= d * 10 or test(x, d))
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
    }
    return 0;
}