简单题,公式计算+最短路。注意点:注意1 取模,2 数组开到n*n+n.
#include<iostream> #include<queue> using namespace std; long long x[1234567];long long y[1234567];long long z[1234567]; int c[1001][1001]; int d[1002];int inq[1002]; const int inf=0x3f3f3f3f; int n,m; void spfa() { for(int i=0;i<n;i++) { d[i]=inf;inq[i]=0; } queue<int>q; inq[0]=1;d[0]=0; q.push(0); while(!q.empty()) { int cur=q.front(); q.pop();inq[cur]=0; for(int i=0;i<n;i++) { if(d[i]>c[cur][i]+d[cur]) { d[i]=c[cur][i]+d[cur]; if(!inq[i]) { q.push(i); inq[i]=1; } } } } } int main() { while(cin>>n>>m>>x[0]>>x[1]>>y[0]>>y[1]) { for(int i=2;i<(n-1)*n+n;i++) { x[i]=(12345+x[i-1]*23456%5837501+x[i-2]*34567%5837501+x[i-1]*x[i-2]%5837501*45678%5837501)%5837501; y[i]=(56789+y[i-1]*67890%9860381+y[i-2]*78901%9860381+y[i-1]*y[i-2]%9860381*89012%9860381)%9860381; } for(int k=0;k<(n-1)*n+n;k++) { z[k]= (x[k]*90123%8475871+y[k])%8475871+1; } for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i==j) { c[i][j]=0; } else { c[i][j]= z[i*n+j]; } } spfa(); int minn=d[1]%m; for(int i=2;i<n;i++) { if(d[i]%m<minn) { minn=d[i]%m; } } cout<<minn<<endl; } }