ICPC南昌邀请赛 Interesting Trip

链接

点击跳转

题解

这题我终究还是被卡常了

但我觉得过没过不重要,学到方法就行了

这题的做法是,先把所有是 2 2 2的倍数的点拿出来,统计有多少条链的长度恰好为 D D D

然后再加上 3 3 3

然后加上 5 5 5

然后减去 6 6 6

概括就是 ∑ μ ( i ) a n s ( i ) \sum \mu(i) ans(i) μ(i)ans(i)

统计链的个数用长链剖分

注意,假设我现在枚举的约数是 n o w now now,那么长链剖分是要统计那些所有点都是 n o w now now的倍数的链,这条链只要含有一个非 n o w now now倍数的点,就不能算进来

所以经过那些非倍数的点的链都不能算进来,也就是说这些非倍数的点把整棵树划分成若干个不连通的块,我只需要对每个块单独统计就行

所以我其实每次只遍历了那些权值为 n o w now now的倍数的点

所以总的复杂度就是每个点权值的约数个数之和

网上大佬的代码能通过 10 10 10个测试点,但我的只能通过 7 7 7个…我太菜了

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 500010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int read(int x=0)
{
    int c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
int now, vis[maxn], pool[maxn], *f[maxn], tot, a[maxn], D, n;
ll cnt, ans;
vector<int> lis[maxn];
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(_,__) for(auto p=__.head[_];p;p=__.next[p])
}G;
struct EasyMath
{
    int prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    void shai(int N)
    {
        int i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
}em;
struct Longest_Chain_Decomposition
{
    int len[maxn], son[maxn], depth[maxn], istop[maxn];
    void dfs(int u, int fa)
    {
        son[u]=0;
        len[u]=1;
        depth[u]=depth[fa]+1;
        istop[u]=false;
        forp(u,G)
        {
            int v(G.to[p]); if(v==fa or a[v]%now)continue;
            dfs(v,u);
            if(len[v]+1>len[u])len[u]=len[v]+1, son[u]=v;
        }
        forp(u,G)
        {
            int v(G.to[p]); if(v==fa)continue;
            if(v!=son[u])istop[v]=true;
        }
    }
    void run(int root)
    {
        tot=0;
        depth[0]=0, dfs(root,0);
        istop[root]=true;
    }
}lcd;
void dfs(int u, int fa)
{
    vis[u]=now;
    int i;
    if(lcd.istop[u])
    {
        f[u] = pool + tot;
        tot += lcd.len[u];
    }
    f[u][0]=1;
    if(lcd.son[u])
    {
        int v(lcd.son[u]);
        f[v] = f[u] + 1;
        dfs(v,u);
    }
    if(D<lcd.len[u])cnt += f[u][D];
    forp(u,G)
    {
        int v(G.to[p]);
        if(v==fa or v==lcd.son[u] or a[v]%now)continue;
        dfs(v,u);
        rep(i,0,lcd.len[v]-1)if(D-i-1<lcd.len[u])cnt +=  ll(f[v][i]) * f[u][D-i-1];
        rep(i,0,lcd.len[v]-1)f[u][i+1]+=f[v][i];
    }
}
vector<int> fac[maxn];
int main()
{
    int T=read(), i, j, M=3e4, kase;
    em.shai(M);
    rep(i,1,M)
    {
        for(j=1;j*j<=i;j++)
            if(i%j==0)
            {
                fac[i].emb(j);
                if(i/j!=j)fac[i].emb(i/j);
            }
    }
    rep(kase,1,T)
    {
        n = read(), D = read();
        rep(i,1,n)a[i]=read();
        G.clear(n);
        rep(i,1,n-1)
        {
            int u = read(), v = read();
            G.adde(u,v), G.adde(v,u);
        }
        rep(i,1,M)lis[i].clear();
        rep(i,1,n)for(auto d:fac[a[i]])lis[d].emb(i);
        ans=0;
        rep(now,2,M)
        {
            if(em.mu[now]==0)continue;
            cnt=0;
            for(auto x:lis[now])
            {
                if(vis[x]==now)continue;
                tot=0;
                lcd.run(x);
                dfs(x,0);
            }
            ans += em.mu[now] * cnt;
        }
        printf("Case #%d: %lld\n",kase,-ans*2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值