Bzoj 1875 [sdoi2009]hh go for a walk (moment multiplication)

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
1 R
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

Ideas

Matrix multiplication, edge-point interchange

There is no need to backtrack in the topic but there are also heavy edges. You can meet this requirement by exchanging a bit of edge. Construct a matrix according to the connection of the side and find out the a^ (t-1), create a new virtual node, make it connect to all the out edge of a, b*a can find out the number of paths of the virtual node to each side length T, and then count all the sides connected with B.

Code

1#include <cmath>2#include <queue>3#include <vector>4#include <cstdio>5#include <cstring>6#include <iostream>7#include <algorithm>8 #definefor (A,B,C) for (int a= (b); a<= (c); a++)9 using namespacestd;Ten  One Const intMAXN = -*5; A Const intMOD =45989; - structEdge {intV,next; -}es[maxn<<1]; the  - structMatrix { -     intR,C,N[MAXN][MAXN]; -     voidInitintRintc) { +          This->r=r, This->c=C; -memset (N,0,sizeof(N)); +     } AMatrixoperator* (ConstMatrix B)Const{ at Matrix C; C.init (R,B.C); -          for(intI=0; i<r;i++) -              for(intj=0; j<b.c;j++) -                  for(intk=0; k<c;k++) -C.n[i][j]= (C.n[i][j]+n[i][k]*b.n[k][j])%MOD; -         returnC; in     } -Matrix Pow (intp) { toMatrix tmp=* This, ans; + Ans.init (r,r); -          for(intI=0; i<r;i++) ans. n[i][i]=1; the          while(p) { *             if(p&1) ans=ans*tmp; $tmp=tmp*tmp; p>>=1;Panax Notoginseng         } -         returnans; the     } + }a,b; A  the intn,m,t,a,b,u,v; + intfront[maxn],en=-1; - voidAdde (intUintv) { $en++; Es[en].next=front[u]; Es[en].v=v; front[u]=en; $ } -  - intMain () { theFreopen ("in.in","R", stdin); -Freopen ("Out.out","W", stdout);Wuyimemset (front,-1,sizeof(front)); thescanf"%d%d%d%d%d",&n,&m,&t,&a,&b); -      for(intI=0; i<m;i++) { Wuscanf"%d%d",&u,&v); - Adde (u,v), Adde (v,u); About     } $en+=3; - A.init (En,en), B.init (En,en); -      for(inti=front[a];i>=0; i=es[i].next) b.n[en-2][i]=1; -      for(intI=0; i<n;i++) { A          for(intu=front[i];u>=0; u=Es[u].next) { +             intj=es[u].v; the              for(intv=front[j];v>=0; v=es[v].next) -                 if(U!= (v^1)) a.n[u][v]++; $         } the     } theA=a.pow (t1); thea=b*A; the     intans=0; -      for(inti=front[b];i>=0; i=es[i].next) inAns= (ans+a.n[en-2][i^1])%MOD; theprintf"%d", ans); the     return 0; About}

Bzoj 1875 [sdoi2009]hh go for a walk (moment multiplication)

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.