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 ...
随机推荐
- Zernike矩之图像重建(附源码)
源码下载 参考: [1] Teague M R. Image analysis via the general theory of moments[J]. JOSA, 1980, 70(8): 920 ...
- eclipse XML TAB键默认为四个空格
- TNS-12508 When Issuing Any SET Command For The Listene
TNS-12508 When Issuing Any SET Command For The Listener fact: Oracle Net Services fact: TNS Liste ...
- Nginx重写规则
Nginx的重写规则,依赖于pcre库(perl compatible regular expression).所以在安装的时候一定要让nginx支持这个功能,以及安装pcre-devel,prce. ...
- Spark MLlib编程API入门系列之特征选择之向量选择(VectorSlicer)
不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). VectorSlicer用于从原来的特征 ...
- P2956 [USACO09OCT]机器人犁田The Robot Plow
题目描述 Farmer John has purchased a new robotic plow in order to relieve him from the drudgery of plowi ...
- P2629 好消息,坏消息
题目描述 uim在公司里面当秘书,现在有n条消息要告知老板.每条消息有一个好坏度,这会影响老板的心情.告知完一条消息后,老板的心情等于之前老板的心情加上这条消息的好坏度.最开始老板的心情是0,一旦老板 ...
- 04.Java多线程并发库API使用3
1.java5的Semaphere同步工具 Semaphore可以维护当前访问自身的线程个数,并提供了同步机制.使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数 ...
- GeoTools坐标转换(投影转换和仿射变换)
GeoTools是在java下的gis开源软件,以下介绍坐标转换的两种方法:投影转换和仿射变换 投影转换 这里以xian80经纬度坐标转xian80,3度分带 111中央经线平面坐标为例 转换函数如下 ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...