
给你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 ;
}