Description
windy在有向图中迷路了。该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。现在给出该有向图,你能告诉windy总共有多少种不同的路径吗?注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。
Input
第一行包含两个整数,N T。接下来有 N 行,每行一个长度为 N 的字符串。第i行第j列为'0'表示从节点i到节点j没有边。为'1'到'9'表示从节点i到节点j需要耗费的时间。
Output
包含一个整数,可能的路径数,这个数可能很大,只需输出这个数除以2009的余数。
Sample Input
【输入样例一】
2 2
11
00
【输入样例二】
5 30
12045
07105
47805
12024
12345
2 2
11
00
【输入样例二】
5 30
12045
07105
47805
12024
12345
Sample Output
【输出样例一】
1
【样例解释一】
0->0->1
【输出样例二】
852
1
【样例解释一】
0->0->1
【输出样例二】
852
HINT
30%的数据,满足 2 <= N <= 5 ; 1 <= T <= 30 。
100%的数据,满足 2 <= N <= 10 ; 1 <= T <= 1000000000 。
显然是矩乘
但是两点之间权值不为1
不过只在1~9
所以一个点拆成9个点转移
program bzoj1297;
const
maxn=2009;
type
mat=array [0..101,0..101] of longint;
var
n,t,i,j,k:longint;
ch:char;
temp,ans,root:mat;
procedure mul (var a,b:mat);
var
i,j,k:longint;
begin
fillchar(temp,sizeof(temp),0);
for i:=1 to n*9 do
for j:=1 to n*9 do
if a[i,j]<>0 then
for k:=1 to n*9 do
temp[i,k]:=(temp[i,k]+a[i,j]*b[j,k]) mod maxn;
a:=temp;
end;
begin
readln(n,t);
for i:=1 to n do
for j:=2 to 9 do
root[i*9-9+j,i*9-9+j-1]:=1;
for i:=1 to n do
begin
for j:=1 to n do
begin
read(ch);
if ch<>'0' then
root[j*9-8,i*9-9+ord(ch)-48]:=1;
end;
readln;
end;
for i:=1 to n*9 do
ans[i,i]:=1;
while t<>0 do
begin
if t and 1 = 1 then mul(ans,root);
t:=t shr 1;
mul(root,root);
end;
writeln(ans[n*9-8,1]);
end.