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个 ...
随机推荐
- javamail发送二进制流附件的问题
最近做个邮件发送功能,要内嵌图片并有附件. 需求很奇怪,图片和附件文件是放在ftp服务器上的,查了下javamail的文档. 添加附件方法如下 MimeBodyPart messageBodyPart ...
- mybatis一对一关联表查询
先创建一个表 CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name ) ); CREATE TABLE class( c_ ...
- java牛人给新人的几点建议
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过的1 ...
- 新唐M0特点分析
1,价格低,05x系列0.6-1.5美金,1xx系列1.5-3.5美金:2,性能好,最新32位CORTEX-M0的ARM核,唯一可工作到+5.5V的CORTEX-M0:3,速度快,CPU核能跑到50M ...
- 通往WinDbg的捷径
通往WinDbg的捷径(一) 原文:http://www.debuginfo.com/articles/easywindbg.html译者:arhat时间:2006年4月13日关键词:CDB WinD ...
- linux-0.11抠代码-GDB+VMWARE
vmware新建一个虚拟机,硬盘为0.1G,建立完成后要先启动一次虚拟机,此时无任何系统,然后再关闭,应该会多出一个ostest-flat.vmdk这个虚拟磁盘文件,下面要用到 新建完成后 我的虚拟机 ...
- [置顶] SOLR 4.4 部署
SOLR 4.4 部署 前言:近期研究下solr4.4的部署,一下是部署步骤,与大家分享下. 下载solr4.4.0.zip 地址 http://mirror.esocc.com/apa ...
- php 登录实例演示
<pre name="code" class="python">一.模板的使用 (重点) a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主 ...
- Win32 GDI基础(笔记)
1.GDI名字的意义 GDI Graphic Device Interface,我说不清和GUI有什么区别.可能一种针对设备,一种针对用户而言吧,反正以后都说GDI,也就是Windows的图形编程. ...
- JAVA GUI学习 - 总结
一:项目 二:重要组件补充 三:组件高级操作