Found |
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:260144KB |
Total submit users: 2, Accepted users: 2 |
Problem 13881 : No special judgement |
Problem description |
Alice and Bob live in a city with N intersections (numbered as intersection 1, intersection 2,…, intersection N). There are M roads. Each road is bidirectional and directly joins two intersections. Each pair of intersections is joined by at most one road. Alice lives in a house at intersection 1. Bob lives in a house at intersection N. They start wandering in the city at 12pm. Each minute each one move from one intersection to another which is connected directly by a road. (Note that since they have to move, they cannot stay at the same intersection they were at in the previous minute). They agree to meet exactly T minutes after 12pm at some intersection. Also they do not want to meet each other before T minutes. In how many ways can they move inside the city? Note that because they travel fairly fast, they do not meet if they travel on the same road on different directions. |
Input |
The first line of the input contains an integer K (1<=K<=5), the number of test cases. Then K test cases follow in the format specified below. Each test case starts with 3 integers N, M, and T (2<=N<=10; 1<=M<=50; 1<=T<=1,000,000,000,000). The next M lines specify road connections. That is, line 1+i, for 1<=i<=M, contain two integers A and B specifying that there is a bidirectional road connecting intersection A and B (1<=A<=N; 1<=B<=N; A is not equal to B). |
Output |
For each test case, your program should output the number of possible ways (modulo 9973) can Alice and Bob move in the city so that at time T they meet at some intersection but they do not meet at any intersection before that. Since the number can be very large, you should output the numbers modulo 9973. |
Sample Input |
4 3 3 2 1 2 2 3 1 3 3 2 2 1 2 2 3 4 4 10 1 2 2 4 1 3 4 3 4 5 1000 1 2 2 4 1 3 4 3 1 4 |
Sample Output |
3 0 1024 3213 |
Problem Source |
ACM-ICPC Asia Thailand National On-Site Programming Contest 2015 |
题解:n的范围很小,所以可以用a*n+b来表示a,b所在位置的状态,用mp[i][j]表示a,b能否从i状态转移到j状态,再进行矩阵快速幂(矩阵的k次乘方就相当于i经过k条路到达j的方数 当k=2时,∑ way[i][x] * way[x][j] (0<=x<n && x!=i && x!=j),以此类推)
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int mod=9973;
const int maxn=105;
bool mp[105][105];
int t,n,m;
ll T;
struct matrix{
int m[105][105];
friend matrix operator*(matrix a,matrix b){
matrix c;
for(int i=0;i<n*n;i++)
for(int j=0;j<n*n;j++){
c.m[i][j]=0;
for(int k=0;k<n*n;k++)
c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;
c.m[i][j]%=mod;
}
return c;
}
}cnt;
matrix qpow(matrix a,ll b){
matrix ret;
memset(ret.m,0,sizeof(ret.m));
for(int i=0;i<n;i++) ret.m[i][i]=1;
while(b){
if(b&1) ret=ret*a;
b>>=1;
a=a*a;
}
return ret;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d%I64d",&n,&m,&T);
memset(mp,0,sizeof(mp));
memset(cnt.m,0,sizeof(cnt.m));
for(int u,v,i=0;i<m;i++){
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=1;
}
for(int i=0;i<n*n;i++){
int a=i/n+1,b=i%n+1;
if(a==b) continue;
for(int j=0;j<n*n;j++){
int c=j/n+1,d=j%n+1;
if(c==d||a==c||b==d||!mp[a][c]||!mp[b][d]) continue;
cnt.m[i][j]=1;
}
}
cnt=qpow(cnt,T-1);
int ans=0;
for(int i=0;i<n*n;i++){
int a=i/n+1,b=i%n+1;
if(!cnt.m[n-1][i]||a==b) continue;
for(int j=1;j<=n;j++)
if(mp[a][j]&&mp[b][j])
ans=(ans+cnt.m[n-1][i])%mod;
}
printf("%d\n",ans);
}
}