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. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  2. mysql学习之通过文件创建数据库以及添加数据

    转自:http://blog.163.com/wujicaiguai@126/blog/static/170171558201411311547655/ 1.# 创建数据库语句 create data ...

  3. find missing conjunction, why?

    find . -name *.c find: missing conjunction, why? SHELL会把*.c直接扩展成当前工作目录的多个.c文件,所以必须用单引号'*.c'或者/*.c进行转 ...

  4. 三色灯渐变DIY制作

    小编前几天查资料,怎么使用12864屏幕的用法,突然发觉微博是个好东西,随着自己的成长,学习了很多的知识,没有做笔记的习惯,只是习惯把用到的硬件,传感器,资料写到程序的备注内,但感觉,用到时不是那么方 ...

  5. 数字(number)

    数字(number) Time Limit:2000ms   Memory Limit:128MB 题目描述 LYK定义了一个新的计算. 具体地,一开始它有两个数字a和b. 每一步,它可以将b增加1, ...

  6. Python 版本对比

    python2 与 python3可认为代码不通用,你也可以点击Python2.x与3​​.x版本区别来查看两者的不同 python3.6以上支持f-string,一种很方便的变量替换方式 高版本可能 ...

  7. JS编写自己的富文本编辑器

    富文本编辑器,网上有很多功能齐全种类丰富的如百度的Ueditor,简单适用型的如WangEditor等等.在经过一番挑选后,我发现都不适用现在的项目,然后决定自己造轮子玩玩.富文本编辑器中主要涉及到J ...

  8. JMeter进入接口压力测试

    关键字: Jmeter.单接口.压力测试.插件监听.服务器端 摘要: 使用Jmeter对单个接口进行压力测试:监听并发量对接口响应时间.服务器资源占量.Jmeter本身只能获取到Tomcat的状态,所 ...

  9. Cognos邮件发送

    1.打开报表,点击下图的标记 2.设置发送格式收件人 3.设置报表格式 4.设置发送内容

  10. Mac environment setting

    java 7 jdk http://www.ifunmac.com/2013/04/mac-jdk-7/ http://blog.sina.com.cn/s/blog_6dce99b101016744 ...