# Codeforces Round #614 选讲

http://codeforces.com/contest/1292/problem/C

 1 #include <bits/stdc++.h>
2 using namespace std;
3 #define LL long long
4
5 int n;
6 #define maxn 3011
7 struct Edge{int to,next;}edge[maxn<<1]; int first[maxn],le=2;
8 void in(int x,int y) {Edge &e=edge[le]; e.to=y; e.next=first[x]; first[x]=le++;}
9 void insert(int x,int y) {in(x,y); in(y,x);}
10
11 int sz[maxn][maxn],fa[maxn][maxn];
12 LL dp[maxn][maxn];
13 struct NODE{int x,y;};
14 vector<NODE> vec[maxn];
15 void dfs(int x,int f,int top,int dep)
16 {
17     sz[top][x]=1; fa[top][x]=f;
18     if (dep>0) vec[dep].push_back((NODE){top,x});
19     for (int i=first[x];i;i=edge[i].next)
20     {
21         Edge &e=edge[i]; if (e.to==f) continue;
22         dfs(e.to,x,top,dep+1); sz[top][x]+=sz[top][e.to];
23     }
24 }
25
26 int main()
27 {
28     scanf("%d",&n);
29     for (int i=1,x,y;i<n;i++) {scanf("%d%d",&x,&y); insert(x,y);}
30     for (int i=1;i<=n;i++) dfs(i,0,i,0);
31
32     for (int len=1;len<n;len++)
33         for (int i=0,to=vec[len].size();i<to;i++)
34         {
35             int a=vec[len][i].x,b=vec[len][i].y;
36             dp[a][b]=dp[b][a]=sz[a][b]*sz[b][a]+max(dp[a][fa[a][b]],dp[b][fa[b][a]]);
37         }
38
39     LL ans=0;
40     for (int i=1;i<=n;i++)
41         for (int j=1;j<=n;j++)
42             ans=max(ans,dp[i][j]);
43     printf("%lld\n",ans);
44     return 0;
45 }
View Code

 1 #include <bits/stdc++.h>
2 using namespace std;
3 #define LL long long
4
5 int n;
6 #define maxn 5011
7 #define MAXNUM 5000
8 int cnt[maxn],fra[maxn][maxn],poi[maxn],cntpri[maxn],pri[maxn],lp; bool vis[maxn];
9 void makeprime(int n)
10 {
11     for (int i=2;i<=n;i++)
12     {
13         if (!vis[i]) pri[++lp]=i;
14         for (int j=1;j<=lp && 1ll*i*pri[j]<=n;j++)
15         {
16             vis[i*pri[j]]=1;
17             if (i%pri[j]==0) break;
18         }
19     }
20 }
21
22 int main()
23 {
24     scanf("%d",&n);
25     for (int i=1,x;i<=n;i++) scanf("%d",&x),cnt[x]++;
26     makeprime(MAXNUM);
27
28     LL ans=0;
29     for (int x=0;x<=MAXNUM;x++)
30         for (int i=1;i<=lp && pri[i]<=x;i++)
31         {
32             for (int j=pri[i];j<=x;j*=pri[i])
33                 fra[x][i]+=x/j;
34             ans+=1ll*cnt[x]*fra[x][i];
35         }
36         //trick: The number of prime 2 in k! is k/2+k/4+k/8+...
37
38     for (int x=0;x<=MAXNUM;x++) poi[x]=lp;
39     while (1)
40     {
41         for (int i=1;i<=lp;i++) cntpri[i]=0;
42         for (int x=0;x<=MAXNUM;x++)
43         {
44             while (poi[x]>0 && fra[x][poi[x]]==0) poi[x]--;
45             if (poi[x]>0) cntpri[poi[x]]+=cnt[x];
46         }
47         bool findans=1;
48         for (int i=1;i<=lp;i++) if (cntpri[i]*2>n)
49         {
50             findans=0;
51             ans-=cntpri[i]-(n-cntpri[i]);
52             for (int x=0;x<=MAXNUM;x++) if (poi[x]>0 && poi[x]==i) fra[x][poi[x]]--; else poi[x]=0;
53             break;
54         }
55         if (findans) break;
56     }
57     printf("%lld\n",ans);
58     return 0;
59 }
View Code

posted @ 2020-01-30 11:01  Blue233333  阅读(155)  评论(0编辑  收藏  举报