题解:CF1778F MooseRiven

· · 题解

题意:有一棵点权为 \{a_n\},值域为 [1,V=1000] 的树,对一个节点进行操作可以将一个点的子树内所有数乘上他们的一个公共因数,问对 k 个不同点进行操作后 a_1 最大值。

提示:

  1. 所有公共因数都用得到吗?
  2. 操作顺序?
  3. 想个 \Omicron(nV) 状态数的 dp;
  4. 把状态数压到 \Omicron(nd(V))。

公共因数换成 \gcd 显然不劣。定义「有向树」为有向图 ([1,n],\{(p_i,i)\mid i\text{ not root}\},即上向下连边做成的有向图。我们来证明,把操作顺序变成一个有向树拓扑排序的子序列,总是不劣的,原来的公共因数不会消失,只会增加。该命题等价于:「给一个序列 \{b_m\} 和一个位置集合 S。对一个集合 A 操作定义为:g\gets\gcd_{i\in A}b_i,然后 i\in A:b_i\gets gb_i。则:先操作 S 再操作 U 得到 g_S,先操作 U 再操作 S 得到 g_U,有 g_U\mid g_S」(U,S 对应树上一个上面的操作点和下面的操作点)。

设 v_p(x):=\max\{y\mid(p^y\mid x)\},则原命题等价于 \forall \text{ prime }p:v_p(g_U)\le v_p(g_S)。令 T:=\complement_U^S,r_A:=\min_{i\in A}v_p(a_i),那么命题又被我们转化为:「先 S 后 U 得到的 r_U」不小于「先 U 后 S 得到的 r_U」。接下来考察 r_U=\min(r_S,r_T) 怎么变化。

步骤 \ 值 r_S r_T r_U
先 S {r_S}'=2r_S {r_T}'=r_T {r_U}'=\min(2r_S,r_T)
后 U {r_S}''={r_S}'+{r_U}' {r_T}''={r_T}'+{r_U}' {r_U}''=2{r_U}'=2\min(2r_S,r_T)
先 U {r_S}'=r_S+r_U {r_T}'=r_T+r_U {r_U}'=2r_U
后 S {r_S}''=2{r_S}'=2r_S+2r_U {r_T}''={r_T}'=r_T+r_U {r_U}''=\min(2r_S+2r_U,r_T+r_U)

下文将 r_S,r_T 分别简写为 S,T,问就是懒。命题进一步被转化为:证明两个非负整数 S,T 一定满足 2\min(2S,T)\ge\min(2S+2\min(S,T),T+\min(S,T))。分类讨论:

证毕。操作顺序是一个拓扑序的子序列的时候能得到答案。由于不重合的子树之间操作顺序不影响,所以拓扑序换成更严格的 dfs 序也是成立的。接下来就是树形 dp 了。设 \mathrm{dp}_{x,v,0/1} 表示 x 子树中,选 / 没选 x 达成整个子树都是 v 的倍数的最小步数。对于叶节点:

\mathrm{dp}_{x,v,0}= \begin{aligned}\begin{cases} 0,\quad&v\mid a_x\\ +\infty,\quad&\text{otherwise.} \end{cases}\end{aligned}

对于非叶节点:

\mathrm{dp}_{x,v,0}= \begin{aligned}\begin{cases} \sum_{p_i=x}\min\mathrm{dp}_{i,v},\quad&v\mid a_x\\ +\infty,\quad&\text{otherwise.} \end{cases}\end{aligned}

对于所有节点:

\mathrm{dp}_{x,v,1}=\min_{j\mid i,ij=v}\mathrm{dp}_{x,i,0}

最后答案:

\mathrm{ans}=a_1\max_{\mathrm{dp}_{1,v,0}<k}v

状态数看似是 \Omicron(nV) 的,其实不然。只有第二维 v 满足 v\mid a_1 的才会用到,状态数 \Omicron(nd(V)),总复杂度 \Omicron(n\max_{i=1}^V\sum_{j\mid i}d(j))。接下来证明 \Omicron(\max_{i=1}^V\sum_{j\mid i}d(j))\le\Omicron(\sqrt{V\log V}),我们考虑一个更强的结论:\Omicron(\sum_{d\mid V})\sqrt d\le\Omicron(\sqrt{V\log V})。记 V=:\prod_{i=1}^k{p_i}^{\alpha_i},则:

\begin{aligned} \sum_{d|V}d^\frac12=&\prod_{i=1}^k\sum_{j=0}^{\alpha_i}{p_i}^\frac j2\\ =&\prod_{i=1}^k\dfrac{{p_i}^\frac{\alpha_i+1}2-1}{{p_i}^\frac12-1}\\ \le&\prod_{i=1}^k\sqrt\dfrac{{p_i}^{\alpha_i+1}}{({p_i}^\frac12-1)({p_i}^\frac12+1)}\\ =&\sqrt{\prod_{i=1}^k{p_i}^{\alpha_i}\cdot\dfrac{{p_i}}{{p_i}-1}}\\ =&\sqrt{V\cdot\dfrac{{p_i}}{{p_i}-1}}\\ \le&\sqrt{V\prod_{\text{prime }p\le V}\dfrac p{p-1}}\\ \xlongequal{\text{Mertens' Third Theorem*}}&\sqrt{V\log V} \end{aligned}

*Mertens' 第三定理:\lim_{n\rightarrow+\infty}\log n\prod_{\text{prime }p\le n}\frac{p-1}p=e^{-\gamma},两边同时取倒数得 \lim_{n\rightarrow+\infty}\frac{\prod_{\text{prime }p\le n}\frac p{p-1}}{\log n}=e^{\gamma},也即 \Omicron(\prod_{\text{prime }p\le n}\frac p{p-1})=\Omicron(\log n)。其中 \gamma 是 Euler-Mascheroni 常数,定义为 \lim_{n\rightarrow+\infty}\sum_{i=1}^n\frac 1i-\ln n,值约为 0.5772。该常数由 Euler 开始研究,算到了小数后 6 位后来 Mascheroni 将其算到了小数后 32 位,算错了 19 位。

#pragma GCC optimize(1,2,3,"Ofast","inline")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
// using namespace __gnu_pbds;
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>T;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ldb;
//#define umap unordered_map
#define umap __gnu_pbds::cc_hash_table
#define mkp make_pair
#define prque priority_queue
#define emp emplace
#define empb emplace_back
#define empf emplace_front
#define invarg invalid_argument
#define cus_throw(msg) throw invarg(string(msg) + " at " + __FILE__ + ":" + to_string(__LINE__))
random_device rndv;
mt19937 rd(rndv());
const ll inf = 1e6;
const vector<ll> millerrabin = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
const double eps = 1e-8;
inline void enter(){putchar('\n');}
inline void space(){putchar(' ');}
inline ll readll(){ll ret=0,k=1;char c;do{c=getchar();if(c=='-'){k=-1;}}while(('0'>c)|('9'<c));do{ret=(ret<<3)+(ret<<1)+c-48;c=getchar();}while(('0'<=c)&(c<='9'));return ret*k;}
inline void read(ll&x){x=readll();}
inline char readchar(){char ret;do{ret=getchar();}while(ret<=32);return ret;}
inline void read(char&x){x=readchar();}
inline string readstr(){string ret;char c;do{c=getchar();}while(c<=32);do{ret+=c;c=getchar();}while((c>32)&(c!=EOF));return ret;}
inline void read(string&x){x=readstr();}
inline void write(ll x){if(!x){putchar('0');return;}if(x<0){putchar('-');x*=-1;}char op[20]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){putchar(op[cur--]);}}
inline ostream& operator<<(ostream& out, __int128 x){if(!x){out<<"0";return out;}if(x<0){out<<"-";x*=-1;}char op[40]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){out<<op[cur--];}return out;}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

const ll MAXN = 2e5 + 5;

ll n, k, a[MAXN], dp[MAXN][40][2], pos[MAXN];
vector<ll> e[MAXN], frac[MAXN], fr2[MAXN], V;

void setdp(ll x, ll pr) {
    if (e[x].size() == bool(pr)) {
        for (ll i : V) {
            if (a[x] % i == 0) dp[x][pos[i]][0] = 0;
            else dp[x][pos[i]][0] = inf;
        }
    }
    for (ll i : V) dp[x][pos[i]][0] = 0;
    for (ll i : e[x]) {
        if (i == pr) continue;
        setdp(i, x);
        for (ll j : V) dp[x][pos[j]][0] += min(dp[i][pos[j]][0], dp[i][pos[j]][1]);
    }
    for (ll i : V) {
        if (a[x] % i) dp[x][pos[i]][0] = inf;
    }
    for (ll i : V) {
        for (ll j : fr2[i]) {
            dp[x][pos[i]][1] = min(dp[x][pos[i]][1], 1 + dp[x][pos[j]][0]);
        }
    }
}

void init() {
    for (ll i = 1; i <= n; ++i) {
        a[i] = 0;
        e[i].clear();
    }
    V.clear();
}
int mian() {
    read(n), read(k);
    for (ll i = 1; i <= n; ++i) read(a[i]);
    for (ll i = 1; i <= a[1]; ++i) if (a[1] % i == 0) pos[i] = V.size(), V.empb(i);
    for (ll i = 1, u, v; i < n; ++i) read(u), read(v), e[u].empb(v), e[v].empb(u);
    for (ll i = 1; i <= n; ++i) {
        for (ll j = 0; j <= V.size(); ++j) dp[i][j][0] = dp[i][j][1] = inf;
    }
    setdp(1, 0);
    ll ans = a[1];
    for (ll i : V) {
        ans = max(ans, a[1] * (1 + (i - 1) * (k > dp[1][pos[i]][0])));
    }
    write(ans), enter();
    return 0;
}

int main() {
    for (ll i = 1; i <= 1000; ++i) {
        for (ll j = i; j <= 1000; j += i) frac[j].empb(i);
        for (ll j : frac[i]) if (j % (i / j) == 0) fr2[i].empb(j);
    }
    ll T;
    read(T);
    while (T--) {
        init();
        mian();
    }
    return 0;
}

;             ;;;;;;;;          ;
;                   ;          ; ;
;                  ;          ;   ;
;                 ;          ;     ;
;                ;          ;;;;;;;;;
;               ;          ;         ;
;              ;          ;           ;
;;;;;;;;;;;   ;;;;;;;;   ;             ;

   ;                        ;
  ;                          ;
 ;                            ;
;                              ;
;                              ;
;                              ;
 ;                            ;
  ;         ;;          ;;   ;
   ;        ;;          ;;  ;