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-7      ;
const int N = 22             ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 100000      ;
const int INF = 0x3f3f3f3f   ;
const int MAX = 10500        ;

double a[22];
int n, m;

struct Mat {
    double mat[N][N];
};

Mat operator * (Mat a, Mat b){
    Mat c;
    memset(c.mat, 0, sizeof(c.mat));
    for(int k = 1; k <= n; ++k){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; 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 = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            c.mat[i][j] = (i == j); //init
        }
    }
    for(; k; k >>= 1){
        if(k & 1)   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 = 0;
    int num;
    cin >> t;
    while(t--){
        Mat real;
        memset(real.mat, 0, sizeof(real.mat));
        cin >> n;
        for(i = 1; i <= n; ++i){
            cin >> a[i];
        }
        for(i = 1; i <= n; ++i){
            cin >> k;
            real.mat[i][i] = (double)1;
            for(j = 1; j <= k; ++j){
                cin >> num;
                real.mat[i][num] += (double)1 / (double)k;
                real.mat[i][i] -= (double)1 / (double)k;
            }
        }
        cin >> m;

        real = real ^ m;

        double ans[22];
        memset(ans, 0, sizeof(ans));
        for(i = 1; i <= n; ++i){
            for(j = 1; j <= n; ++j){
                ans[j] += a[i] * real.mat[i][j];
            }
        }
        for(i = 1; i <= n; ++i){
            if(i != n){
                printf("%.2f ",ans[i]);
            } else{
                printf("%.2f\n",ans[i]);
            }
        }
    }

    return 0;
}
时间: 2024-08-05 15:24:33

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

【ZOJ 2974】Just Pour the Water(矩阵快速幂)

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均分入所指定的杯子 进行x次后 输出杯子剩余水量 刚拿到这个题,第一反应是递推找规律,但是因为每个杯子的初始水量是未知的,所以能找的只是每个杯子水量与其余杯子水量的关系. 但是看到了操作次数巨大,而且最多只有20个杯子,感觉可以用快速幂去做. 我们假设矩阵a[i][j]代表第i个杯子的水有a[i][j

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

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 3 c

zoj 3538 Arrange the Schedule(矩阵快速幂)

Arrange the Schedule Time Limit: 1 Second      Memory Limit: 65536 KB In Summer 2011, the ZJU-ICPC Team has a n-days training schedule. ZJU-ICPC Team has been divided into 4 Group: Akiba, BiliBili, CIA, Double(Group A, B, C, D). There is a group in c

ZOJ 3256 Tour in the Castle 矩阵快速幂加速

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3256 题意:给一个n*m的棋盘,求从左上到左下的经过所有格子的方案数 在左边加一列问题就变成了求回路 由于m很大,所以我们需要按列dp 构造出矩阵后,用矩阵快速幂加速 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include&l

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

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

zoj 2853 Evolution(矩阵快速幂)

Evolution is a long, long process with extreme complexity and involves many species. Dr. C. P. Lottery is currently investigating a simplified model of evolution: consider that we haveN (2 <= N <= 200) species in the whole process of evolution, inde

ZOJ 3690 &amp; HDU 3658 (矩阵快速幂+公式递推)

ZOJ 3690 题意: 有n个人和m个数和一个k,现在每个人可以选择一个数,如果相邻的两个人选择相同的数,那么这个数要大于k 求选择方案数. 思路: 打表推了很久的公式都没推出来什么可行解,好不容易有了想法结果WA到天荒地老也无法AC.. 于是学习了下正规的做法,恍然大悟. 这道题应该用递推 + 矩阵快速幂. 我们设F(n) = 有n个人,第n个人选择的数大于k的方案数: G(n) = 有n个人,第n个人选择的数小于等于k的方案数: 那么递推关系式即是: F(1)=m?k,G(1)=k F(n

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

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33