题目大意是求

\(\sum_{v,fa,lca(v,fa)=fa}gcd(v \to fa)\)

容易发现 \(\gcd\) 只会变小,所以根据这玩意是从上到下的,每次暴力一下就可以了,\(\gcd\)数量不会超过\(\log\),所以复杂度大概是 \(n \log n\)

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
sort(all(v));
v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
~FILEIN() {}
char qwq[SZ], *S = qwq, *T = qwq, ch;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
FILEIN& operator>>(string& s) {
while (isspace(ch = GETC()))
;
s = ch;
while (!isspace(ch = GETC())) s += ch;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 1;
while ((ch = GETC()) < 0x30)
if (ch == 0x2d) sign = 0;
x = (ch ^ 0x30);
while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
x = sign ? x : -x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
FILEIN& operator>>(signed& x) { return read(x), *this; }
FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 0x114514;
char quq[SZ], ST[0x114];
signed sz, O;
~FILEOUT() { flush(); }
void flush() {
fwrite(quq, 1, O, stdout);
fflush(stdout);
O = 0;
}
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
FILEOUT& operator<<(string str) {
if (O > LIMIT) flush();
for (char c : str) quq[O++] = c;
return *this;
}
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 0x2d;
x = -x;
}
do {
ST[++sz] = x % 0xa ^ 0x30;
x /= 0xa;
} while (x);
while (sz) quq[O++] = ST[sz--];
return;
}
FILEOUT& operator<<(int x) { return write(x), *this; }
FILEOUT& operator<<(signed x) { return write(x), *this; }
FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out; int n, ans;
const int maxn = 1e5 + 10;
vector<int> g[maxn];
int val[maxn];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
const int mod = 1e9 + 7;
void dfs(vector<pii> vc, int u, int p) {
vector<pii> nvc;
nvc.pb(val[u], 1ll);
ans = (ans + val[u]) % mod;
for (pii x : vc) {
int y = x.first;
int c = x.second;
int ny = gcd(y, val[u]);
ans = (ans + ny * c) % mod;
if (ny == nvc.back().first)
nvc.back().second += c;
else
nvc.pb(ny, c);
}
nvc.swap(vc);
for (int v : g[u])
if (v != p) dfs(vc, v, u);
}
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#else
ios_base ::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
#endif
// code begin.
in >> n;
for (int i = 0; i < n; i++) {
in >> val[i];
}
for (int i = 1; i < n; i++) {
int u, v;
in >> u >> v;
--v, --u;
g[u].pb(v);
g[v].pb(u);
}
dfs({}, 0, -1);
out << ans << '\n';
return 0;
// code end.
}

最新文章

  1. Python初识(一)
  2. Object.ReferenceEquals
  3. java算法小知识练习(二)
  4. Java简单算法--出圈问题
  5. 转载:在Visual Studio 2013中管理中国特色的社会主义Windows Azure
  6. Http Analyzer 数据抓包
  7. AOP 切面编程
  8. Linux java进程无故被kill
  9. spring mvc 500错误Allocate exception for servlet AppService javax.naming.NamingException: Cannot create resource instance 竟是@Resource的原因
  10. C语言数组一种巧妙的使用方式
  11. 生产者消费者模型java
  12. SLF4J bindings
  13. poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵 (矩阵快速幂)
  14. C、C++打包成.dll .so .a 给Unity使用
  15. 【xsy2506】 bipartite 并查集+线段树
  16. 洛咕 P3702 [SDOI2017]序列计数
  17. python-day8-列表的内置方法
  18. NHibernate的调试技巧和Log4Net配置
  19. 通过jdbc使用PreparedStatement,提升性能,防止sql注入
  20. (2.16)Mysql之SQL基础——函数

热门文章

  1. 小米重新上锁[BL]
  2. Linux如何运行和停止jar包
  3. python中map()和dict()的用法
  4. 使用nginx构建一个具备缓存功能的反向代理服务器
  5. Dubbox 环境搭建-新(Windows下)
  6. 让Android模拟器速度飞起来_Eclipse+BlueStacks调试Android应用【2012-10-30】
  7. 【TensorFlow】TensorFlow基础 —— 模型的保存读取与可视化方法总结
  8. Jmeter之存储测试结果
  9. 动手学习pytorch——(1)线性回归
  10. Java Stack使用