Saturday, December 23, 2023

[Hackerearth] Tree operations

 

Problem Link    : Tree operations
Category        : Approximate Problem
Contest         : December Circuits'23
#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
using pii = pair<int, int>;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int Rand(int l, int r) {
    int rnd = uniform_int_distribution<int>(l, r)(rng);
    return rnd;
}
 
const int maxn = 2e5 + 5;
const int logn = 20;
 
int n;
int k;
int arr[maxn];
vector<vector<int>> graph;
int depth[maxn];
int father[maxn][logn];
 
void dfs(int u, int p, int lvl) {
    depth[u] = lvl;
    for (int i = 1; i < logn; i++) {
        father[u][i] = father[ father[u][i - 1] ][i - 1];
    }
    for (int v : graph[u]) {
        if (v == p) {
            continue;
        }
        father[v][0] = u;
        dfs(v, u, lvl + 1);
    }
}
 
int findLca(int u, int v) {
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    for (int i = logn - 1; i >= 0; i--) {
        if (depth[ father[u][i] ] >= depth[v]) {
            u = father[u][i];
        }
    }
    if (u == v) {
        return u;
    }
    for (int i = logn - 1; i >= 0; i--) {
        if (father[u][i] != father[v][i]) {
            u = father[u][i];
            v = father[v][i];
        }
    }
    return father[u][0];
}
 
vector<int> up(int u, int lca, bool isForV = false) {
    vector<int> curPath;
    do {
        curPath.emplace_back(u);
        if (u == lca) {
            break;
        }
        u = father[u][0];
    } while (true);
    if (isForV) {
        reverse(curPath.begin(), curPath.end());
        curPath.pop_back();
    }
    return curPath;
}
 
int getEdgesSum(int u, int p) {
    int sum = 0;
    for (int v : graph[u]) {
        if (v == p) {
            continue;
        }
        int add = (arr[u] ^ arr[v]) * (arr[u] | arr[v]);
        sum += add + getEdgesSum(v, u);
    }
    return sum;
}
 
int solve(int &u, int &v, int operations) {
    u = Rand(1, n);
    v = Rand(1, n);
    int lca = findLca(u, v);
    vector<int> pathOfU = up(u, lca);
    vector<int> pathOfV = up(v, lca, true);
    vector<int> path;
    for (int x : pathOfU) {
        path.push_back(x);
    }
    for (int x : pathOfV) {
        path.push_back(x);
    }
    int sz = path.size();
    int temp = arr[ path[0] ];
    for (int i = 0; i < path.size(); i++) {
        int j = (i + 1) % sz;
        arr[ path[i] ] = j == 0 ? temp : arr[ path[j] ];
    }
    int sum = getEdgesSum(1, 0);
    return sum * (k - operations + 1);
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
 
    cin >> n >> k;
    graph.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
    }
    for (int e = 1; e < n; e++) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dfs(1, 0, 1);
    int maxi = 0;
    int optimalOperation = 0;
    vector<pii> choosenNodes;
    for (int operations = 1; operations <= k; operations++) {
        int u;
        int v;
        int curSum = solve(u, v, operations);
        choosenNodes.push_back(pii(u, v));
        if (curSum > maxi) {
            optimalOperation = operations;
            maxi = curSum;
        }
    }
    cout << optimalOperation << endl;
    for (int i = 0; i < optimalOperation; i++) {
        cout << choosenNodes[i].first << " " << choosenNodes[i].second << endl;
    }
}

[Hackerearth] StickerMix

 

Problem Link    : StickerMix
Category        : Math, Derangement
Contest         : December Circuits'23
#include "bits/stdc++.h"
using namespace std;
#define endl    '\n'
 
