ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水
我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放
然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M <= 1,000,000,000) 的情况下使用矩阵快速幂再好不过
这到题目注意的一点是,得使用Double 变量,如果使用FLoat会导致Wrong Answer
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ;
const int MAX = ; double a[];
int n, m; struct Mat {
double mat[N][N];
}; Mat operator * (Mat a, Mat b){
Mat c;
memset(c.mat, , sizeof(c.mat));
for(int k = ; k <= n; ++k){
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j){
c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
}
}
}
return c;
} Mat operator ^ (Mat a, int k){
Mat c;
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j){
c.mat[i][j] = (i == j); //init
}
}
for(; k; k >>= ){
if(k & ) c = c * a;
a = a * a;
}
return c;
} int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, x, y, numCase = ;
int num;
cin >> t;
while(t--){
Mat real;
memset(real.mat, , sizeof(real.mat));
cin >> n;
for(i = ; i <= n; ++i){
cin >> a[i];
}
for(i = ; i <= n; ++i){
cin >> k;
real.mat[i][i] = (double);
for(j = ; j <= k; ++j){
cin >> num;
real.mat[i][num] += (double) / (double)k;
real.mat[i][i] -= (double) / (double)k;
}
}
cin >> m; real = real ^ m; double ans[];
memset(ans, , sizeof(ans));
for(i = ; i <= n; ++i){
for(j = ; j <= n; ++j){
ans[j] += a[i] * real.mat[i][j];
}
}
for(i = ; i <= n; ++i){
if(i != n){
printf("%.2f ",ans[i]);
} else{
printf("%.2f\n",ans[i]);
}
}
} return ;
}
ZOJ 2794 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. ...
- 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 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
- 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.若三种食物都有的情况,巧克力不能在中 ...
- ZOJ - 2853 Evolution 线性变换变成矩阵快速幂
题意:给你N个数,1~N分别为num[i], 以及T个 (i,j,P) 对于每组(i,j,P),让你将 num[i] 减去 P*num[i] 再把 P*num[i] 加到 num[j] 上.T个 ...
随机推荐
- A Byte of Python 笔记(9) 面向对象编程
第11章 面向对象编程 面向过程:根据操作数据的函数或语句块来设计程序. 面向对象(OOP, object-oriented programming):把数据和功能结合起来,用对象包裹组织程序. 类 ...
- Oracle SQL篇(三)Oracle ROWNUM 与TOP N分析
首先我们来看一下ROWNUM: 含义解释: 1.rownum是oracle为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推.这是一个伪列,可以用于限制查询返回的总行数. 2 ...
- cocos2dx中的精灵CCSprite
什么是精灵(CCSprite),在官网文档中是这么定义的 Sprites A cocos2d CCSprite is similar to sprites you find in other game ...
- Hello China操作系统STM32移植指南(二)
移植步骤详解 下面就以MDK 4.72为开发环境,详细说明Hello China内核向STM32的移植过程.MDK 4.72评估版只支持32K代码的编译,这对Hello China的内核来说,裁剪掉一 ...
- [Windows编程] 使用AttachThreadInput 来捕捉其它窗口的键盘输入
在一些情况下(比如屏幕软键盘或者输入法程序),自己的窗口没有输入焦点但是想要当前焦点窗口的键盘输入消息,可以使用Win32 API函数AttachThreadInput()来解决这个问题.Attach ...
- ZYB's Premutation(有逆序数输出原序列,线段树)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Linux新手笔记 ibus
centos 6.4 32 笔记 一.安装gccyum install gcc ======================================================== ...
- Oracle包Package调用Package
Package左侧文件: create or replace package CALL_DETAILS is strdatarange varchar2(1) := '1'; numrow_num n ...
- CSS的clear属性
以下引用自w3school clear 属性定义了元素的哪边上不允许出现浮动元素.在 CSS1 和 CSS2 中,这是通过自动为清除元素(即设置了 clear 属性的元素)增加上外边距实现的.在 CS ...
- js基础之冒号
js中冒号的作用有两种, 转自:http://www.kqiqi.com/knowledge/web-based/506.html 一.?: 操作符 a = expression ? b : c;如果 ...