zoj 2974 Just Pour the Water矩阵快速幂
Just Pour the Water
Time Limit: 2 Seconds Memory Limit: 65536 KB
Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the water in B into A. As the pouring continues, she finds it is very easy to calculate the amount of water in A and B at any time. It is really an easy job :).
But now Shirly wants to know how to calculate the amount of water in each container if there are more than two containers. Then the problem becomes challenging.
Now Shirly has N (2 <= N <= 20) containers (numbered from 1 to N). Every minute, each container is supposed to pour water into another K containers (K may vary for different containers). Then the water will be evenly divided into K portions and accordingly poured into anther K containers. Now the question is: how much water exists in each container at some specified time?
For example, container 1 is specified to pour its water into container 1, 2, 3. Then in every minute, container 1 will pour its 1/3 of its water into container 1, 2, 3 separately (actually, 1/3 is poured back to itself, this is allowed by the rule of the game).
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case starts with a line containing an integer N, the number of containers. The second line contains N floating numbers, denoting the initial water in each container. The following N lines describe the relations that one container(from 1 to N) will pour water into the others. Each line starts with an integer K (0 <= K <= N) followed by K integers. Each integer ([1, N]) represents a container that should pour water into by the current container. The last line is an integer M (1<= M <= 1,000,000,000) denoting the pouring will continue for M minutes.
Output
For each test case, output contains N floating numbers to two decimal places, the amount of water remaining in each container after the pouring in one line separated by one space. There is no space at the end of the line.
Sample Input
1
2
100.00 100.00
1 2
2 1 2
2
Sample Output
75.00 125.00
Note: the capacity of the container is not limited and all the pouring at every minute is processed at the same time.
题目大意:有N个容器,编号为i的容器可以把自己的水分成k份放进k个容器里,每次把1—N依次操作,求M次后各个容器里的水量。
解题思路:矩阵快速幂(注意K=0的情况,我被坑了一发)。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
double f[maxn]; struct Matrix
{
double a[maxn][maxn];
int n;
}; Matrix Matrix_mult(Matrix A,Matrix B)
{
Matrix C;C.n=A.n;
int i,j,k;
for(i=;i<=A.n;i++)
{
for(j=;j<=A.n;j++)
{
double sum=;
for(k=;k<=A.n;k++)
sum+=A.a[i][k]*B.a[k][j];
C.a[i][j]=sum;
}
}
return C;
} Matrix Matrix_pow(Matrix A,int m)
{
Matrix ret;ret.n=A.n;
memset(ret.a,,sizeof(ret.a));
for(int i=;i<=A.n;i++) ret.a[i][i]=1.0;
while(m)
{
if(m&) ret=Matrix_mult(A,ret);
A=Matrix_mult(A,A);m>>=;
}
return ret;
} void Printf(Matrix A)
{
for(int i=;i<=A.n;i++)
{
double ans=;
for(int j=;j<=A.n;j++)
ans+=A.a[i][j]*f[j];
printf(i==?"%.2lf":" %.2lf",ans);
}
printf("\n");
} int main()
{
int t,i,j,k,p,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%lf",f+i);
Matrix A;A.n=n;
for(i=;i<=n;i++)
{
scanf("%d",&k);
for(j=;j<=n;j++) A.a[j][i]=;
if(k==)
{
A.a[i][i]=1.0;continue;
}
double c=1.0/k;
for(j=;j<=k;j++)
{
scanf("%d",&p);A.a[p][i]=c;
}
}
scanf("%d",&m);
Matrix B=Matrix_pow(A,m);
Printf(B);
}
return ;
}
zoj 2974 Just Pour the Water矩阵快速幂的更多相关文章
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
- ZOJ 2974 Just Pour the Water
矩阵快速幂. 构造一个矩阵,$a[i][j]$表示一次操作后,$j$会从$i$那里得到水的比例.注意$k=0$的时候,要将$a[i][j]$置为$1$. #pragma comment(linker, ...
- ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- 焦作网络赛L-Poor God Water【矩阵快速幂】
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- dutacm.club Water Problem(矩阵快速幂)
Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Tot ...
- ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)
https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同:2.若三种食物都有的情况,巧克力不能在中 ...
随机推荐
- linux虚拟机配置网络
第一步.网络模式设置为桥接模式 第二步.设置ip和掩码 vim /etc/sysconfig/network-scripts/ifcfg-ens33 ens33为当前机器的网卡名称 在文件尾部添 ...
- 66. Plus One@python
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- 【数学 BSGS】bzoj2242: [SDOI2011]计算器
数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...
- How to Install PhantomJS on Ubuntu 16.04
Introduction PhantomJS is a scripted, headless browser that can be used for automating web page inte ...
- Python3学习了解日记
# 单行注释 ''' 多行注释 ''' """ 这个也是多行注释 """ ''' 声明变量 Python 中的变量不需要声明.每个变量在使用 ...
- python3通过Beautif和XPath分别爬取“小猪短租-北京”租房信息,并对比时间效率(附源代码)
爬虫思路分析: 1. 观察小猪短租(北京)的网页 首页:http://www.xiaozhu.com/?utm_source=baidu&utm_medium=cpc&utm_term ...
- leetcode-27-exercise_bit maniputation
461. Hamming Distance 解题思路: 把两个数的每一位和1比较,如果结果不同说明这两位不同.要比较32次. int hammingDistance(int x, int y) { i ...
- PHP如何利用sleep实现 输出->等待->输出
sleep()函数一般用在暂停上,但是一个PHP一旦有了sleep,其他的输出(print,echo)就都要等待sleep()函数的完成,这是因为缓冲区,这里有详细解释 在这里不赘述,而如果要实现先输 ...
- JAVA 消耗 CPU过高排查方法
#找出cpu占用最高的进程top -H#再次确定进程ps aux|grep 17408 #查看进程的线程(tid) ps -mp 17408 -o THREAD,tid,time#将线程转换为十六进制 ...
- 智能DNS解析之edns-client-subnet篇
摘要:智能DNS解析是CDN的重要组成部份,所谓的智能也就是根据请求用户来对同一域名作出相应不同解析(目前大多数域名注册商还没提供线路解析的服务),所以CDN的调度准确性也就完全依靠DNS智能解析,但 ...