[Codeforces 986E] Prince's Problem
[题目链接]
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的更多相关文章
- Codeforces 986E - Prince's Problem(树上前缀和)
题面传送门 题意: 有一棵 \(n\) 个节点的树,点上有点权 \(a_i\),\(q\) 组询问,每次询问给出 \(u,v,w\),要求: \(\prod\limits_{x\in P(u,v)}\ ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
- codeforces 803G Periodic RMQ Problem
codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...
- 【codeforces 527D】Clique Problem
[题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
随机推荐
- [Python3网络爬虫开发实战] 7.3-Splash负载均衡配置
用Splash做页面抓取时,如果爬取的量非常大,任务非常多,用一个Splash服务来处理的话,未免压力太大了,此时可以考虑搭建一个负载均衡器来把压力分散到各个服务器上.这相当于多台机器多个服务共同参与 ...
- buf.slice()
buf.slice([start[, end]]) start {Number} 默认:0 end {Number} 默认:buffer.length 返回:{Buffer} 返回一个指向相同原始内存 ...
- 洛谷 4933 洛谷10月月赛II T2 大师
[题解] f[i][j]表示最后一个数为h[i],公差为j的等差数列的个数.n方枚举最后一个数和倒数第二个数转移即可.注意公差可能为负数,需要移动为正数再作为下标. #include<cstdi ...
- PHP学习笔记<参数的传递>
简单的例子说明参数在PHP文件之间的传递(有两个PHP文件在index.php文件上点击链接,在跳转的时候,依据参数的不同在neirong.php文件中显示不同的内容) inde.php的内容如下: ...
- xtu summer individual 5 F - Post Office
Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...
- spark streaming基于Kafka的开发
spark streaming使用Kafka数据源进行数据处理,本文侧重讲述实践使用. 一.基于receiver的方式 在使用receiver的时候,如果receiver和partition分配不当, ...
- HDU1074 Doing Homework 状态压缩dp
题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 /* 首先要明白的一点是状态1/0分别表示这件事做了还是没做 而1/0的位 ...
- ZOJ 3329 期望DP
题目大意: 给定3个已经规定好k1,k2,k3面的3个色子,如果扔到a,b,c则重新开始从1 计数,否则不断叠加所有面的数字之和,直到超过n,输出丢的次数的数学期望 我们在此令dp[]数组记录从当前数 ...
- 详解SpringBoot 添加对JSP的支持(附常见坑点)
序言: SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作.为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP . 常见 ...
- 听dalao讲课 7.26
XFZ今天讲了些关于多项式求ln和多项式求导以及多项式求积分的东西 作为一个连导数和积分根本就不会的蒟蒻,就像在听天书,所以不得不补点前置知识 1.积分 积分是微积分学与数学分析里的一个核心概念.通常 ...