题解:CF1778F MooseRiven
题意:有一棵点权为
提示:
- 所有公共因数都用得到吗?
- 操作顺序?
- 想个
\Omicron(nV) 状态数的 dp; - 把状态数压到
\Omicron(nd(V)) 。
公共因数换成
设
| 步骤 \ 值 | |||
|---|---|---|---|
| 先 |
|||
| 后 |
|||
| 先 |
|||
| 后 |
下文将
-
-
\frac T2\le S<T:\Leftrightarrow2T\ge\min(4S,S+T)\Leftarrow2T\ge T+S\ge\min(4S,S+T) -
证毕。操作顺序是一个拓扑序的子序列的时候能得到答案。由于不重合的子树之间操作顺序不影响,所以拓扑序换成更严格的 dfs 序也是成立的。接下来就是树形 dp 了。设
对于非叶节点:
对于所有节点:
最后答案:
状态数看似是
*Mertens' 第三定理:后来 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;
}
; ;;;;;;;; ;
; ; ; ;
; ; ; ;
; ; ; ;
; ; ;;;;;;;;;
; ; ; ;
; ; ; ;
;;;;;;;;;;; ;;;;;;;; ; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;; ;; ;
; ;; ;; ;