"BZOJ1875" "Matrix multiplication" [sdoi2009]hh go for a walk

Source: Internet
Author: User

DESCRIPTIONHH has a constant habit, like to walk after a meal hundred steps. The so-called hundred step Walk, is a walk, is in a certain time, through a certain distance. But at the same time HH is a person who likes to change, so he will not immediately follow the road just walked back. And because HH is a person who likes to change, so he walks the path every day is not exactly the same, he wants to know how many ways he has to walk. Now give your school map (assuming the length of each road is the same is 1), ask the length of T, from a given location A to a given location B total number of eligible paths input line: five integer n,m,t,a,b. where n indicates the number of intersections in the school, m indicates the number of roads in the school, T indicates the distance that HH wants to walk, a is the starting point for a walk, and B represents the end of the walk. The next m line, a set of Ai,bi per row, represents a road from the intersection AI to the intersection Bi. The data guarantees AI = Bi, but there is no guarantee that there is at most one route connected between any two intersections. The intersection number is from 0 to n−1. All data in the same row is separated by a space, and there is no extra space at the end of the line. There are no extra empty lines. Answer modulus 45989. An output row that represents the answer. Sample Input4 5 3) 0 0
0 1
0 2
0 3
2 1
3 2Sample Output4HINT

For 30% of data, n≤4,m≤10,t≤10.
For 100% of data, n≤20,m≤60,t≤230,0≤a,b<n,0≤= "ai,bi=" "<n. <= "p=" ">

Analysis

Well, you can count on it. (It's actually DP)

Simple matrix multiplication, practice.

1 /*2 Song Dynasty Zhu Dunru's3 "Xijiang Moon, the world is short like a Dream"4 the world is short like a dream, the human thin like autumn cloud. No need to care about elbow grease heart. Everything turns out to be alive. 5 Fortunately, three cups of wine is good, the situation every flower new. A laugh and a blind date. Tomorrow cloudy and clear undecided. 6 */7#include <cstdio>8#include <cstring>9#include <algorithm>Ten#include <cmath> One#include <queue> A#include <vector> -#include <iostream> -#include <string> the#include <ctime> - #defineLOCAL - Const intMAXN = -*2+Ten; - Const intMOD =45989; + Const DoublePi = ACOs (-1.0); - Long LongG = the;//Original Root + Const intMAXM = -*2+Ten;  A using namespacestd; at //read-in optimization - voidReadint&x) { -     CharCh;x =0; -     intFlag =1; -CH =GetChar (); -      while(Ch <'0'|| CH >'9') {if(ch = ='0') flag =-1; CH =GetChar ();} in      while(Ch >='0'&& CH <='9') {x = x *Ten+ (CH-'0'); CH =GetChar ();} -X *=Flag; to } +  - structedge{ the        intu, v; * }EDGE[MAXM]; $ intM;Panax Notoginseng structmatrix{ -        intNUM[MAXN][MAXN]; the        //Matrix () {memset (num, 0, sizeof (num));} +Matrixoperator* (ConstMatrix &b) { A Matrix C; thememset (C.num,0,sizeof(C.num)); +                for(inti =1; i < M; i++) -                for(intj =1; J < M; J + +) $                for(intK =1; K < M; k++){ $                   //if (i = = 5 && J = = 9) -                   //printf (""); -C.NUM[I][J] = (C.num[i][j] + num[i][k] * b.num[k][j])%MOD; the               } -               returnC;Wuyi        } the }x1, x2, x3; -Matrix Pow (Matrix A,intb) { Wu        if(b = =1)returnA; -Matrix tmp = POW (A, b/2); About        if(b%2==0)returnTMP *tmp; $        Else return(TMP * tmp) *A; - } - intN, M, T, A, B, HEAD[MAXN], NEXT[MAXM]; -  A //No forward Edge + voidAddedge (intUintv) { theedge[m].u = u; EDGE[M].V =v; -NEXT[M] =Head[u]; $Head[u] = m++; the       theedge[m].u = v; EDGE[M].V =u; theNEXT[M] =Head[v]; theHEAD[V] = m++; - } in voidinit () { thememset (X1.num,0,sizeof(X1.num)); thememset (X2.num,0,sizeof(X2.num)); Aboutmemset (X3.num,0,sizeof(X3.num)); theMemset (Head,-1,sizeof(head)); the read (n); read (m); the Read ( t); Read (A); Read (B); +K2;//Note that there is an artificial source -       for(inti =1; I <= m; i++){ the          intu, v;Bayi read (u); Read (v); the Addedge (U, v); the      } - }  - voidprepare () { the       for(inti = Head[a]; I! =-1; i = Next[i]) x1.num[1][i]++; the       the       for(inti =2; i < M; i++) the       for(intj =2; J < M; J + +) -      if(EDGE[I].V = = edge[j].u && (i ^1)! = j) x2.num[i][j]++;//Note that this is connected by an edge. the /* the for (int i = 1; i < M; i++) { the for (int j = 1; j < M; J + +) printf ("%d", x2.num[i][j]);94 printf ("\ n"); the      }*/ the } the voidWork () {98      intAns =0; AboutX1 = x1 * POW (x2, T-1);  -      /*for (int i = 1; i < M; i++) {101 for (int j = 1; j < M; J + +) printf ("%d", x1.num[i][j]);102 printf ("\ n");103      }*/104       for(inti = head[b]; I! =-1; i = Next[i]) ans = (ans + x1.num[1][i ^1]) %MOD; theprintf"%d\n", Ans); 106 }107 108 intMain () {109 init (); the prepare ();111 Work (); the     return 0;113}
View Code

"BZOJ1875" "Matrix multiplication" [sdoi2009]hh go for a walk

Related Article

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.