105002024-04-03 15:52:06111A Day in Olbiacpp17Runtime error 32/1008ms5116 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;
	if(N>1000)exit(1);
	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
1Accepted3ms1832 KiB
2Accepted3ms2048 KiB
subtask216/16
3Accepted3ms2464 KiB
4Accepted3ms2632 KiB
5Accepted3ms2588 KiB
6Accepted3ms3044 KiB
subtask316/16
7Accepted8ms4428 KiB
8Accepted8ms4036 KiB
9Accepted6ms4528 KiB
10Accepted6ms4496 KiB
11Accepted7ms4720 KiB
12Accepted7ms4880 KiB
13Accepted7ms4892 KiB
14Accepted6ms5116 KiB
15Accepted6ms5080 KiB
subtask40/16
16Runtime error3ms3628 KiB
17Runtime error3ms3660 KiB
subtask50/52
18Runtime error3ms3868 KiB
19Runtime error3ms3984 KiB
20Runtime error3ms4084 KiB
21Runtime error3ms4068 KiB
22Runtime error3ms4296 KiB
23Runtime error2ms4376 KiB
24Runtime error2ms4352 KiB
25Runtime error2ms4360 KiB
26Runtime error2ms4356 KiB
27Runtime error3ms4456 KiB
28Runtime error3ms4464 KiB
29Runtime error3ms4468 KiB
30Runtime error3ms4564 KiB
31Runtime error3ms4784 KiB