Fiber-optic Network

Time Limit: 15000ms
Memory Limit: 262144KB

This problem will be judged on ZJU. Original ID: 3824
64-bit integer IO format: %lld      Java class name: Main

 

one router. These routers are connected by optical cables in such a way that there is exactly one path between any two routers.

Each router should be initialized with an operating frequency Fi before it starts to work. Due to the limitations of hardware and environment, the operating frequency should be an integer number within [LiRi]. In order to reduce the signal noise, the operating frequency of any two adjacent routers should be co-prime.

Edward is the headmaster of Marjar University. He is very interested in the number of different ways to initialize the operating frequency. Please write a program to help him! To make the report simple and neat, you only need to calculate the sum of Fi (modulo 1000000007) in all solutions for each router.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains one integer N (1 <= N <= 50). The next line contains N integers Li (1 <= Li <= 50000). Then, the following line contains N integers Ri (Li <= Ri <= 50000).

For the next N - 1 lines, each line contains two integers Xi and Yi. That means there is an optical cable connecting router Xi and router Yi (indexes are 1-based).

Output

For each test case, output a line with N integers representing the sum of Fi (modulo 1000000007) in all solutions.

Sample Input

2
4
1 2 3 4
2 3 4 5
1 2
2 3
3 4
4
1 2 3 4
2 3 4 5
1 2
1 3
1 4

Sample Output

5 10 14 19
10 23 31 41

Hint

In the first sample test case, there are 4 ways to initialize the operating frequency:

  • 1 2 3 4
  • 1 2 3 5
  • 1 3 4 5
  • 2 3 4 5
 

Source

Author

JIANG, Kai
 
解题:一道比较重口味的树形dp数论容斥题
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
const int maxm = ;
const LL mod = ;
vector<int>p[maxn];
vector<int>g[maxm];
vector<int>fac[maxm][maxn];
bool np[maxn] = {true,true};
int L[maxm],R[maxm];
LL dp[maxm][maxn],multi[maxm][maxn],sum[maxm],ans[maxm];
void init() {
for(int i = ; i < maxn; ++i) {
if(!np[i]) {
for(int j = i; j < maxn; j += i) {
np[j] = true;
p[j].push_back(i);
}
}
}
}
LL quickPow(LL base,LL index,LL mod) {
LL ret = ;
while(index) {
if(index&) ret = ret*base%mod;
base = base*base%mod;
index >>= ;
}
return ret;
}
LL inv(LL b,LL mod) {
return quickPow(b,mod-,mod);
}
void dfs(int u,int fa) {
for(int i = L[u]; i <= R[u]; ++i) dp[u][i] = ;
for(int i = ; i < g[u].size(); ++i) {
if(g[u][i] == fa) continue;
dfs(g[u][i],u);
}
for(int j = L[u]; j <= R[u]; ++j) {
fac[u][j].clear();
for(int i = ; i < g[u].size(); ++i) {
if(g[u][i] == fa) continue;
LL S = ;
for(int k = ,sz = (<<p[j].size()); k < sz; ++k) {
LL tmp = ;
int cnt = ;
for(int t = ; t < p[j].size(); ++t) {
if((k>>t)&) {
++cnt;
tmp *= p[j][t];
if(tmp >= maxn) break;
}
}
if(tmp < maxn) S = (S + ((cnt&)?multi[g[u][i]][tmp]:-multi[g[u][i]][tmp]))%mod;
}
LL num = ((sum[g[u][i]] - S)%mod + mod)%mod;
dp[u][j] = dp[u][j]*num%mod;
fac[u][j].push_back(num);
}
}
sum[u] = ;
for(int i = ; i < maxn; ++i) {
sum[u] = (sum[u] + dp[u][i])%mod;
multi[u][i] = ;
for(int j = i; j < maxn; j += i)
multi[u][i] = (multi[u][i] + dp[u][j])%mod;
}
}
void dfs2(int u,int fa) {
ans[u] = ;
for(int i = L[u]; i <= R[u]; ++i) ans[u] = (ans[u] + dp[u][i]*i)%mod;
for(int i = ,c = ; i < g[u].size(); ++i) {
if(g[u][i] == fa) continue;
for(int j = L[u]; j <= R[u]; ++j)
if(dp[u][j]) dp[u][j] = dp[u][j]*inv(fac[u][j][c],mod)%mod;
sum[u] = ;
for(int k = ; k < maxn; ++k) {
multi[u][k] = ;
sum[u] = (sum[u] + dp[u][k])%mod;
for(int j = k; j < maxn; j += k)
multi[u][k] = (multi[u][k] + dp[u][j])%mod;
}
for(int j = L[g[u][i]]; j <= R[g[u][i]]; ++j) {
LL S = ;
for(int k = ,sz = p[j].size(); k < (<<sz); ++k) {
int cnt = ;
LL tmp = ;
for(int t = ; t < sz; ++t)
if((k>>t)&) {
++cnt;
tmp *= p[j][t];
if(tmp >= maxn) break;
}
if(tmp < maxn) S = (S + ((cnt&)?multi[u][tmp]:-multi[u][tmp]))%mod;
}
dp[g[u][i]][j] = dp[g[u][i]][j]*(((sum[u] - S)%mod + mod)%mod)%mod;
}
dfs2(g[u][i],u);
for(int j = L[u]; j <= R[u]; ++j)
if(dp[u][j]) dp[u][j] = dp[u][j]*fac[u][j][c]%mod;
++c;
}
}
int main() {
init();
int kase,n,u,v;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
for(int i = ; i <= n; ++i){
scanf("%d",L + i);
g[i].clear();
}
memset(dp,,sizeof dp);
for(int i = ; i <= n; ++i)
scanf("%d",R + i);
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(,-);
dfs2(,-);
for(int i = ; i <= n; ++i)
printf("%lld%c",ans[i],i==n?'\n':' ');
}
return ;
}

