【矩阵快速幂专题】POJ 3735 Training little cats

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qian2213762498/article/details/82185527

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

Training little cats

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15535   Accepted: 3829

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

Source

PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Facer

矩阵快速幂。

参考链接:https://blog.csdn.net/KEYboarderQQ/article/details/52770671

我也想AC,可是Runtime Error,方法毕竟学到了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll nmax=105;
const ll MOD=1e9;
#define mod(x) ((x)%MOD)
int n,mm,k;//猫数,次数mm,变换次数k 
 
struct mat{
	ll m[nmax][nmax];
}unit;
 
mat operator *(mat a,mat b){
	mat ret;
	ll x;
	for(ll i=0;i<=n;i++){//<=n
		for(ll j=0;j<=n;j++){
			x=0;
			if(a.m[i][j]){
				for(ll k=0;k<=n;k++){
					x+=mod((ll)a.m[i][j]*b.m[j][k]);
				}
				ret.m[i][k]=mod(x);
			}
		}
	}
	return ret;
}
 
void init_unit(){
	for(ll i=0;i<n;i++){
		unit.m[i][i]=1;
	} 
	return;
}
mat pow_mat(mat a,ll n){//求矩阵a的n次幂 
	mat ret=unit;
	while(n){
		if(n&1) ret=ret*a;
		a=a*a;
		n>>=1;
	} 
	return ret;
}
mat solve(){
	mat b;
	memset(b.m,0,sizeof(b.m));
	for(int i=0;i<=n;i++){
		b.m[i][i]=1;
	}
	char s[5];
	int x,y;
	for(int i=0;i<=k;i++){//k次变换 
		scanf("%s",s);
		if(s[0]=='g'){//得到1个花生 
			scanf("%d",&x);
			b.m[0][x]++; 
		}
		else if(s[0]=='e'){//吃掉所有花生 
			scanf("%d",&x);
			for(int i=0;i<=n;i++){
				b.m[i][x]=0;//清零该列 
			}
		}
		else if(s[0]=='s'){//交换两只猫的花生 
			scanf("%d",&x,&y);
			for(int i=0;i<=n;i++){
				swap(b.m[i][x],b.m[i][y]);//交换两列 
			}
		}
		
	}
	return b;
	
}

int main(int argc, char** argv) {
	//init_unit();
	while(true){
		scanf("%lld %lld %lld",&n,&mm,&k);
		if(n==0&&mm==0&&k==0){
			break;
		}
		mat b=solve();
		b=pow_mat(b,mm);//b^mm
		mat a;
		memset(a.m,0,sizeof(a.m));
		a.m[0][0]=1; //[1,0,0,...0]
		a=a*b;
		for(int i=1;i<=n;i++){//越过0 
			printf("%lld",a.m[0][i]);
			if(i!=n){
				printf(" ");
			}
		} 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qian2213762498/article/details/82185527
今日推荐