[题目链接]

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. PHP 数组使用之道

    本文首发于 PHP 数组使用之道,转载请注明出处. 这个教程我将通过一些实用的实例和最佳实践的方式列举出 PHP 中常用的数组函数.每个 PHP 工程师都应该掌握它们的使用方法,以及如何通过组合使用来 ...

  2. Vue实例方法之事件的实现

    开始 这段时间一直在看vue的源码,源码非常多和杂,所以自己结合资料和理解理出了一个主线,然后根据主线去剥离其他的一些知识点,然后将各个知识点逐一学习.这里主要是分析的Vue事件处理的实现. 正文 一 ...

  3. html css笔记 -度一

    """浏览器 shell 内核外表 内心 IE tridentFirefox Geckogoogle chrome webkit/blinksafari webkitop ...

  4. 洛谷 4933 洛谷10月月赛II T2 大师

    [题解] f[i][j]表示最后一个数为h[i],公差为j的等差数列的个数.n方枚举最后一个数和倒数第二个数转移即可.注意公差可能为负数,需要移动为正数再作为下标. #include<cstdi ...

  5. python字典及相关操作

    1.字典 1.1.字典特性 字典是一种key-value的数据类型.key必须可hash,必须为不可变数据类型,且必须是唯一的:value可以存放任意多个值.可修改.可以不唯一:字典是无序的,通过ke ...

  6. 如何在matlab里安装libsvm包

    有时我们需要用到SVR(支持向量回归)方法,而 matlab 自带的svm工具箱不能做回归分析,于是有了安装libsvm包的打算. 中间遇到一些困难,比如找不到编译器等等,经过一下午和一晚上的努力,在 ...

  7. xtu summer individual 5 D - Subsequence

    Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  8. Leetcode 187.重复的DNA序列

    重复的DNA序列 所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮 ...

  9. 安装K/3 Cloud过程中发现的两个新问题。

    卸载掉K/3 Cloud然后重装时出现下面的错误提示: 可能原因: 1.安装目录下的Setup.exe会检查操作系统版本.有些操作系统可能是被串改过注册信息,所以取不到版本信息(有些是因为盗版的原因) ...

  10. msp430入门学习13

    msp430的定时器--Timer_B(定时器B)