[题目链接]

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. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. Python之机器学习-朴素贝叶斯(垃圾邮件分类)

    目录 朴素贝叶斯(垃圾邮件分类) 邮箱训练集下载地址 模块导入 文本预处理 遍历邮件 训练模型 测试模型 朴素贝叶斯(垃圾邮件分类) 邮箱训练集下载地址 邮箱训练集可以加我微信:nickchen121 ...

  3. Python操作Redis、Memcache

       今天主要介绍如何通过python来对Redis和memcache进行操作,下面开始今天的内容: 一.Memcached操作 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web ...

  4. Qt笔记——2.编写多窗口程序

    所学教程网址:http://www.qter.org/portal.php?mod=view&aid=27&page=2 设置按钮文字 MainWindow::MainWindow(Q ...

  5. Python爬虫 爬取Web页面图片

    从网页页面上批量下载jpg格式图片,并按照数字递增命名保存到指定的文件夹 Web地址:http://news.weather.com.cn/2017/12/2812347.shtml 打开网页,点击F ...

  6. xtu summer individual-4 D - Martian Strings

    Martian Strings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. IntentService用于服务中开启子线程的自动关闭

    package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...

  8. Mac 系统引导过程概述 & BootCamp 的秘密

    http://bbs.feng.com/read-htm-tid-6890655.html

  9. 我的arcgis培训照片13

    来自:http://www.cioiot.com/successview-535-1.html

  10. HTTP请求方式之GET和POST比较

    什么是HTTP协议 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 HTTP在客户端和服务器之间以request ...