# Regional 2014 - Asia Mudanjiang - F Fiber-optic Network

1N50,1LiRi50000$1\leq N\leq 50, 1 \leq L_i \leq R_i \leq 50000$

f[u][i]=v is the son of ugcd(i,j)=1f[v][j]=v is the son of ujf[v][j]d|id|jμ(d)=v is the son of ud|iμ(d)d|jf[v][j]=v is the son of ud|iμ(d)g[v][d]=v is the son of uh[v][i]
，其中g[v]$g[v]$相当于是f[v]$f[v]$的狄利克雷卷积，h[v]$h[v]$则相当于是v$v$u$u$产生的贡献，这样做是O(NMlogM)$O(NM \log M)$的。

#include <vector>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 51, maxm = 50001, mod = 1000000007;
int t, n, mx, l[maxn], r[maxn], mu[maxm];
int f[maxn][maxm], g[maxn][maxm], h[maxn][maxm];
int pre[maxn][maxm], suf[maxn][maxm];
vector<int> e[maxn];
inline void mod_inc(int &x, int y)
{
if((x += y) >= mod)
x -= mod;
}
inline void mod_dec(int &x, int y)
{
if((x -= y) < 0)
x += mod;
}
void pfs(int u)
{
int sz = e[u].size();
for(int i = 0; i < sz; ++i)
{
int v = e[u][i];
e[v].erase(remove(e[v].begin(), e[v].end(), u), e[v].end());
pfs(v);
}
memset(f[u] + 1, 0, mx * sizeof(int));
if(sz > 0)
{
{ // pre
int p, v = e[u][0];
for(int i = l[u]; i <= r[u]; ++i)
pre[v][i] = 1;
for(int i = 1; i < sz; ++i)
{
p = v, v = e[u][i];
for(int j = l[u]; j <= r[u]; ++j)
pre[v][j] = (LL)pre[p][j] * h[p][j] % mod;
}
}
{ // suf
int p, v = e[u][sz - 1];
for(int i = l[u]; i <= r[u]; ++i)
suf[v][i] = 1;
for(int i = sz - 2; i >= 0; --i)
{
p = v, v = e[u][i];
for(int j = l[u]; j <= r[u]; ++j)
suf[v][j] = (LL)suf[p][j] * h[p][j] % mod;
}
}
for(int i = l[u]; i <= r[u]; ++i)
f[u][i] = (LL)suf[e[u][0]][i] * h[e[u][0]][i] % mod;
}
else
for(int i = l[u]; i <= r[u]; ++i)
f[u][i] = 1;
memcpy(g[u] + 1, f[u] + 1, mx * sizeof(int));
for(int i = 1; i <= mx; ++i)
for(int j = i + i; j <= mx; j += i)
mod_inc(g[u][i], g[u][j]);
memset(h[u] + 1, 0, mx * sizeof(int));
for(int i = 1; i <= mx; ++i)
if(mu[i] > 0)
for(int j = i; j <= mx; j += i)
mod_inc(h[u][j], g[u][i]);
else if(mu[i] < 0)
for(int j = i; j <= mx; j += i)
mod_dec(h[u][j], g[u][i]);
}
void dfs(int u)
{
for(int i = l[u]; i <= r[u]; ++i)
f[u][i] = (LL)f[u][i] * h[u][i] % mod;
for(vector<int>::iterator it = e[u].begin(); it != e[u].end(); ++it)
{
int v = *it;
memset(g[u] + 1, 0, mx * sizeof(int));
for(int i = l[u]; i <= r[u]; ++i)
g[u][i] = (LL)pre[v][i] * suf[v][i] % mod * h[u][i] % mod;
for(int i = 1; i <= mx; ++i)
for(int j = i + i; j <= mx; j += i)
mod_inc(g[u][i], g[u][j]);
memset(h[v] + 1, 0, mx * sizeof(int));
for(int i = 1; i <= mx; ++i)
if(mu[i] > 0)
for(int j = i; j <= mx; j += i)
mod_inc(h[v][j], g[u][i]);
else if(mu[i] < 0)
for(int j = i; j <= mx; j += i)
mod_dec(h[v][j], g[u][i]);
dfs(v);
}
}
int main()
{
mu[1] = 1;
for(int i = 1; i < maxm; ++i)
for(int j = i + i; j < maxm; j += i)
mu[j] -= mu[i];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", l + i);
mx = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%d", r + i);
if(mx < r[i])
mx = r[i];
}
for(int i = 1; i <= n; ++i)
vector<int>().swap(e[i]);
for(int i = 1, u, v; i < n; ++i)
{
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
pfs(1);
for(int i = l[1]; i <= r[1]; ++i)
h[1][i] = 1;
dfs(1);
for(int i = 1; i <= n; ++i)
{
int ans = 0;
for(int j = l[i]; j <= r[i]; ++j)
ans = (ans + (LL)j * f[i][j]) % mod;
printf("%d%c", ans, " \n"[i == n]);
}
}
return 0;
}

10-15 236

10-15 291

10-12 1168

10-13 2059

10-15 260

10-16 316

09-09 2112

10-12 391

05-19 18

10-12 407

#### The 2014 ACM-ICPC Asia Mudanjiang Regional Contest - I.Information Entropy

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。