#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);
BP.erase(unique(BP.begin(), BP.end()), BP.end());
M=BP.size();
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(j==BP[cur].fir)
{
adj[j].push_back({idx, 0});
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 |
188120 KB |
Output is correct |
2 |
Correct |
104 ms |
188172 KB |
Output is correct |
3 |
Correct |
105 ms |
188144 KB |
Output is correct |
4 |
Correct |
102 ms |
188100 KB |
Output is correct |
5 |
Correct |
110 ms |
188156 KB |
Output is correct |
6 |
Incorrect |
99 ms |
188164 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
188184 KB |
Output is correct |
2 |
Correct |
107 ms |
188092 KB |
Output is correct |
3 |
Correct |
97 ms |
188100 KB |
Output is correct |
4 |
Correct |
109 ms |
188176 KB |
Output is correct |
5 |
Correct |
113 ms |
188068 KB |
Output is correct |
6 |
Incorrect |
106 ms |
188060 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
188152 KB |
Output is correct |
2 |
Correct |
103 ms |
188264 KB |
Output is correct |
3 |
Correct |
103 ms |
188132 KB |
Output is correct |
4 |
Correct |
101 ms |
188064 KB |
Output is correct |
5 |
Correct |
108 ms |
188068 KB |
Output is correct |
6 |
Incorrect |
102 ms |
188112 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
188100 KB |
Output is correct |
2 |
Correct |
101 ms |
188168 KB |
Output is correct |
3 |
Correct |
105 ms |
188116 KB |
Output is correct |
4 |
Correct |
105 ms |
188124 KB |
Output is correct |
5 |
Correct |
105 ms |
188172 KB |
Output is correct |
6 |
Incorrect |
113 ms |
188192 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
188140 KB |
Output is correct |
2 |
Correct |
106 ms |
188160 KB |
Output is correct |
3 |
Correct |
114 ms |
188104 KB |
Output is correct |
4 |
Correct |
105 ms |
188100 KB |
Output is correct |
5 |
Correct |
105 ms |
188172 KB |
Output is correct |
6 |
Incorrect |
110 ms |
188176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |