ZOJ 2974 Just Pour the Water
矩阵快速幂。
构造一个矩阵,$a[i][j]$表示一次操作后,$j$会从$i$那里得到水的比例。注意$k=0$的时候,要将$a[i][j]$置为$1$。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} struct Matrix
{
double A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; int T,n,p; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= b.C; j++)
for (k = ; k <= C; k++)
c.A[i][j] = c.A[i][j] + A[i][k] * b.A[k][j];
c.R = R; c.C = b.C;
return c;
} void init()
{
Y.R = n; Y.C = n;
for (int i = ; i <= n; i++) Y.A[i][i] = ;
X.R = n; X.C = n;
Z.R = ; Z.C = n;
} void work()
{
while (p)
{
if (p % == ) Y = Y*X;
p = p >> ;
X = X*X;
}
Z = Z*Y;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n); memset(X.A, , sizeof X.A);
memset(Y.A, , sizeof Y.A);
memset(Z.A, , sizeof Z.A); for(int i=;i<=n;i++) scanf("%lf",&Z.A[][i]); for(int i=;i<=n;i++)
{
int K; scanf("%d",&K);
for(int j=;j<=K;j++)
{
int id; scanf("%d",&id);
X.A[i][id]=1.0/K;
}
if(K==)
{
X.A[i][i]=1.0;
}
} scanf("%d",&p); init();
work(); for(int i=;i<=n;i++)
{
printf("%.2f",Z.A[][i]);
if(i<n) printf(" "); else printf("\n");
}
}
return ;
}
ZOJ 2974 Just Pour the Water的更多相关文章
- zoj 2974 Just Pour the Water矩阵快速幂
Just Pour the Water Time Limit: 2 Seconds Memory Limit: 65536 KB Shirly is a very clever girl. ...
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
- ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M ...
- ZOJ 2974 矩阵快速幂
题意 给出n个杯子与初始其中有多少水 “同时”进行如下指令 将其中的水同时分入所指定的杯子 进行x次后 输出杯子剩余水量 队友想出应该是一道快速幂 但并不是过去的用初始杯子的水组成的矩阵乘某个矩阵 可 ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- ZOJ 3632 Watermelon Full of Water (线段树 区间更新 + dp)
题目大意: 让每天都能吃到西瓜. 最少须要花多少钱. 思路分析: dp[pos] 就表示 要让 前i天每天都有西瓜吃.最少须要花多少钱. 那么假设你买这个西瓜的话. 那么这个西瓜能吃的持续时间都要更 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...
- Codeforces gym 100685 C. Cinderella 水题
C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C ...
随机推荐
- 在centos中使用rpm安装包安装jenkins
jenkins下载:http://pkg.jenkins-ci.org/redhat/ 下载rpm包到本地 在linux下使用rpm包安装命令: sudo rpm -ih jenkins-1.562- ...
- 基于http的追加协议、构建web内容的技术、web的攻击技术(9,10,11)
第九章 基于http的追加协议 用来提升http的瓶颈,比如Ajax技术,spdy等 第十章 构建web内容的技术 html.css.js等 第十一章 web的攻击技术 比如sql注入攻击.xss等.
- 【51NOD-0】1137 矩阵乘法
[算法]简单数学 [题解] 对于A*B=C C中第i行第j列的数字由A中第i行和B中的j列的数字各自相乘后相加得到. 所以两个矩阵能相乘要求A的列数等于B的行数,复杂度为O(n3). #include ...
- 苹果API常用英语名词---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 苹果API常用英语名词0. indicating 决定1.in order to 以便 ...
- Spring总结以及在面试中的一些问题(山东数漫江湖)
1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...
- JQuery-Ajax后台提交数据与获取数据 Java代码
function jqajax(){ var urlName = $("#urlName").val(); var urla = $("#url").val() ...
- 线程,JSP,Servlet面试题
线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...
- ubuntu gnome桌面秀
之前装的是ubuntu14.10gnome版的 然后一路升级成15.04,到今天的15.10 最后发现现在的gnome桌面已经非常适合我的工作了 先放一张图,这是我的桌面 原来使用ubuntu15.0 ...
- bisai.py
比赛专用py #!/usr/etc/env python #encoding:utf-8 #by i3ekr #token import re,os,requests res = "(fla ...
- ARM linux的启动部分源代码简略分析【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/07/2396784.html ARM linux的启动部分源代码简略分析 以友善之臂的mini2 ...