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 ...
随机推荐
- ImageView加载长图(适用不需要缩放的情况)
此案例适用于加载网络长图且图片的宽和高已知的情况.由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理. 有两种解决方案:第一种就是比较l ...
- HDU 2828 Lamp 二分图的最大匹配 模型题
http://acm.hdu.edu.cn/showproblem.php?pid=2828 给定n个灯,m个开关,使得每栈灯亮,前提是控制这栈灯的开关的状态是其中一个.(题目应该都看得懂) 其实我想 ...
- 动手实现 Redux(五):不要问为什么的 reducer
经过了这么多节的优化,我们有了一个很通用的 createStore: function createStore (state, stateChanger) { const listeners = [] ...
- Elasticsearch (1) - 索引库 文档 分词
创建索引库 ES的索引库是一个逻辑概念,它包括了分词列表及文档列表,同一个索引库中存储了相同类型的文档.它就相当于MySQL中的表,或相当于Mongodb中的集合. 关于索引这个语: 索引(名词):E ...
- ImageView控件
ImageView 显示图片 常用属性: src 要显示的图片 foreground 前景图 backgrund 背景图 alpha 透明度 clickable 是否可以点击 onClick ...
- Android 基础知识总结
搞了这么久安卓开发,对基础的知识点总会遗忘,所有有必要总结一下:
- JMeter进入接口压力测试
关键字: Jmeter.单接口.压力测试.插件监听.服务器端 摘要: 使用Jmeter对单个接口进行压力测试:监听并发量对接口响应时间.服务器资源占量.Jmeter本身只能获取到Tomcat的状态,所 ...
- SqlSessionFactory
源码: public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean va ...
- 洛谷 P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- easybcd 支持 windows 10 和 ubuntu 14.04 双系统启动
家里计算机系统 windows 10 全新安装. 原本是双系统的,还有一个ubuntu. windows 10 安装以后,恢复ubuntu就是问题了. (事后经验:请不要立刻安装bcd修改工具) 最初 ...