[Bzoj 1875] [Sdoi 2009] HH go for a Walk "matrix multiplication"

Source: Internet
Author: User
Tags mul

Title Link: BZOJ-1875

Topic Analysis:

This problem if you remove the "do not immediately follow the road back to" the limit, directly using the adjacency matrix to run the matrix multiplication is possible. Now, however, with this restriction, the way to build the map is to make some changes. If we think of each side as a point matrix, then each time we start from one edge to the other, we cannot still "stay" on this side, so this can satisfy the limitation of the topic. Each edge is split into two one-way edges, such as a number of 4, and a number of 5. So 4^1=5, 5^1=4. So as long as do not walk from the side of the I or i^1 on it can be. The initial number of edges in the matrix with a as a starting point arrives at 1 and the remainder is 0. Finally, the solution count of the edge of the end of B is summed up as the answer. 、

This idea of flexible conversion of edges and points is ingenious and should be noted.

The code is as follows:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm>using namespace std;const int maxn = 5, MAXM = + 5, Mod = 45989;int n, m, T, A, B, Topa, TOPB, Index, RT, Ans, A, b;int EA[MAXM], eb[maxm];struct edge{int u, v; Edge () {}edge (int a, int b) {u = A; v = b;}} e[maxm];struct Matrix {int x, y, num[maxm][maxm];void setxy (int xx, int yy) {x = xx; y = yy;} void Clear (int nn) {for (int i = 0, i < x; ++i) {for (int j = 0; j < y; ++j) {num[i][j] = nn;}}} M0, MZ; Matrix Mul (Matrix A, Matrix B) {matrix Ret;ret. SetXY (a.x, B.Y); ret. Clear (0), for (int i = 0, i < ret.x; ++i) {for (int j = 0; j < Ret.y; ++j) {for (int k = 0; k < a.y; ++k) {ret. NUM[I][J] + = a.num[i][k] * B.num[k][j];ret. NUM[I][J]%= Mod;}}} return ret;} Matrix Pow (Matrix A, int b) {matrix ret, f;f = A;ret. SetXY (f.x, F.Y); for (int i = 0; I <= ret.x; ++i) ret. Num[i][i] = 1;while (b) {if (b & 1) ret = Mul (ret, f); b >>= 1;f = Mul (f, f);} return ret;} int main () {scanf ("%d%d%d%d%d", &n, &m, &t, &a, &b); Index = -1;for (int i = 1; I <= m; ++i) {scanf ( "%d%d", &a, &b); E[++index] = Edge (A, b); E[++index] = Edge (b, a);} Mz. SetXY (M * 2, M * 2); Mz. Clear (0); Topa = TOPB = 0;for (int i = 0; I <= Index; ++i) {if (e[i].u = A) Ea[++topa] = i;if (e[i].v = B) EB[++TOPB] = I;for ( int j = 0; J <= Index; ++J) {if (i! = J && I! = (j ^ 1) && e[i].v = = e[j].u) MZ. NUM[I][J] = 1;}} M0. SetXY (1, M * 2); M0. Clear (0), for (int i = 1; I <= Topa; ++i) M0. Num[0][ea[i]] = 1; MZ = Pow (mz, t-1); M0 = Mul (M0, MZ); ans = 0;for (int i = 1; I <= TOPB; ++i) {ans + = M0. Num[0][eb[i]]; Ans%= Mod;} printf ("%d\n", Ans); return 0;}

  

[Bzoj 1875] [Sdoi 2009] HH go for a Walk "matrix 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.