poj 3745 Training little cats (矩阵运算+矩阵快速幂)

时间:2021-03-15 22:13:51

poj 3745 Training little cats (矩阵运算+矩阵快速幂)
Time Limit: 2000ms Memory Limit: 65536kB

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 n, m 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


首先注意到重复次数很大,所以肯定有数学算法。
看看操作,全部是线性操作,所以可以用向量的线性变换处理这些操作,唯一有点问题的是有一个加一的非其次操作,所以给花生数量向量进行增广,增加一个非其次维度即可。
那么对于 x=[1,x1,x2,...,xn]T,xii
x=Am[1,0,0...,0]T
A可以用简单的线性变换转化为矩阵运算的技术求出。
再用一次快速幂即可。


Accepted    3840kB  812ms   1824 B  G++
#include<stdio.h>

const int N=101;
const int POWER=50;

typedef long long data_t;

data_t A[N][N];
data_t AP[POWER][N][N];
/*
x'=Ax
x[0] is always 1,
x[i] is the peanuts cat i owns.
*/

void copyto(data_t A[N][N],data_t B[N][N],int n);
void mul(data_t A[N][N],data_t B[N][N],int n);
void power(data_t A[N][N],int n,int m);
void init(data_t A[N][N],int n);

int main()
{
//freopen("in.txt","r",stdin);
char cmd;
int n,m,k,x,y,temp;
while (scanf("%d%d%d\n",&n,&m,&k)&&n)
{
init(A,n);
while (k--)
{
scanf("%c",&cmd);
if (cmd=='e')
{
scanf("%d\n",&x);
for (int i=0;i<=n;i++)
A[x][i]=0;
}
else if (cmd=='s')
{
scanf("%d%d\n",&x,&y);
for (int i=0;i<=n;i++)
{
temp=A[x][i];
A[x][i]=A[y][i];
A[y][i]=temp;
}
}
else if (cmd=='g')
{
scanf("%d\n",&x);
A[x][0]++;
}
}
power(A,n,m);
for (int i=1;i<=n;i++)
printf("%lld%c",A[i][0],i==n?'\n':' ');
}
return 0;
}

void mul(data_t A[N][N],data_t B[N][N],int n)
{
static data_t A0[N][N];
for (int i=0;i<=n;i++)
for (int j=0;j<=n;j++)
A0[i][j]=A[i][j];
for (int i=0;i<=n;i++)
for (int j=0;j<=n;j++)
{
A[i][j]=0;
for (int k=0;k<=n;k++)
A[i][j]+=A0[i][k]*B[k][j];
}
return;
}

void power(data_t A[N][N],int n,int m)
{
int p=0;
data_t power=1;
copyto(A,AP[0],n);
while (power<(data_t) m)
{
power*=2;
p++;
copyto(AP[p-1],AP[p],n);
mul(AP[p],AP[p-1],n);
}
init(A,n);
for (int i=0;i<=p;i++)
if (m&(1<<i))
mul(A,AP[i],n);
return;
}

void copyto(data_t A[N][N],data_t B[N][N],int n)
{
for (int i=0;i<=n;i++)
for (int j=0;j<=n;j++)
B[i][j]=A[i][j];
return;
}

void init(data_t A[N][N],int n)
{
for (int i=0;i<=n;i++)
for (int j=i+1;j<=n;j++)
A[i][j]=A[j][i]=(data_t) 0;
for (int i=0;i<=n;i++)
A[i][i]=(data_t) 1;
return;
}