给你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 【矩阵快速幂】的更多相关文章

  1. zoj 2974 Just Pour the Water矩阵快速幂

    Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB Shirly is a very clever girl. ...

  2. 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 ...

  3. zoj 2974 Just Pour the Water (矩阵快速幂,简单)

    题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...

  4. bnuoj 16493 Just Pour the Water(矩阵快速幂)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...

  5. 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 ...

  6. 焦作网络赛L-Poor God Water【矩阵快速幂】

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  7. dutacm.club Water Problem(矩阵快速幂)

    Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Tot ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)

    https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同:2.若三种食物都有的情况,巧克力不能在中 ...

  9. 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个 ...

随机推荐

  1. bzoj 1046 : [HAOI2007]上升序列 dp

    题目链接 1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3620  Solved: 1236[Submit] ...

  2. linux(ubuntu) 遇到的问题 --1

    1.使用sudo提示用户不在sudoers文件中的解决方法 切换到root用户 su root 查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [root@localhost ~] ...

  3. mybatis foreach where test用法

    <select id="selectAny" resultType="user" parameterType="user"> s ...

  4. Primavera 6.0

    Primavera 6.0(原p3e/c)荟萃了P3软件20年的项目管理精髓和经验,采用最新的IT技术,在大型关系数据库Oracle和MS SQL Server上构架起企业级的.包涵现代项目管理知识体 ...

  5. C++模板:qsort

    void qsort(int l,int r){ int i,j,t,mid; mid=b[(l+r)>>1]; i=l; j=r; do{ while (b[i]<mid) i++ ...

  6. java--String常量池问题的几个例子

    关于string内存分配不错的博客:http://blog.csdn.net/rj042/article/details/6871030 String常量池问题的几个例子 示例1: Java代码 St ...

  7. onpagehide、onunload 和 onpageshow、onload事件疑惑

    今天在最新版本chrome浏览器中测试在什么情况下会触发onpagehide.onunload事件(点击一个链接,刷新页面,提交表单,关闭浏览器等).测试代码粘贴如下: <!DOCTYPE ht ...

  8. Learn Python The Hard Way, 2nd Edition 尾声

    看完了这本书,你决定继续做编程.也许它能成为你的一个职业,也许它能成为你的一项爱好.但你需要一些指导,确保自己不会走错了道路,或帮助你从这个新业余爱好中得到最大的乐趣. 我做了很久的编程.久的你都想象 ...

  9. java线程池分析和应用

    比较 在前面的一些文章里,我们已经讨论了手工创建和管理线程.在实际应用中我们有的时候也会经常听到线程池这个概念.在这里,我们可以先针对手工创建管理线程和通过线程池来管理做一个比较.通常,我们如果手工创 ...

  10. Eclipse之报错信息及其解决方案

    一.有很多人都喜欢开发js的时候用aptana,因此在eclipse中集成aptana插件是必须的,可是,在用link方式在eclipse中安装好aptana后,启动时会报如下错误 An intern ...