|
| 1 | +#include <cstring> |
| 2 | +#include <map> |
| 3 | +#include <deque> |
| 4 | +#include <queue> |
| 5 | +#include <stack> |
| 6 | +#include <sstream> |
| 7 | +#include <numeric> |
| 8 | +#include <iostream> |
| 9 | +#include <iomanip> |
| 10 | +#include <cstdio> |
| 11 | +#include <cmath> |
| 12 | +#include <fstream> |
| 13 | +#include <string> |
| 14 | +#include <cstdlib> |
| 15 | +#include <ctime> |
| 16 | +#include <functional> |
| 17 | +#include <algorithm> |
| 18 | +#include <vector> |
| 19 | +#include <set> |
| 20 | +#include <complex> |
| 21 | +#include <list> |
| 22 | +#include <climits> |
| 23 | +#include <cctype> |
| 24 | +#include <bitset> |
| 25 | +#include <assert.h> |
| 26 | +using namespace std; |
| 27 | + |
| 28 | +#define PI 3.14159265359 |
| 29 | +#define all(v) v.begin(),v.end() |
| 30 | +#define sortva(v) sort(all(v)) |
| 31 | +#define sortvd(v) sort(v.rbegin(),v.rend()) |
| 32 | +#define sortaa(first,n) sort(first,first+n) |
| 33 | +#define sortad(first,n) sort(first,first+n),reverse(first,first+n) |
| 34 | +#define sfi1(v) scanf("%dist",&v) |
| 35 | +#define sfi2(v1,v2) scanf("%dist %dist",&v1,&v2) |
| 36 | +#define sfi3(v1,v2,v3) scanf("%dist %dist %dist",&v1,&v2,&v3) |
| 37 | +#define sfll1(v) scanf("%I64d",&v); |
| 38 | +#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2) |
| 39 | +#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3) |
| 40 | +#define sfstr(v) scanf("%sum", v); |
| 41 | +#define sz(v) (int)v.size() |
| 42 | +#define ndl puts("") |
| 43 | +#define flush fflush(stdout) |
| 44 | +#define SS stringstream |
| 45 | +typedef long long ll; |
| 46 | +typedef unsigned long long ull; |
| 47 | +typedef long double ld; |
| 48 | + |
| 49 | +int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; |
| 50 | +int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 }; |
| 51 | + |
| 52 | +ll gcd(ll first, ll second) { return !second ? first : gcd(second, first % second); } |
| 53 | +ll lcm(ll first, ll second) { return (first / gcd(first, second)) * second; } |
| 54 | + |
| 55 | +void PLAY() { |
| 56 | +cout << fixed << setprecision(15); |
| 57 | +ios::sync_with_stdio(0); |
| 58 | +cin.tie(0); |
| 59 | +cout.tie(0); |
| 60 | +} |
| 61 | + |
| 62 | +const int MAX = 2e5 + 10; |
| 63 | +const int MAXLOG = 20; |
| 64 | + |
| 65 | +vector<int> adj[MAX]; |
| 66 | +int table[MAX][MAXLOG], depth[MAX]; |
| 67 | + |
| 68 | +void dfs(int u, int p) { |
| 69 | +table[u][0] = p; |
| 70 | +for (int i = 0; i < sz(adj[u]); i++) { |
| 71 | +int v = adj[u][i]; |
| 72 | +if (v == p) continue; |
| 73 | +depth[v] = depth[u] + 1; |
| 74 | +dfs(v, u); |
| 75 | +} |
| 76 | +} |
| 77 | + |
| 78 | +int LCA(int u, int v) { |
| 79 | +if (depth[u] < depth[v]) |
| 80 | +swap(u, v); |
| 81 | +for (int k = MAXLOG - 1; k >= 0; k--) |
| 82 | +if (depth[u] - (1 << k) >= depth[v]) |
| 83 | +u = table[u][k]; |
| 84 | +if (u == v) return u; |
| 85 | +for (int k = MAXLOG- 1; k >= 0; k--) { |
| 86 | +if (table[u][k] != table[v][k]) { |
| 87 | +u = table[u][k]; |
| 88 | +v = table[v][k]; |
| 89 | +} |
| 90 | +} |
| 91 | +return table[u][0]; |
| 92 | +} |
| 93 | + |
| 94 | +int length(int u, int v) { |
| 95 | +return depth[u] + depth[v] - 2 * depth[LCA(u, v)]; |
| 96 | +} |
| 97 | + |
| 98 | +int main() { |
| 99 | +PLAY(); |
| 100 | + |
| 101 | +int n; |
| 102 | +cin >> n; |
| 103 | +for (int i = 1; i < n; i++) { |
| 104 | +int u, v; |
| 105 | +cin >> u >> v; |
| 106 | +u--; v--; |
| 107 | +adj[u].push_back(v); |
| 108 | +adj[v].push_back(u); |
| 109 | +} |
| 110 | + |
| 111 | +memset(table, -1, sizeof table); |
| 112 | +dfs(0, -1); |
| 113 | + |
| 114 | +for (int k = 1; k < MAXLOG; k++) { |
| 115 | +for (int u = 0; u < n; u++) { |
| 116 | +if (table[u][k - 1] == -1) continue; |
| 117 | +table[u][k] = table[table[u][k - 1]][k - 1]; |
| 118 | +} |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +ll res = 0; |
| 123 | +for (int i = 1; i <= n; i++) |
| 124 | +for (int j = 2 * i; j <= n; j += i) |
| 125 | +res += length(i - 1, j - 1) + 1; |
| 126 | + |
| 127 | +cout << res << endl; |
| 128 | + |
| 129 | +return 0; |
| 130 | +} |
0 commit comments