105012024-04-03 15:52:19111A Day in Olbiacpp17Accepted 100/1002.128s229068 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<vector<int>>g(N);
	for(int i=0;i<N-1;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int ans=0;
	auto dfs=[&](auto self,int i,int p,int d)->map<pair<int,int>,int>{
		map<pair<int,int>,int>s;
		for(int m=1;i*m<=N&&m<=N;m++){
			s[{m,i*m-d}]++;
		}
		for(int j:g[i]){
			if(j==p){
				continue;
			}
			auto z=self(self,j,i,d+1);
			if(s.size()<z.size()){
				swap(s,z);
			}
			for(auto[mx,y]:z){
				auto[m,x]=mx;
				auto t=s.find({m,-x-d*2});
				if(t!=s.end()){
					ans+=t->second*y;
				}
			}
			for(auto[mx,y]:z){
				s[mx]+=y;
			}
		}
		return s;
	};
	dfs(dfs,0,-1,0);
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2056 KiB
subtask216/16
3Accepted3ms2464 KiB
4Accepted3ms2740 KiB
5Accepted3ms2920 KiB
6Accepted3ms3148 KiB
subtask316/16
7Accepted8ms4292 KiB
8Accepted8ms4072 KiB
9Accepted6ms4820 KiB
10Accepted6ms4640 KiB
11Accepted6ms4776 KiB
12Accepted6ms4868 KiB
13Accepted7ms5112 KiB
14Accepted6ms5056 KiB
15Accepted6ms5392 KiB
subtask416/16
16Accepted1.725s183272 KiB
17Accepted1.764s181260 KiB
subtask552/52
18Accepted2.115s185500 KiB
19Accepted2.128s201352 KiB
20Accepted1.223s223016 KiB
21Accepted1.213s208328 KiB
22Accepted1.217s214644 KiB
23Accepted1.195s210484 KiB
24Accepted1.192s218408 KiB
25Accepted1.251s229068 KiB
26Accepted1.231s226204 KiB
27Accepted1.052s224148 KiB
28Accepted982ms223592 KiB
29Accepted931ms223912 KiB
30Accepted1.078s225024 KiB
31Accepted1.088s227820 KiB