poj 3735 Training little cats(构造矩阵)

http://poj.org/problem?id=3735

大致题意:

有n只猫,开始时每只猫有花生0颗,现有一组操作,由下面三个中的k个操作组成:

1. g i 给i只猫一颗花生米

2. e i 让第i只猫吃掉它拥有的所有花生米

3. s i j 将猫i与猫j的拥有的花生米交换

现将上述一组操作循环m次后,问每只猫有多少颗花生?

再一次感受到了矩阵的强大。。。循环m次,且m这么大,很容易想到构造转换矩阵,但如何构造是个问题。尤其是第一种操作,第i只猫增加一个花生。具体构造方法是把矩阵扩大为(n+1)*(n+1)的,初始化为单位矩阵,g i: a.mat[0][i] += 1; e  i :第i列全置为0; s i j: 第i、j列元素互换

这样形成转换矩阵后,循环m次,用矩阵快速幂求得,最后取矩阵第0行的1~n就是答案。

点击打开链接 学习了。。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8
#define PI acos(-1.0)

using namespace std;
const int maxn = 105;

int n,k,m;

struct matrix
{
	_LL mat[maxn][maxn];
	//初始化为单位矩阵
	void init()
	{
		memset(mat,0,sizeof(mat));
		for(int i = 0; i <= 102; i++) //不知道为什么,102换成maxn就会崩,求解。
			mat[i][i] = 1;
	}
}a,res;

//矩阵相乘
matrix matrixMul(matrix x, matrix y)
{
	matrix tmp;
	memset(tmp.mat,0,sizeof(tmp.mat));

	for(int i = 0; i <= n; i++)
	{
		for(int k = 0; k <= n; k++)
		{
			if(x.mat[i][k] == 0) continue;
			for(int j = 0; j <= n; j++)
			{
				tmp.mat[i][j] += x.mat[i][k] * y.mat[k][j];
			}
		}
	}
	return tmp;
}

//矩阵求幂
matrix Mul(matrix x, int k)
{

	matrix tmp;
	tmp.init();

	while(k)
	{
		if(k & 1)
			tmp = matrixMul(tmp,x);
		x = matrixMul(x,x);
		k >>= 1;
	}
	return tmp;
}

int main()
{
	char ch[4];

	while(~scanf("%d %d %d",&n,&m,&k))
	{
		if(n == 0 && k == 0 && m == 0) break;
		a.init();

        int num1,num2;
        while(k--)
        {
            scanf("%s",ch);
            if(ch[0] == 'g')
            {
                scanf("%d",&num1);
                a.mat[0][num1] += 1;
            }
            else if(ch[0] == 'e')
            {
                scanf("%d",&num1);
                for(int i = 0; i <= n; i++)
                    a.mat[i][num1] = 0;
            }
            else
            {
                scanf("%d %d",&num1,&num2);
                for(int i = 0; i <= n; i++)
                    swap(a.mat[i][num1],a.mat[i][num2]);
            }
        }
		if(m == 0)
		{
			for(int i = 0; i < n;i++)
			{
				if(i == 0)printf("0");
				else printf(" 0");
			}
            printf("\n");
            continue ;
		}
        res = Mul(a,m);
        for(int i = 1; i < n; i++)
			printf("%I64d ",res.mat[0][i]);
		printf("%I64d\n",res.mat[0][n]);
	}

	return 0;
}

poj 3735 Training little cats(构造矩阵)

时间: 2024-12-27 23:32:34

poj 3735 Training little cats(构造矩阵)的相关文章

[POJ 3735] Training little cats (构造矩阵、矩阵快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9613   Accepted: 2296 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make th

poj 3735 Training little cats(矩阵快速幂)

Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed a set of moves for his cats. He is now asking you t

poj 3735 Training little cats(矩阵构造,快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10350   Accepted: 2471 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make t

poj 3735 Training little cats (矩阵快速幂)

题目链接: http://poj.org/problem?id=3735 题意: 有n只猫咪,开始时每只猫咪有花生0颗,现有一组操作,由下面三个中的k个操作组成:               1. g i 给i只猫咪一颗花生米               2. e i 让第i只猫咪吃掉它拥有的所有花生米               3. s i j 将猫咪i与猫咪j的拥有的花生米交换 现将上述一组操作做m次后,问每只猫咪有多少颗花生? 思路: http://www.cnblogs.com/acS

poj 3735 Training little cats 矩阵

假设n=3 构造矩阵[1,0,0,0] 对于g 1操作,构造矩阵(0行i列++) 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 对于e 1操作,构造矩阵 (i整列清空) 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 对于s 1 2操作,构造矩阵 (i,j整列交换) 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 将k次操作依次按上述构造矩阵,得到一个轮回的转置矩阵.做m次快速幂就行了. 最坑的地方在于,答案要用longlong存,而longlo

矩阵快速幂 POJ 3735 Training little cats

题目传送门 1 /* 2 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 3 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 4 自己讲太麻烦了,网上有人讲的很清楚,膜拜之 5 详细解释:http://www.cppblog.com/y346491470/articles/157284.html 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #inc

POJ 3735 Training little cats 矩阵快速幂应用

点击打开链接 Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9807   Accepted: 2344 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to

POJ 3735 Training little cats (矩阵快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10593   Accepted: 2532 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make t

poj 3735 Training little cats矩阵快速幂

Training little cats Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed a set of moves for his cats. H