mr_dot_convict

tourists-Kattis-mr.convict

Oct 2nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows * * * * * * */
  9. #include      <bits/stdc++.h>
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC   optimize ("Ofast")
  17. #pragma GCC   optimize ("unroll-loops")
  18. #pragma GCC   target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS   ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC  cout.precision (10); cout << fixed
  23. #define x     first
  24. #define y     second
  25. #define bg(x) " [ " << #x << " : " << (x) << " ] "
  26. #define un(x) sort(x.begin(), x.end()), \
  27.    x.erase(unique(x.begin(), x.end()), x.end())
  28. using   ll  = long long;
  29. using   ull = unsigned long long;
  30. using   ff  = long double;
  31. using   pii = pair<int,int>;
  32. using   pil = pair<int,ll>;
  33. typedef tree
  34. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  35. ordered_set;
  36.  
  37. struct chash {
  38.    int operator () (pii x) const { return x.x*31 + x.y; }
  39. };
  40. gp_hash_table <pii, int, chash> mp;
  41.  
  42. #if __cplusplus > 201103L
  43. seed_seq seq{
  44.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  45.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  46.       (uint64_t) __builtin_ia32_rdtsc(),
  47.       (uint64_t) (uintptr_t) make_unique<char>().get()
  48. };
  49. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  50. #else
  51. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  52. mt19937 rng(seed);
  53. #endif
  54.  
  55. #define debug(args...) { \
  56.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  57.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  58.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  59.    stringstream _ss(_s); \
  60.    istream_iterator<string> _it(_ss); err(_it, args); \
  61. }
  62. void err(istream_iterator<string> it) {
  63.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  64. }
  65. template<typename T, typename... Args>
  66. void err(istream_iterator<string> it, T a, Args... args) {
  67.    cerr << fixed << setprecision(15)
  68.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  69.    err(++it, args...);
  70. }
  71. /*****************************************************************************/
  72. //Don’t practice until you get it right. Practice until you can’t get it wrong
  73.  
  74. signed main() {
  75.    IOS; PREC;
  76.  
  77.    int n;
  78.    cin >> n;
  79.    const int D = 18;
  80.    vector <vector <int>> T(n, vector <int>());
  81.    vector <vector <int>> table(n, vector <int>(D + 1, -1));
  82.    vector <int> depth(n, 0);
  83.  
  84.    for (int e = 0; e < n - 1; ++e) {
  85.       int u, v;
  86.       cin >> u >> v;
  87.       --u, --v;
  88.       T[u].push_back(v);
  89.       T[v].push_back(u);
  90.    }
  91.  
  92.    function <void(int, int, int)> dfs =
  93.       [&] (int u, int pr, int d) -> void {
  94.          depth[u] = d;
  95.          for (int v : T[u]) {
  96.             if (v == pr) continue;
  97.             table[v][0] = u;
  98.             dfs(v, u, d + 1);
  99.          }
  100.       };
  101.  
  102.  
  103.    dfs (0, -1, 0);
  104.  
  105.    for (int k = 1; k <= D; ++k) {
  106.       for (int i = 1; i < n; ++i)  {
  107.          if (table[i][k - 1] != -1)
  108.             table[i][k] = table[table[i][k - 1]][k - 1];
  109.       }
  110.    }
  111.  
  112.    auto walk = [&] (int u, int k) -> int {
  113.       for (int d = 0; d <= D && u != -1; ++d) {
  114.          if ((1 << d) & k)  {
  115.             u = table[u][d];
  116.          }
  117.       }
  118.       return u;
  119.    };
  120.  
  121.    auto lca = [&] (int u, int v) {
  122.       if (depth[u] > depth[v]) {
  123.          u = walk(u, depth[u] - depth[v]);
  124.       }
  125.       if (depth[v] > depth[u]) {
  126.          v = walk(v, depth[v] - depth[u]);
  127.       }
  128.  
  129.       if (u == v) return u;
  130.  
  131.       for (int d = D; d >= 0; --d){
  132.          if (table[u][d] != table[v][d]) {
  133.             u = table[u][d];
  134.             v = table[v][d];
  135.          }
  136.       }
  137.       return table[u][0]; // jump once more
  138.    };
  139.  
  140.    ll ans = 0;
  141.  
  142.    for (int x = 1; x <= n; ++x) { // NlogN
  143.       for (int y = 2 * x; y <= n; y += x) {
  144.          int z = lca(x - 1, y - 1) + 1;
  145.          ans += depth[x - 1] + depth[y - 1] - 2 * depth[z - 1] + 1;
  146.       }
  147.    }
  148.  
  149.    cout << ans << '\n';
  150.  
  151.    return EXIT_SUCCESS;
  152. }
Add Comment
Please, Sign In to add comment