#include
<
cstdio
>
#include
<
cstring
>
using
namespace
std;
typedef
int
Arr[
55
][
55
];
int
N;
Arr A,B,C;
int
M,s,t,MOD;
void
Mul(Arr
&
a,Arr b)
{
Arr c; memset(c,
0
,
sizeof
(c));
for
(
int
i
=
1
;i
<=
N;
++
i)
for
(
int
j
=
1
;j
<=
N;
++
j)
for
(
int
k
=
1
;k
<=
N;
++
k)
c[i][j]
=
(c[i][j]
+
(a[i][k]
*
b[k][j])
%
MOD)
%
MOD;
memcpy(a,c,
sizeof
(c));
}
void
MulA(Arr
&
a,Arr b)
{
Arr c; memset(c,
0
,
sizeof
(c));
for
(
int
i
=
1
;i
<=
1
;
++
i)
for
(
int
j
=
1
;j
<=
N;
++
j)
for
(
int
k
=
1
;k
<=
N;
++
k)
c[i][j]
=
(c[i][j]
+
(a[i][k]
*
b[k][j])
%
MOD)
%
MOD;
memcpy(a,c,
sizeof
(c));
}
int
main()
{
scanf(
"
%d
"
,
&
N);
for
(
int
i
=
1
;i
<=
N;
++
i)
for
(
int
j
=
1
;j
<=
N;
++
j)
scanf(
"
%d
"
,
&
B[i][j]);
scanf(
"
%d%d%d%d
"
,
&
M,
&
s,
&
t,
&
MOD);
memset(A,
0
,
sizeof
(A));
memcpy(C,B,
sizeof
(B));
M
--
;
while
(M){
if
(M
&
1
) Mul(B,C);
Mul(C,C);
M
>>=
1
;
}
A[
1
][s]
=
1
;
MulA(A,B);
printf(
"
%d\n
"
,A[
1
][t]);
return
0
;
}
posted on 2009-08-11 21:24
xfstart07 阅读(163)
评论(0) 编辑 收藏 引用