ZOJ 3824 Fiber-optic Network的更多相关文章

  1. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  2. zoj 1967 Fiber Network/poj 2570

    题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...

  3. 2014牡丹江 现场赛 F zoj 3824 Fiber-optic Network

    首先赞一下题目, 好题 题意: Marjar University has decided to upgrade the infrastructure of school intranet by us ...

  4. ZOJ 2182 Cable TV Network(无向图点割-最大流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2182 题意:给出一个无向图,问最少删掉多少个顶点之后图变得不连通 ...

  5. POJ 1966 ZOJ 2182 Cable TV Network

    无向图顶点连通度的求解,即最少删除多少个点使无向图不连通. 我校“荣誉”出品的<图论算法理论.实现及其应用>这本书上写的有错误,请不要看了,正确的是这样的: 对于每个顶点,分成两个点,v和 ...

  6. ZOJ 2676 Network Wars[01分数规划]

    ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special J ...

  7. Heterogeneous Self-Organizing Network for Access and Backhaul

    This application discloses methods for creating self-organizing networks implemented on heterogeneou ...

  8. Method and apparatus for establishing IEEE 1588 clock synchronization across a network element comprising first and second cooperating smart interface converters wrapping the network element

    Apparatus for making legacy network elements transparent to IEEE 1588 Precision Time Protocol operat ...

  9. Internet History, Technology and Security (Week 5-1)

    Week 5 Technology: Internets and Packets Welcome to Week 5! This week, we'll be covering internets a ...

随机推荐

  1. Jury Meeting CodeForces - 854D

    Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体 ...

  2. java自带线程池

    1. newSingleThreadExecutor 创建一个单线程的线程池.这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务.如果这个唯一的线程因为异常结束,那么会有一个新的线程来替 ...

  3. nodejs的mysql模块学习笔记(结合业务)

    1. 包官网地址 https://www.npmjs.com/package/mysql#install https://www.oschina.net/translate/node-mysql-tu ...

  4. webpack采坑十连跳

    4.css.js一同打包进html ---------------- https://www.cnblogs.com/amiezhang/p/9723565.html 依赖内联插件  HtmlWebp ...

  5. 浅议block实现原理,block为什么使用copy关键字?

    1.block是一个特殊的oc对象,建立在栈上,而不是堆上,这么做一个是为性能考虑,还有就是方便访问局部变量. 2.默认Block使用到的局部变量会被copy,而不是retain.所以,他无法改变局部 ...

  6. re正则表达式公式讲解1

    常用的表达式一些规则 1.“.”  匹配出了\n之外的任意一个字符,包括特殊字符 有几个·就匹配几个字符. import re print(re.search("."," ...

  7. 【HEVC帧间预测论文】P1.5 Fast Coding Unit Size Selection for HEVC based on Bayesian Decision Rule

    Fast Coding Unit Size Selection for HEVC based on Bayesian Decision Rule <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  8. CFBundleURLTypes URL scheme

    https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Ar ...

  9. Postman 安装及使用入门教程 | 前后台 写接口的 徐工给的

    https://www.cnblogs.com/mafly/p/postman.html

  10. ORA-03113: end-of-file on & ORA-07445

    --------------ORA-03113: end-of-file on-------------- SQL> show parameter background_dump; NAME T ...