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

博客围绕训练小猫问题展开,Facer为小猫设计了包含三种动作的锻炼方案,小猫需重复执行动作序列m次。输入包含猫的数量、重复次数和动作序列长度等信息,要求输出每只猫最终的花生数。还提到使用矩阵快速幂解决该问题,虽作者运行报错但学到了方法。

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.youkuaiyun.com/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;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值