Swamp crocodile (matrix multiplication)

Source: Internet
Author: User

ZJTSC Swamp crocodile

Description

The Pantanal Marsh, known as the world's largest wetland, is in the southern part of Mato Grosso, central Brazil. Whenever the rainy season comes, here the blue waves, vitality, attracting many tourists.
In order to make the play more interesting, people in the center of the pond built several dun foping and stone bridges, each stone bridge connected to two Dun foping, and every two dun foping between at most only one stone bridge. This site has not dared to open up after its creation, because there are many dangerous piranhas in the pond.
Mr. Bean had a passion for adventure, and as soon as he heard the news, he rushed to the pond and wanted to be the first person to travel on a bridge. Although the beans love adventure, but also dare not to make jokes on their lives, so he began a careful field survey, and got some surprising conclusion: Piranha's travel route is cyclical, this cycle can only be 2,3 or 4 units of time. In each unit of time, piranhas can swim from one dun foping to another dun foping. Each to a dun foping, if the person above it will carry out an attack, otherwise continue its periodic movement. If not to Dun foping, it will not attack people.
With the aid of advanced instruments, the beans quickly figured out the laws of all piranhas, and he began to design his own course of action. Every unit of time, he can only follow the stone bridge from one dun foping to another Dun foping, and can not stop in a dun foping, because standing motionless there will be other dangers. If peas and a piranha arrive at a dun foping at the same time, they will be attacked by piranhas, which he certainly does not want to happen.
Now that the beans have been selected two Dun foping start and end, he wants to start from start, after K units of time to stand on the Dun foping end. Assuming that the DUN foping can be repeated (including start and end), he would ask you to help calculate how many of these routes (of course not being attacked by piranhas).

Input

Input file Total m+ 2 + nfish line.
The first line contains five positive integers n,m,start,end and K, each representing the number of Dun foping, the number of stone bridges, the number of Start dun foping and end Dun foping, and the unit time required for a route. The Dun foping is numbered with an integer of 0 to n–1.
2nd to m+ 1 line, give the relevant information of the Stone Bridge. Each line of two integers x and y,0≤x, y≤n–1, indicates that the stone bridge is connected to two Dun foping, numbered x and Y.
Line m+ 2 is an integer nfish that represents the number of piranhas.
Section m+ 3 to M + 2 + nfish line, each row gives information about a piranha. The first integer of each line is t,t= 2,3 or 4, which represents the piranha's cycle of movement. Next there is the number of T, which represents the path of the piranha during a cycle.
If t=2, next there are 2 numbers P0 and P1, piranha from P0 to P1, from P1 to P0, ... ;
If t=3, then there are 3 numbers of p0,p1 and P2, piranha from P0 to P1, P1 to P2, P2 to P0, ... ;
If t=4, then there are 4 numbers of p0,p1,p2 and P3, piranhas from P0 to P1, P1 to P2, P2 to P3, P3 to P0, ...
When the beans start, all piranhas are on their route P0 position, please rest assured that this position will not be the start Dun foping.

Output

Output the number of routes, because this number can be large, you just output that number divided by the remainder of 10000

Sample Input

6 8 1) 5 3
0 2
2 1
1 0
0 5
5 1
1 4
4 3
3 5
1
3 0 5 1

Sample Output

2
"Sample description"
Moment 0 1 2 3
Fish position 0 5 1 0
Route 11 2 0 5
Route 21 4 3 5

Convention
1≤n≤50
1≤k≤2,000,000,000
1≤nfish≤20

It looks like a search. But K is too big, O (k) will time out, but there are not many columns (if too much of the matrix will be too large, burst memory), you can use matrix multiplication.

The fish cycle is only 2,3,4, so a period of up to 12 steps. We can multiply the adjacency matrix of each moment to know the number of paths. When A[i,k]>0,b[k,j]>0, that is, both i->k and k->j have paths c[i,j] have the path and are equal to the actual i-j path.

A[i] represents the adjacency matrix for the first moment of time, Ans[start,end] is a request. So Ans=a[1]*a[2]*a[3]*.....*a[k]

We know that 12 steps a cycle,

a[1]*a[2]*......*a[12]=a[12p+1]*a[12p+2]*......*a[12p+12] (p is any positive integer)

So

ans= (a[1]*a[2]*......*a[12]) k div * (a[1]*a[2]*......*a[k MoD))

 

We can find the G K div(g=a[1]*a[2]*......*a[12]) with a fast power, and the time complexity is O (log n)

Code:

· Const
· p=10000;
·
· Type
· ARR=ARRAY[1..50,1..50] of Longint;
·
· Var
· N,m,s,e,t,w1,nf,x,y,i,j,k,u:longint;
· A,c,d:arr;
· F:ARRAY[1..12] of arr;
· W:ARRAY[1..4] of Longint;
·
· Procedure Cheng (A,b:arr);
· Var
· I,j,k:longint;
· Begin
· Fillchar (C,sizeof (c), 0);
· For I:=1 to N do
· For J:=1 to N do
· For K:=1 to N do
· C[i,j]:= (c[i,j]+a[i,k]*b[k,j]) mod p;
· End
·
· Procedure KSM (X:longint);
· Begin
· If X=0 then exit;
· KSM (x Div 2);
· Cheng (c,c);
· If x mod 2=1 then Cheng (c,d);
· End
·
· Begin
· READLN (n,m,s,e,t);
· For I:=1 to M do
· Begin
· READLN (x, y);
· A[x+1,y+1]:=1; (Because the stone pillars are 0~n-1, plus 1 is a better calculation)
· A[y+1,x+1]:=1;
· End
· READLN (NF);
· For I:=1 to
· F[i]:=a;
· For I:=1-NF do
· Begin
· Read (W1);
· For J:=1 to W1 do
· Read (W[j]);
· For J:=1 to
· U:=j MoD w1+1;
· For K:=1 to N do
· f[j,k,w[u]+1]:=0;
· End
· End
· For I:=1 to N do
· C[i,i]:=1;
· For I:=1 to
· Cheng (c,f[i]);
· D:=c;
· Fillchar (C,sizeof (c), 0);
· For I:=1 to N do
· C[i,i]:=1;
· KSM (T div 12);
· For I:=1 to T MoD
· Cheng (c,f[i]);
· Writeln (c[s+1,e+1]);
· End.
·


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.