Luo Gu P3758 [TJOI2017] Coke
Description
- People Jiali Dun planet particularly fond of drinking cola. Thus, they developed a hostile planet a Coke robot and placed on the No. 1 city Jiali Dun planet. The cola robot has three acts: stay still, go to the next adjacent city, blew. It will be every second random trigger an action. Now give Jiali Dun planet city map, 0 seconds when Coke robot No. 1 in the city, and asked after t seconds, Coke robot behavior program number is how much?
Input
The first line input condition two positive integers N, M, N represents the number of cities, M is the number of roads. (1 <= N <= 30,0 <M <100)
The next M line input u, v, represents u, there is a road between v. (1 <= u, v <= n) to ensure that only one way between the two cities are connected.
Finally, the input time t
Output
- Coke output of the robot's behavior program number, the answer may be large, please post the output of modulo 2017.
Sample Input
3 2 1 2 2 3 2
Sample Output
8
Data Size
For 20% of the pn, there are 1 <t ≤ 1000
To 100% of the pn, there are 1 <t ≤ 10 ^ 6.
answer:
- Fast Matrix power.
- Ideas from Zhang_RQ greatly
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 35
#define mod 2017
using namespace std;
struct Node
{
Node() {memset(m, 0, sizeof(m));}
int m[N][N];
}a;
int n, m, k, ans;
Node cal(Node x, Node y)
{
Node z;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = 0; k <= n; k++)
z.m[i][j] += ((x.m[i][k] % mod) * (y.m[k][j] % mod)) % mod,
z.m[i][j] %= mod;
return z;
}
Node power(Node a, int b)
{
Node r = a, base = a;
while(b)
{
if(b & 1) r = cal(r, base);
base = cal(base, base);
b >>= 1;
}
return r;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++) a.m[i][0] = 1; //自爆
for(int i = 0; i <= n; i++) a.m[i][i] = 1; //停留
for(int i = 1; i <= m; i++)
{
int u, v; cin >> u >> v;
a.m[u][v] = a.m[v][u] = 1;
}
cin >> k;
a = power(a, k - 1);
for(int i = 0; i <= n; i++)
ans += a.m[1][i] % mod,
ans %= mod;
cout << ans;
return 0;
}