template <const int32_t MOD> struct modint {
    int32_t value;
    modint() = default;
    modint(int32_t value_) : value(value_) {}
    inline modint<MOD> operator + (modint<MOD> other) const { int32_t c = this->value + other.value; return modint<MOD>(c >= MOD ? c - MOD : c); }
    inline modint<MOD> operator - (modint<MOD> other) const { int32_t c = this->value - other.value; return modint<MOD>(c <    0 ? c + MOD : c); }
    inline modint<MOD> operator * (modint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return modint<MOD>(c < 0 ? c + MOD : c); }
    inline modint<MOD> & operator += (modint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline modint<MOD> & operator -= (modint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; }
    inline modint<MOD> & operator *= (modint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
    inline modint<MOD> operator - () const { return modint<MOD>(this->value ? MOD - this->value : 0); }
    modint<MOD> pow(uint64_t k) const { modint<MOD> x = *this, y = 1; for (; k; k >>= 1) { if (k & 1) y *= x; x *= x; } return y; }
    modint<MOD> inv() const { return pow(MOD - 2); }  // MOD must be a prime
    inline modint<MOD> operator /  (modint<MOD> other) const { return *this *  other.inv(); }
    inline modint<MOD> operator /= (modint<MOD> other)       { return *this *= other.inv(); }
    inline bool operator == (modint<MOD> other) const { return value == other.value; }
    inline bool operator != (modint<MOD> other) const { return value != other.value; }
    inline bool operator < (modint<MOD> other) const { return value < other.value; }
    inline bool operator > (modint<MOD> other) const { return value > other.value; }
};
template <int32_t MOD> modint<MOD> operator * (int64_t value, modint<MOD> n) { return modint<MOD>(value) * n; }
template <int32_t MOD> modint<MOD> operator * (int32_t value, modint<MOD> n) { return modint<MOD>(value % MOD) * n; }
template <int32_t MOD> istream & operator >> (istream & in, modint<MOD> &n) { return in >> n.value; }
template <int32_t MOD> ostream & operator << (ostream & out, modint<MOD> n) { return out << n.value; }
 
const int mod = 1e9 + 7;
using mint = modint<mod>;
 
const int maxn = 1e6 + 5;
 
mint fact[maxn];
mint derangements[maxn];
 
mint nCr(int n, int r) {
    return fact[n] / (fact[r] * fact[n - r]);
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
 
    fact[0] = 1;
    for (int i = 1; i < maxn; i++) {
        fact[i] = i * fact[i - 1];
        derangements[i] = i <= 2 ? i - 1 : (i - 1) * (derangements[i - 1] + derangements[i - 2]);
    }
 
    int tc;
    cin >> tc;
    for (int tcase = 1; tcase <= tc; tcase++) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            mint probability = nCr(n, i) * derangements[n - i] / fact[n];
            cout << probability << " ";
        }
        mint probability = mint(1) / fact[n];
        cout << probability << endl;
    }
}

[Hackerearth] EvenOddity

 

Problem Link    : EvenOddity
Category        : Data Structure, Counting, Bits, Centroid Decomposition
Contest         : December Circuits'23  
Tutorial        : Counting Subarrays 
#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
const int maxn = 1e6 + 5;
 
struct Node {
    int v, w;
    Node(int v = 0, int w = 0)
        : v(v), w(w) {}
};
 
int n;
vector<vector<Node>> graph;
vector<int> subtree;
vector<bool> isCentroid;
int curTreeNodes;
 
void calcSubtreeSize(int u, int p) {
    subtree[u] = 1;
    for (Node it : graph[u]) {
        int v = it.v;
        if (v == p or isCentroid[v]) {
            continue;
        }
        calcSubtreeSize(v, u);
        subtree[u] += subtree[v];
    }
}
 
int getCentroid(int u, int p) {
    for (Node it : graph[u]) {
        int v = it.v;
        if (v == p or isCentroid[v]) {
            continue;
        }
        if (subtree[v] > curTreeNodes / 2) {
            return getCentroid(v, u);
        }
    }
    return u;
}
 
int cache[2][2]; // cache[ distance % 2 ][ parity(xorSum) % 2 ]
int curCache[2][2];
int ans;
 
void add(int u, int p, int lvl, int xorSum) {
    int bit = lvl & 1;
    int parity = __builtin_popcount(xorSum) & 1;
    int curAns = cache[bit ^ 1][parity];
    ans += curAns;
    curCache[bit][parity]++;
    for (Node it : graph[u]) {
        int v = it.v;
        int w = it.w;
        if (v == p or isCentroid[v]) {
            continue;
        }
        add(v, u, lvl + 1, xorSum ^ w);
    }
}
 
void decompose(int u) {
    calcSubtreeSize(u, 0);
    curTreeNodes = subtree[u];
    int centroid = getCentroid(u, 0);
    isCentroid[centroid] = true;
    memset(cache, 0, sizeof cache);
    cache[0][0] = 1;
    for (Node it : graph[centroid]) {
        int child = it.v;
        if (isCentroid[child]) {
            continue;
        }
        memset(curCache, 0, sizeof curCache);
        add(child, centroid, 1, it.w);
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                cache[i][j] += curCache[i][j];
            }
        }
    }
    for (Node it : graph[centroid]) {
        int child = it.v;
        if (isCentroid[child]) {
            continue;
        }
        decompose(child);
    }
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("f2.txt", "r")) {
        freopen("f2.txt", "r", stdin);
        freopen("f2out.txt", "w", stdout);
    }
 
    int tc;
    cin >> tc;
    for (int tcase = 1; tcase <= tc; tcase++) {
        cin >> n;
        graph.clear(); graph.resize(n + 1);
        subtree.clear(); subtree.resize(n + 1);
        isCentroid.clear(); isCentroid.resize(n + 1);
        for (int e = 1; e < n; e++) {
            int u, v, w;
            cin >> u >> v >> w;
            graph[u].emplace_back(v, w);
            graph[v].emplace_back(u, w);
        }
        ans = 0;
        decompose(1);
        cout << ans * 2 << endl;
    }
}