#include #include #include #include using namespace std; // Graph layout // -- Each problem has its own Edge structure. // If you see "typedef int Edge;" at the top of an algorithm, change // vector > nbr; ---> vector > nbr; typedef int Edge; struct Graph { vector > nbr; int num_nodes; Graph(int n) : nbr(n), num_nodes(n) { } void add_edge(int u, int v) { Edge e1 = v; nbr[u].push_back(e1); Edge e2 = u; nbr[v].push_back(e2); } }; // RMQ: // call constructRMQ to get data structure M O(n) // call getmax to get the maximum from [a..b] inclusive O(log n) // returns a pair of the form (maximum value,index of maximum value) // call update to change a value in the array O(log n) const int MAX_NODES = 200000; const int MAX_N = MAX_NODES*2; // include RMQ code (Minimum) -- MAX_N must be 2*MAX_NODES // call constructLCA once before starting O(n) // call LCA to find lca of vertex u and v O(log n) typedef pair Type; // These two lines replace the const Type oo = make_pair(INT_MAX,-1); // corresponding lines in rmq.cc typedef pair pti; const pti p_oo = make_pair(oo,-1); int size; void constructRMQ(Type A[MAX_N], pti M[4*MAX_N], int n) { size = 1; while(size < n) size <<= 1; for (int i=0; i < size; i++) M[size-1+i] = (i < n ? make_pair(A[i],i) : p_oo); for (int i=size-2; i>=0; i--) M[i] = min(M[2*i+1],M[2*i+2]); } pti getmin(pti M[4*MAX_N], int a, int b, int st=0,int en=size,int ind=0) { if (a > b || a >= en || b < st) return p_oo; if ((a <= st && en <= b) || st+1 >= en) return M[ind]; int mid = (st+en)/2; return min(getmin(M,a,b,st,mid,2*ind+1), getmin(M,a,b,mid,en,2*ind+2)); } void update(Type A[MAX_N], pti M[4*MAX_N], int i, Type val){ A[i] = val; M[i += size-1] = make_pair(val,i); while((i = (i-1)/2)) M[i] = min(M[2*i+1],M[2*i+2]); } void preLCA(const Graph& G,int r,int p,Type A[MAX_N],int loc[MAX_N],int d,int& idx){ for(int i=0;i> n; Graph G(n); for (int i = 0; i < n-1; i++) { int u, v; cin >> u >> v; G.add_edge(u-1, v-1); } constructLCA(G, 0, M, loc); fill(dist, dist+n, -1); comp_dist(G, 0, dist, 0); long long sum = 0; for (int i = 1; i <= n; i++) { for (int j = 2*i; j <= n; j += i) { sum += dist[i-1] + dist[j-1] - 2 * dist[LCA(M, loc, i-1, j-1)] + 1; } } cout << sum << endl; return 0; }