POJ3735 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 to supervise the cats to do his exercises. Facer's great exercise for cats contains three different moves:
g i : Let the ith cat take a peanut.
e i : Let the ith cat eat all peanuts it have.
s i j : Let the ith cat and jth cat exchange their peanuts.
All the cats perform a sequence of these moves and must repeat it m times! Poor cats! Only Facer can come up with such embarrassing idea. 
You have to determine the final number of peanuts each cat have, and directly give them the exact quantity in order to save them.

Input

The input file consists of multiple test cases, ending with three zeroes "0 0 0". For each test case, three integers nm and k are given firstly, where n is the number of cats and k is the length of the move sequence. The following k lines describe the sequence.
(m≤1,000,000,000, n≤100, k≤100)

Output

For each test case, output n numbers in a single line, representing the numbers of peanuts the cats have.

Sample Input

3 1 6
g 1
g 2
g 2
s 1 2
g 3
e 2
0 0 0

Sample Output

2 0 1

 

题意:有n只猫咪,开始时每只猫有花生米0颗,现给出k组操作,将该组操作做m(m<=10^9)次后,问每只猫咪有多少颗花生米。其中:

 

g i 表示第i只猫得到一个花生
e i 表示第i只猫吃掉所有花生

s i j 表示第i只猫和第j只猫交换花生。

分析:数据量大,采用矩阵快速幂。先构造单位矩阵,根据每组操作分别构造矩阵进行连乘,再计算矩阵的M次方。n只猫要用n+1维矩阵,最后一列记录猫的花生数。矩阵乘法复杂度为O(N^3),需要对稀疏矩阵进行优化。M<10^9,花生数可能会超过int取值范围,需要用long long类型。

 

#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
const int maxn=105;
int N,M,K;
struct matrix{
	ll v[maxn][maxn];
	matrix(){memset(v,0,sizeof(v));}
	
	matrix operator*(matrix& b)const
	{
		matrix res;
		for(int i=0;i<=N;i++)
			for(int j=0;j<=N;j++)
				if(v[i][j])//对稀疏矩阵优化 
					for(int k=0;k<=N;k++)
					{
						res.v[i][k]+=v[i][j]*b.v[j][k];				
					}
		return res;
	}
	
}; 

matrix pow(matrix a,int m)
{
	matrix res;
	for(int i=0;i<=N;i++)
		res.v[i][i]=1;
	while(m>0)
	{
		if(m&1)	
			res=res*a;
		a=a*a;
		m>>=1; 
	}
	return res;
}
int main()
{
	while(cin>>N>>M>>K)
	{
		if(N==0&&M==0&&K==0) break;
		matrix ans;
		//初始化为单位矩阵 
		for(int i=0;i<=N;i++)
			ans.v[i][i]=1;
		while(K--)
		{
			char move;
			int a,b;
			scanf(" %c",&move);
			switch(move)
			{
				case 'g':
					scanf("%d",&a);
					ans.v[a-1][N]++;
					break;
				case 's'://两行交换 
					scanf("%d%d",&a,&b);
					for(int i=0;i<=N;i++)
						swap(ans.v[a-1][i],ans.v[b-1][i]);
					break;
				case 'e':
					scanf("%d",&a);
					for(int i=0;i<=N;i++)
						ans.v[a-1][i]=0;
					break;
				default:break;
					
			}
		}
		ans=pow(ans,M);//计算M次方 
		for(int i=0;i<N;i++)
			printf("%lld ",ans.v[i][N]);
		cout<<endl;
			
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值