Evolution(矩阵快速幂)

Evolution


Time Limit: 5 Seconds      Memory Limit: 32768 KB

Description

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 have N (2 <= N <= 200) species in the whole process of evolution, indexed from 0 to N-1, and there is exactly one ultimate species indexed as N-1. In addition, Dr. Lottery divides the whole evolution process into M (2 <= M<= 100000) sub-processes. Dr. Lottery also gives an ‘evolution rate‘ P(i, j) for 2 species i and j, where i and j are not the same, which means that in an evolution sub-process, P(i, j) of the population of species i will transform to species j, while the other part remains unchanged.

Given the initial population of all species, write a program for Dr. Lottery to determine the population of the ultimate species after the evolution process. Round your final result to an integer.

Input

The input contains multiple test cases!

Each test case begins with a line with two integers NM. After that, there will be a line with N numbers, indicating the initial population of each species, then there will be a number T and T lines follow, each line is in format "i j P(i,j)" (0 <= P(i,j) <=1).

A line with N = 0 and M = 0 signals the end of the input, which should not be proceed.

Output

For each test case, output the rounded-to-integer population of the ultimate species after the whole evolution process. Write your answer to each test case in a single line.

Notes

  • There will be no ‘circle‘s in the evolution process.
  • E.g. for each species i, there will never be a path i, s1, s2, ..., st, i, such that P(i,s1) <> 0, P(sx,sx+1) <> 0 and P(st, i) <> 0.
  • The initial population of each species will not exceed 100,000,000.
  • There‘re totally about 5 large (N >= 150) test cases in the input.

Example

Let‘s assume that P(0, 1) = P(1, 2) = 1, and at the beginning of a sub-process, the populations of 0, 1, 2 are 40, 20 and 10 respectively, then at the end of the sub-process, the populations are 0, 40 and 30 respectively.

Sample Input

2 3
100 20
1
0 1 1.0
4 100
1000 2000 3000 0
3
0 1 0.19
1 2 0.05
0 2 0.67
0 0

Sample Output

120
0

题意:就是说物种进化,有N种物种,编号是0-----N-1,M次进化后,问你编号为N-1的物种有多少数量;

其中要注意的就是i物种进化到j物种的概率是p;(那么剩下的不要忘了);所以单位矩阵初始化对角线的值为1,

然后根据题目进化的概率进行加减;比如P(i, j) = 0.3,则mat[i][j] += 0.3,mat[i][i] -= 0.3;

把题目转化为数学模型,分析后就是 有一个初始序列, 有一个进化率矩阵,求的是初始序列 与进化率矩阵进

行 m 次运算后, 初始序列最后一位的答案,那么显然,可以对进化率矩阵进行快速幂计算

  这里,有一点是要注意的 由于矩阵的大小是200*200的但是一般的笔记本内存不够,至少我的笔记本不

行。。。。。以至于本人纠结了N久,,,谁让题目是服务器改的呢。。。。!<Memory Limit: 32768 KB>

内存就是大!

不说了,其他的都是比较基础的。

转载请注明出处:http://www.cnblogs.com/yuyixingkong/p/4326818.html

题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int N,M;struct matrix
{
    double mat[210][210];
};

double num[210];

matrix multiply(matrix x,matrix y)//乘
{
    matrix temp;
    memset(temp.mat,0,sizeof(temp.mat));
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
        {
            if(x.mat[i][j]==0) continue;
            for(int k=0; k<N; k++)
            {
                if(y.mat[j][k]==0) continue;
                temp.mat[i][k]+=x.mat[i][j]*y.mat[j][k];
            }
        }
    }
    return temp;
}

matrix quicklymod(matrix a,int n)
{
    matrix res;//单位阵
    memset(res.mat,0,sizeof(res.mat));
    for(int i=0;i<N;i++) res.mat[i][i]=1;
    while(n)
    {
        if(n&1)
            res=multiply(res,a);
        n>>=1;
        a=multiply(a,a);
    }
    /*for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
                printf("%5.2lf",a.mat[i][j]);
            printf("\n");
        }
        printf("\n");*/
    return res;
}
int main()
{
    int i,j,T;
    double p,ans;
    matrix ant;
    while(scanf("%d%d",&N,&M),(N||M))
    {
        for(i=0;i<N;i++)
        {
            scanf("%lf",&num[i]);
            for(j=0;j<N;j++)
            {
                if(i==j)
                    ant.mat[i][j]=1;
                else
                    ant.mat[i][j]=0;
            }
        }

        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%lf",&i,&j,&p);
            ant.mat[i][j]+=p;
            ant.mat[i][i]-=p;
        }

        ant=quicklymod(ant,M);

        ans=0;
        for(i=0;i<N;i++)
        {
            ans+=num[i]*ant.mat[i][N-1];
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}

/*
2 3
100 20
1
0 1 1.0
4 100
1000 2000 3000 0
3
0 1 0.19
1 2 0.05
0 2 0.67
0 0
*/
时间: 2024-12-26 09:57:06

Evolution(矩阵快速幂)的相关文章

zoj 2853 Evolution 矩阵快速幂

在我的电脑上code::blocks运行过不了,但代码是可以AC的,很是郁闷. 问了大神,知道了函数的参数是放在栈区,结构体太大的话,栈就爆了,如是后来就听从大神的意见用引用改写了. 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853 #include <stdio.h> #include <string.h> #include <stdlib.h> int Len_Matrix; in

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

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...