ZOJ 3824 Fiber-optic Network
Fiber-optic Network
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 [Li, Ri]. 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

#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的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- zoj 1967 Fiber Network/poj 2570
题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...
- 2014牡丹江 现场赛 F zoj 3824 Fiber-optic Network
首先赞一下题目, 好题 题意: Marjar University has decided to upgrade the infrastructure of school intranet by us ...
- ZOJ 2182 Cable TV Network(无向图点割-最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2182 题意:给出一个无向图,问最少删掉多少个顶点之后图变得不连通 ...
- POJ 1966 ZOJ 2182 Cable TV Network
无向图顶点连通度的求解,即最少删除多少个点使无向图不连通. 我校“荣誉”出品的<图论算法理论.实现及其应用>这本书上写的有错误,请不要看了,正确的是这样的: 对于每个顶点,分成两个点,v和 ...
- ZOJ 2676 Network Wars[01分数规划]
ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special J ...
- Heterogeneous Self-Organizing Network for Access and Backhaul
This application discloses methods for creating self-organizing networks implemented on heterogeneou ...
- 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 ...
- 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 ...
随机推荐
- 题解报告:hdu 4607 Park Visit(最长链)
Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...
- 18.3.2从Class上获取信息(注解)
package d18_3_1; /** * Class类上所包含的注解 * * getAnnotation(Class annotationClass) 获取该元素上指定的类型的注解 * getAn ...
- gp服务输出的结果文件输出到绝对路径
gp服务跟本地用arcmap执行gp有个不同,就是输出的文件一般只能输出到arcgis server默认的output目录里面(arcgis server有此限制,无论怎么配还是写到output目录里 ...
- qt read excel
void exceladapter::readfile(QString filename, QString sheetname, int colNo){ QSqlDatabase db = QSqlD ...
- 如何在win7、win8、win8.1上安装使用vb6.0
https://jingyan.baidu.com/article/915fc414fdf8fb51384b2062.html如何在win7.win8.win8.1上安装使用vb6.0 如何在win7 ...
- bat判断服务是否启动
sc query|find "tomcat6" && echo yes || echo nosc query|find "eventlog" & ...
- 为Qt添加SSL支持
目标:为Qt添加SSL支持,使得应用可以发送HTTPS请求 环境:win7,Qt4.8.6 步骤: 1.到http://slproweb.com/products/Win32OpenSSL.html下 ...
- CENTOS6.4上KVM虚拟机环境搭建
CENTOS6.4上KVM虚拟机环境搭建 关键词: KVM,虚拟机,windows7, VNC, 桥接网络,br0, SCSI, IDE 环境: host: CENTOS6.4 guest: ...
- HTTP 方法:GET 对比 POST 转自w3school
两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客户机与服务器之间的请求-应答协 ...
- web测试需要注意点