题目总结
一个无向图,每一时刻有一些点不能到达,这些点的出现以12为最小公倍数,求s到t的路径数;
数据范围
1 ≤ N ≤ 50
1 ≤ K ≤ 2,000,000,000
1 ≤ NFish ≤ 20
解题思路
处理出每一时刻的邻接矩阵,12的整数倍有快速幂,剩下的手动乘,注意顺序;
易错误区
不该加的代码一定不要加
day[i][k][t] = 0;
//day[i][t][k] = 0;注释掉的那一行不可以加,因为假如此时你已经到达那个点,而食人鱼在路上,你是可以从这个点到其他点的;
矩阵乘法没有交换律
乘法一定按顺序,从1乘到11,再乘0;
代码展示
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<bitset>
#include<deque>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define ri register int
#define il inline
#define fi first
#define se second
#define mp make_pair
#define pi pair<int,int>
#define mem0(x) memset((x),0,sizeof (x))
#define mem1(x) memset((x),0x3f,sizeof (x))
il char gc() {
static const int BS = 1 << 22;
static unsigned char buf[BS], *st, *ed;
if (st == ed) ed = buf + fread(st = buf, 1, BS, stdin);
return st == ed ? EOF : *st++;
}
#define gc getchar
template<class T>void in(T &x)
{
x = 0; bool f = 0; char c = gc();
while (c < '0' || c > '9') {if (c == '-') f = 1; c = gc();}
while ('0' <= c && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = gc();}
if (f) x = -x;
}
#undef gc
void out(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
#define Maxsz 130
#define md 10000
#define sz n
il void mod(int &x) {
while (x > md) x -= md;
while (x < 0) x += md;
}
int n, m, st, en, d, nf;
struct Mat {
int m[Maxsz][Maxsz];
il void clear() {mem0(m);}
Mat () {clear();}
int* operator[](int x) {
return this->m[x];
}
Mat operator*(const Mat &x)const {
Mat res;
for (ri i = 1; i <= sz; i++) {
for (ri k = 1; k <= sz; k++) {
for (ri j = 1; j <= sz; j++)
mod(res.m[i][j] += m[i][k] * x.m[k][j] % md);
}
}
return res;
}
void operator*=(const Mat &x) {
*this = (*this) * x;
}
void operator+=(const Mat &x) {
for (ri i = 1; i <= sz; ++i) {
for (ri j = 1; j <= sz; ++j) {
mod(m[i][j] += x.m[i][j]);
}
}
}
Mat operator+(const Mat &x)const {
Mat res = *this;
res += x;
return res;
}
void print() {
for (ri i = 1; i <= sz; i++) {
for (ri j = 1; j <= sz; j++)
printf("%lld ", m[i][j]);
puts("");
}
puts("*****************");
}
void toe() {
clear();
for (ri i = 1; i <= sz; i++)
m[i][i] = 1;
}
Mat qpow(int x) {
Mat res, mul = *this;
if (x == 0) {
res.toe();
return res;
}
res = *this; --x;
for (; x; x >>= 1, mul *= mul) if (x & 1) res *= mul;
return res;
}
} pri, day[15], s;
#define N 55
#define M 2010
int v[M], u[M], nx[M];
int cnt, head[N];
il void add(int uu, int vv) {
u[++cnt] = uu, v[cnt] = vv, nx[cnt] = head[uu];
head[uu] = cnt;
}
int fish[25][8];
int zh, res;
signed main() {
in(n), in(m), in(st), in(en), in(d);
++st, ++en;
for (ri i = 1, a, b; i <= m; ++i) {
in(a), in(b);
++a, ++b;
add(a, b);
add(b, a);
pri[a][b] = pri[b][a] = 1;
}
in(nf);
for (ri i = 1; i <= nf; ++i) {
in(fish[i][5]);
for (ri j = 0; j < fish[i][5]; ++j) {
in(fish[i][j]);
fish[i][j]++;
}
}
day[13].toe();
for (ri i = 0; i < 12; ++i) {
day[i] = pri;
for (ri j = 1, t; j <= nf; ++j) {
t = fish[j][i % fish[j][5]];
for (ri k = 1; k <= n; ++k) {
day[i][k][t] = 0;
//day[i][t][k] = 0;
}
}
//day[i].print();
if (i) day[13] *= day[i];
}
day[13] *= day[0];
zh = (int)(d / 12);
res = d % 12;
day[13] = day[13].qpow(zh);
//day[13].print();
for (ri i = 0; i < res; ++i) day[13] *= day[(i + 1) % 12];
// day[13].print();
printf("%lld", day[13][st][en]);
return 0;
}