[题目链接]

https://codeforces.com/contest/986/problem/E

[算法]

X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径积 , 再除以LCA父节点到根的路径积

考虑如何计算根到X路径上每个点与Value的GCD之积

不妨对于每个质数P开一个数组cnt[] , 表示根到当前节点P^i有多少个 , 我们可以在DFS的过程中维护这个数组

将询问离线即可

时间复杂度 : O(V + NlogN + QlogV^2)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXLOG 30
const int P = 1e9 + ;
const int MAXN = 1e5 + ;
const int MAXP = 1e6 + ;
const int MAXV = 1e7 + ; struct edge
{
int to , nxt;
} e[MAXN << ];
struct info
{
int value , home;
bool type;
} ;
int tot , n , t;
int a[MAXN],depth[MAXN],prime[MAXP],head[MAXN],ans[MAXN],f[MAXV];
int cnt[MAXP][MAXLOG],anc[MAXN][MAXLOG];
vector< info > q[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
t++;
e[t] = (edge){v,head[u]};
head[u] = t;
}
inline int exp_mod(int a,int n)
{
int res = , b = a;
while (n > )
{
if (n & ) res = 1ll * res * b % P;
b = 1ll * b * b % P;
n >>= ;
}
return res;
}
inline int inv(int x) { return exp_mod(x,P - ); }
inline void dfs(int u,int fa)
{
depth[u] = depth[fa] + ;
for (int i = ; i < MAXLOG; i++)
{
if (depth[u] <= ( << i)) break;
anc[u][i] = anc[anc[u][i - ]][i - ];
}
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
anc[v][] = u;
depth[v] = depth[u] + ;
dfs(v,u);
}
}
inline int lca(int u,int v)
{
if (depth[u] > depth[v]) swap(u,v);
for (int i = MAXLOG - ; i >= ; i--)
{
if (depth[anc[v][i]] >= depth[u])
v = anc[v][i];
}
if (u == v) return u;
for (int i = MAXLOG - ; i >= ; i--)
{
if (anc[u][i] != anc[v][i])
u = anc[u][i] , v = anc[v][i];
}
return anc[u][];
}
inline void modify(int x,int delta)
{
for (int i = ; 1ll * prime[i] * prime[i] <= x; i++)
{
if (x % prime[i] == )
{
int p = ;
while (x % prime[i] == )
{
x /= prime[i];
p++;
}
cnt[i][p] += delta;
}
}
if (x != )
{
int pos = lower_bound(prime + ,prime + tot + ,x) - prime;
cnt[pos][] += delta;
}
}
inline int query(int x)
{
int ret = ;
for (int i = ; 1ll * prime[i] * prime[i] <= x; i++)
{
if (x % prime[i] == )
{
int p = ;
while (x % prime[i] == )
{
p++;
x /= prime[i];
}
int s = ;
for (int j = ; j <= p; j++) s += cnt[i][j] * j;
for (int j = p + ; j < MAXLOG; j++) s += cnt[i][j] * p;
ret = 1ll * ret * exp_mod(prime[i],s) % P;
}
}
if (x != )
{
int pos = lower_bound(prime + ,prime + tot + ,x) - prime;
int s = ;
for (int i = ; i < MAXLOG; i++) s += cnt[pos][i];
ret = 1ll * ret * exp_mod(x,s) % P;
}
return ret;
} inline void solve(int u,int fa)
{
modify(a[u],);
for (unsigned i = ; i < q[u].size(); i++)
{
if (q[u][i].type) ans[q[u][i].home] = 1ll * ans[q[u][i].home] * query(q[u][i].value) % P;
else ans[q[u][i].home] = 1ll * ans[q[u][i].home] * inv(query(q[u][i].value)) % P;
}
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
solve(v,u);
}
modify(a[u],-);
} int main()
{ read(n);
for (int i = ; i < MAXV; i++)
{
if (!f[i])
{
f[i] = i;
prime[++tot] = i;
}
for (int j = ; j <= tot; j++)
{
int tmp = i * prime[j];
if (tmp >= MAXV) break;
f[tmp] = prime[j];
if (prime[j] == f[i]) break;
}
}
for (int i = ; i < n; i++)
{
int u , v;
read(u); read(v);
addedge(u,v);
addedge(v,u);
}
dfs(,);
for (int i = ; i <= n; i++) read(a[i]);
int Q;
read(Q);
for (int i = ; i <= Q; i++)
{
int u , v , x;
read(u); read(v); read(x);
int Lca = lca(u,v);
q[u].push_back((info){x,i,true});
q[v].push_back((info){x,i,true});
q[Lca].push_back((info){x,i,false});
if (Lca != ) q[anc[Lca][]].push_back((info){x,i,false});
ans[i] = ;
}
solve(,);
for (int i = ; i <= Q; i++) printf("%d\n",ans[i]); return ; }

[Codeforces 986E] Prince's Problem的更多相关文章

  1. Codeforces 986E - Prince's Problem(树上前缀和)

    题面传送门 题意: 有一棵 \(n\) 个节点的树,点上有点权 \(a_i\),\(q\) 组询问,每次询问给出 \(u,v,w\),要求: \(\prod\limits_{x\in P(u,v)}\ ...

  2. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  3. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  4. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  5. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  6. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  7. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...

  8. 【codeforces 527D】Clique Problem

    [题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...

  9. 【codeforces 793C】Mice problem

    [题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...

随机推荐

  1. mysql事物隔离

    1.读未提交(行锁) 会发生脏读,事物未提交被其他事物看到,未提交的数据为脏数据. 2.读已提交(行锁) 会发生不可重复读,事物开始时,只能看到已经提交了的事物修改. 3.重复读(行锁) 该级别保证了 ...

  2. Python之机器学习-sklearn生成随机数据

    sklearn-生成随机数据 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotli ...

  3. python书籍推荐:Python数据科学手册

    所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...

  4. django2

    八 Models 数据库的配置 1    django默认支持sqlite,mysql, oracle,postgresql数据库.  <1> sqlite django默认使用sqlit ...

  5. onos控制器通过REST API下发流表

    onos控制器REST API地址:http://192.168.43.14:8181/onos/v1/docs/ stream书写格式: { "id": "675540 ...

  6. poj 1752 Advertisement (差分约束)

    题目大意:题目大意:有n个人在一条路上跑步,广告商准备在这条路上设置广告牌,假设这条路上每一个点有一个广告牌 现在已知这n个人从Ai开始跑,到Bi结束,那么他可以看到max(Ai,Bi)-min(Ai ...

  7. MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能

    数据操作 插入数据(记录): 用insert: 补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...; ...

  8. cdq分治入门--BZOJ3262: 陌上花开

    n<=100000个人,每个人三个属性Ai,Bi,Ci,一个人i的等级为Ai>=Aj,Bi>=Bj,Ci>=Cj的人数,求每个等级有多少人. 裸的三维偏序.按照常规思路,一维排 ...

  9. msp430入门学习06

    msp430的IO端口的第一功能 msp430入门学习

  10. springboot 关于第三方包 打包问题

    第三方包: 添加library 依赖 在pom.xml中配置 <resources> <resource> <directory>lib</directory ...