Bzoj 1875 [sdoi] hh go for a walk (DP, matrix multiplication optimization)

Source: Internet
Author: User

Title Link: Bzoj 1875

The idea of this problem, the main idea is to construct a matrix is very ingenious. Our common use of matrix multiplication transfer is to use points to transfer, but this does not remove the situation in a place to stay.
A very magical approach is to use the edge to compose (for an edge I, except for an edge J to meet i== (j^1), and other edges are connected) transfer t-1 times,
It is then multiplied by a matrix (equivalent to a coefficient matrix) constructed from an edge that is connected to the starting point. Finally, just add the answer to the side that connects the end point.

#include <cstdio> #include <cstring> #include <iostream> using namespace std;
#define MOD (45989) int n,m,t,a,b,tot=0,k=2;

int q[10000],head[10000]; struct Edge{int v,next;}

E[125];
	struct node{int a[125][125];
	Node () {memset (a,0,sizeof (a));

}}s;
	inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
	} node Mut (node X,node y) {node t; for (int i=0;i<k;i++) for (int j=0;j<k;j++) for (int p=0;p<k;p++) t.a[i][j]= (t.a[i][j]+x.a[i][p] * y.a[p][j
	])%mod;
return t;
	} Node Pow (node X,int y) {node t;
	for (int i=0;i<=k;i++) t.a[i][i]=1;
		while (y) {if (y&1) T=mut (t,x);
		y>>=1;
	X=mut (X,X);
} return t;

} void Adde (int u,int v) {e[k].v=v; e[k].next=head[u]; head[u]=k++;} void input () {n=read (); M=read (); T=read (); A=read ();
	B=read ();
		for (int i=1;i<=m;i++) {int x=read (), Y=read (); Adde (x, y); ADDE (Y,X);
	
	}} void Solve () {for (int i=head[a];i;i=e[i].next) s.a[0][i]=1;
	Node T;
		for (int i=2;i<k;i++) {int v=e[i].v;
		if (v==b) q[++tot]=i;
			for (int j=head[v];j;j=e[j].next) {if (i== (j^1)) continue;
		T.a[i][j]=1;
	}} node Sai=pow (t,t-1);
	Node Ans=mut (S,SAI);
	int cnt=0;
	for (int i=1;i<=tot;i++) {cnt= (Cnt+ans.a[0][q[i]])%mod;
} printf ("%d\n", CNT);
	} int main () {input ();
	Solve ();
return 0; }


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.