题意:有一棵点权为 ,值域为 的树,对一个节点进行操作可以将一个点的子树内所有数乘上他们的一个公共因数,问对 个不同点进行操作后 最大值。
提示:
- 所有公共因数都用得到吗?
- 操作顺序?
- 想个 状态数的 dp;
- 把状态数压到 。
公共因数换成 显然不劣。定义「有向树」为有向图 ,即上向下连边做成的有向图。我们来证明,把操作顺序变成一个有向树拓扑排序的子序列,总是不劣的,原来的公共因数不会消失,只会增加。该命题等价于:「给一个序列 和一个位置集合 。对一个集合 操作定义为:,然后 。则:先操作 再操作 得到 ,先操作 再操作 得到 ,有 」( 对应树上一个上面的操作点和下面的操作点)。
设 ,则原命题等价于 。令 ,那么命题又被我们转化为:「先 后 得到的 」不小于「先 后 得到的 」。接下来考察 怎么变化。
| 步骤 \ 值 | |||
|---|---|---|---|
| 先 | |||
| 后 | |||
| 先 | |||
| 后 |
下文将 分别简写为 ,问就是懒。命题进一步被转化为:证明两个非负整数 一定满足 。分类讨论:
- ;
- 。
证毕。操作顺序是一个拓扑序的子序列的时候能得到答案。由于不重合的子树之间操作顺序不影响,所以拓扑序换成更严格的 dfs 序也是成立的。接下来就是树形 dp 了。设 表示 子树中,选 / 没选 达成整个子树都是 的倍数的最小步数。对于叶节点:
对于非叶节点:
对于所有节点:
最后答案:
状态数看似是 的,其实不然。只有第二维 满足 的才会用到,状态数 ,总复杂度 。接下来证明 ,我们考虑一个更强的结论:。记 ,则:
*Mertens’ 第三定理:,两边同时取倒数得 ,也即 。其中 是 Euler-Mascheroni 常数,定义为 ,值约为 。该常数由 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;
}
; ;;;;;;;; ;
; ; ; ;
; ; ; ;
; ; ; ;
; ; ;;;;;;;;;
; ; ; ;
; ; ; ;
;;;;;;;;;;; ;;;;;;;; ; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;; ;; ;
; ;; ;; ;