Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)


HDU 4849 - Wow! Such City!_#include


 


Input


There are several test cases. Please process till EOF.

For each test case, there is only one line containing 6 integers N,M,X 0,X1,Y0,Y1.See the description for more details.


 


Output


For each test case, output a single line containing a single integer: the number of minimal category.


 


Sample Input



3 10 1 2 3 4


4 20 2 3 4 5


 


Sample Output



1


10


 


 


Hint:


HDU 4849 - Wow! Such City!_图论_02


 

怎么说吧,就是一个又臭又长又水的单源最短路径外加算算MOD的题。



1 #include<cstdio>
2 #include<queue>
3 #include<cstring>
4 #define MAXN 1003
5 #define MAXK 1000*1000+1000
6 #define INF 0x3f3f3f3f
7 using namespace std;
8 typedef long long ll;
9 int n,m;
10 ll x[MAXK],y[MAXK],z[MAXK],c[MAXN][MAXN];
11 int cate[1000000+3];
12 void calc()
13 {
14 int max_k=(n-1)*n+(n-2);
15 for(int k=0;k<=max_k;k++)
16 {
17 if(k>=2)
18 {
19 x[k]=(12345 + (x[k-1] * 23456) % 5837501 + (x[k-2] * 34567) % 5837501 + (x[k-1] * x[k-2] * 45678) % 5837501 ) % 5837501;
20 y[k]=(56789 + (y[k-1] * 67890) % 9860381 + (y[k-2] * 78901) % 9860381 + (y[k-1] * y[k-2] * 89012) % 9860381 ) % 9860381;
21 }
22 z[k]=(x[k] * 90123 + y[k] ) % 8475871 + 1;
23 }
24 //for(int k=0;k<=max_k;k++) printf("x[%d]=%lld \t y[%d]=%lld \t z[%d]=%lld \n",k,x[k],k,y[k],k,z[k]);
25 for(int i=0;i<n;i++)
26 {
27 for(int j=0;j<n;j++)
28 {
29 if(i==j) c[i][j]=0;
30 else c[i][j]=z[(i*n+j)];
31 //printf("%lld\t",c[i][j]);
32 }
33 //printf("\n");
34 }
35 }
36 bool vis[MAXN];
37 ll d[MAXN];
38 void spfa()
39 {
40 for(int i=1;i<n;i++){
41 vis[i]=0;
42 d[i]=INF;
43 }
44 vis[0]=1;
45 d[0]=0;
46 queue<int> q;
47 q.push(0);
48 while(!q.empty())
49 {
50 int u=q.front();q.pop();vis[u]=0;
51 for(int v=0;v<n;v++)
52 {
53 if(u==v) continue;
54 ll tmp=d[v];
55 if(d[v]>d[u]+c[u][v]) d[v]=d[u]+c[u][v];
56 if(d[v]<tmp && !vis[v]) q.push(v),vis[v]=1;
57 }
58 }
59 }
60 int main()
61 {
62 while(scanf("%d %d %lld %lld %lld %lld",&n,&m,&x[0],&x[1],&y[0],&y[1])!=EOF)
63 {
64 calc();
65 spfa();
66 memset(cate,0,sizeof(cate));
67 for(int i=1;i<n;i++)
68 {
69 //printf("d[%d]=%lld\n",i,d[i]);
70 cate[(d[i]%m)]++;
71 }
72 for(int i=0;i<m;i++)
73 {
74 if(cate[i]!=0)
75 {
76 printf("%d\n",i);
77 break;
78 }
79 }
80 }
81 }