这题犯了两个sb错误,写下来,为以后做个警告
一、mul过程中将k作为了循环变量
二、看错了题……
题目中说是数到k行,而我却以为数k遍……
做矩阵乘法,只要记住一句话:置换一定可以写成矩阵的形式!
并且:矩阵满足结合律,不满足交换率!
代码:
1 type matrix=array[0..110,0..110] of longint; 2 var a:array[0..15] of matrix; 3 b,c:matrix; 4 i,j,n,m,k,x:longint; 5 ans,d:array[0..150] of longint; 6 procedure tiao(x:matrix); 7 var i,j:longint; 8 begin 9 for i:=1 to n do 10 begin 11 for j:=1 to n do write(x[i,j]); 12 writeln; 13 end; 14 end; 15 procedure init; 16 begin 17 readln(n,m,k); 18 fillchar(a,sizeof(a),0); 19 fillchar(b,sizeof(b),0); 20 fillchar(c,sizeof(c),0); 21 for i:=1 to m do 22 begin 23 for j:=1 to n do 24 begin 25 read(x); 26 a[i,x,j]:=1; 27 end; 28 readln; 29 end; 30 for i:=1 to n do begin b[i,i]:=1;c[i,i]:=1;end; 31 end; 32 procedure mul(var x,y,z:matrix); 33 var t:matrix; 34 i,j,l:longint; 35 begin 36 fillchar(t,sizeof(t),0); 37 for i:=1 to n do 38 for j:=1 to n do 39 for l:=1 to n do 40 inc(t[i,j],x[i,l]*y[l,j]); 41 z:=t; 42 end; 43 procedure ksm(cs:longint); 44 begin 45 while cs<>0 do 46 begin 47 if cs and 1=1 then mul(b,c,c); 48 cs:=cs>>1; 49 mul(b,b,b); 50 end; 51 end; 52 procedure main; 53 begin 54 for i:=1 to m do mul(b,a[i],b); 55 ksm(k div m); 56 for i:=1 to k mod m do mul(c,a[i],c); 57 for i:=1 to n do d[i]:=i; 58 fillchar(ans,sizeof(ans),0); 59 for i:=1 to n do 60 for j:=1 to n do 61 inc(ans[i],d[j]*c[j,i]); 62 for i:=1 to n do write(ans[i],' '); 63 end; 64 begin 65 init; 66 main; 67 end.