题意是求树中所有直链的gcd的总和。
做法是dfs把前缀的所有信息传下去,暴力求值。
为什么可以暴力呢?x = gcd(x,a);x会怎么变呢?要么不变,x,a的最大公因数就是x,要么x变为x的一个因子,也就是说最大是x/2,不出log次计算,x就会变成1
这也就是说维护每个点到根的后缀gcd,最多只有log个不同的数,用链表存起来就可以。
#include <iostream>
#include <queue>
#include <cstring>
#include <list>
#include <algorithm>
using namespace std;
#define MAXX 201000
typedef long long LL;
struct E {
int to,next;
}g[MAXX];
int front[MAXX],tot;
void Add(int from,int to) {
g[++tot].to = to;
g[tot].next = front[from];
front[from] = tot;
}
LL gcd(LL a,LL b) {
return b==0 ? a:gcd(b,a%b);
}
LL v[MAXX],ans;
const LL Mod = 1e9+7;
typedef list<pair<LL,int> > List;
#define val first
#define num second
void dfs(int t,int fa,List st) {
/* if(!st.empty()) {
List::reverse_iterator it = st.rbegin()