#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=30010;
const int mxM=8000000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N, M;
vector <pii> BP;
vector <pair<int, bool>> adj[mxM];
int idx;
int X, Y;
list <int> lst;
int dis[mxM];
bool Chk[mxM];
bool cmp1(pii a, pii b)
{
if(a.sec!=b.sec) return a.sec<b.sec;
if(a.fir%a.sec!=b.fir%b.sec) return a.fir%a.sec<b.fir%b.sec;
return a.fir<b.fir;
}
void zero_one_bfs()
{
while(!lst.empty())
{
int now=lst.front();
lst.pop_front();
if(Chk[now]) continue;
Chk[now]=true;
for(pii nxt : adj[now])
{
if(dis[nxt.fir]>dis[now]+nxt.sec)
{
dis[nxt.fir]=dis[now]+nxt.sec;
if(lst.empty() || dis[lst.front()]<=dis[nxt.fir]) lst.push_front(nxt.fir);
else lst.push_back(nxt.fir);
}
}
}
}
int main()
{
gibon
cin >> N >> M;
idx=N;
BP.resize(M);
for(int i=0;i<M;i++)
{
cin >> BP[i].fir >> BP[i].sec;
if(i==0) X=BP[i].fir;
if(i==1) Y=BP[i].fir;
}
sort(BP.begin(), BP.end(), cmp1);
int cur=0;
while(cur!=M)
{
int ncur=cur;
for(int j=BP[ncur].fir%BP[ncur].sec;j<N;j+=BP[ncur].sec)
{
if(cur<M && BP[cur].fir==j)
{
adj[j].push_back({idx, 0});
while(cur<M && BP[cur].fir==j) cur++;
}
adj[idx].push_back({j, 0});
if(j!=BP[ncur].fir%BP[ncur].sec)
{
adj[idx].push_back({idx-1, 1});
adj[idx-1].push_back({idx, 1});
}
idx++;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=MOD;
}
dis[X]=0;
lst.push_front(X);
zero_one_bfs();
if(dis[Y]==MOD) cout << -1;
else cout << dis[Y];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
188056 KB |
Output is correct |
2 |
Correct |
104 ms |
188184 KB |
Output is correct |
3 |
Correct |
107 ms |
188088 KB |
Output is correct |
4 |
Correct |
104 ms |
188100 KB |
Output is correct |
5 |
Correct |
102 ms |
188072 KB |
Output is correct |
6 |
Incorrect |
104 ms |
188060 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
188180 KB |
Output is correct |
2 |
Correct |
102 ms |
188228 KB |
Output is correct |
3 |
Correct |
115 ms |
188084 KB |
Output is correct |
4 |
Correct |
148 ms |
188176 KB |
Output is correct |
5 |
Correct |
105 ms |
188112 KB |
Output is correct |
6 |
Incorrect |
101 ms |
188176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
188132 KB |
Output is correct |
2 |
Correct |
104 ms |
188176 KB |
Output is correct |
3 |
Correct |
105 ms |
188168 KB |
Output is correct |
4 |
Correct |
112 ms |
188132 KB |
Output is correct |
5 |
Correct |
104 ms |
188148 KB |
Output is correct |
6 |
Incorrect |
102 ms |
188084 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
188132 KB |
Output is correct |
2 |
Correct |
100 ms |
188092 KB |
Output is correct |
3 |
Correct |
105 ms |
188176 KB |
Output is correct |
4 |
Correct |
99 ms |
188156 KB |
Output is correct |
5 |
Correct |
101 ms |
188056 KB |
Output is correct |
6 |
Incorrect |
102 ms |
188176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
188100 KB |
Output is correct |
2 |
Correct |
103 ms |
188184 KB |
Output is correct |
3 |
Correct |
103 ms |
188100 KB |
Output is correct |
4 |
Correct |
103 ms |
188100 KB |
Output is correct |
5 |
Correct |
99 ms |
188188 KB |
Output is correct |
6 |
Incorrect |
105 ms |
188176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |