#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << "\n";
const int mod = 1e9 + 7;
int n, m, b[30005], p[30005];
vector <pair<pair<int,int>,int>> g[30005][180];
int dp[30005][180];
bool doge[30005][180];
priority_queue <pair<int,pair<int,int>>> pq;
map <pair<int,int>,bool> mp;
void solve () {
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
if(mp[{b[i], p[i]}]) continue;
mp[{b[i], p[i]}] = true;
if(p[i] * p[i] <= n)
doge[b[i]][p[i]] = true;
if(p[i] * p[i] > n) {
for(int j = b[i] - p[i], c = 1; j >= 0; j -= p[i], c++)
g[b[i]][0].pb({{j, 0}, c});
for(int j = b[i] + p[i], c = 1; j < n; j += p[i], c++)
g[b[i]][0].pb({{j, 0}, c});
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j * j <= n; j++) {
dp[i][j] = 1e9 + 7;
}
}
dp[b[0]][0] = 0;
pq.push({0, {b[0], 0}});
while(!pq.empty()) {
int cost = -pq.top().fi;
int pos = pq.top().se.fi;
int pow = pq.top().se.se;
pq.pop();
if(cost > dp[pos][pow]) continue;
if(pos == b[1]) {
cout << cost << "\n";
return;
}
if(pow == 0) {
for(auto c : g[pos][pow]) {
if(dp[c.fi.fi][c.fi.se] > cost + c.se) {
dp[c.fi.fi][c.fi.se] = cost + c.se;
pq.push({-dp[c.fi.fi][c.fi.se], {c.fi.fi, c.fi.se}});
}
}
}
else {
if(pos + pow < n) {
if(dp[pos + pow][pow] > cost + 1) {
dp[pos + pow][pow] = cost + 1;
pq.push({-dp[pos + pow][pow], {pos + pow, pow}});
}
}
if(pos - pow >= 0) {
if(dp[pos - pow][pow] > cost + 1) {
dp[pos - pow][pow] = cost + 1;
pq.push({-dp[pos - pow][pow], {pos - pow, pow}});
}
}
}
for(int i = 0; i * i <= n; i++) {
if(i == pow) continue;
if(i == 0 || doge[pos][i] == true) {
if(dp[pos][i] > cost) {
dp[pos][i] = cost;
pq.push({-dp[pos][i], {pos, i}});
}
}
}
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j * j <= n; j++) {
// cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
cout << -1 << "\n";
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve ();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
127092 KB |
Output is correct |
2 |
Correct |
60 ms |
127072 KB |
Output is correct |
3 |
Correct |
60 ms |
127156 KB |
Output is correct |
4 |
Correct |
60 ms |
127052 KB |
Output is correct |
5 |
Correct |
61 ms |
127048 KB |
Output is correct |
6 |
Correct |
62 ms |
127144 KB |
Output is correct |
7 |
Correct |
60 ms |
127164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
127084 KB |
Output is correct |
2 |
Correct |
61 ms |
127156 KB |
Output is correct |
3 |
Correct |
63 ms |
127108 KB |
Output is correct |
4 |
Correct |
61 ms |
127080 KB |
Output is correct |
5 |
Correct |
61 ms |
127052 KB |
Output is correct |
6 |
Correct |
59 ms |
127052 KB |
Output is correct |
7 |
Correct |
61 ms |
127152 KB |
Output is correct |
8 |
Correct |
61 ms |
127188 KB |
Output is correct |
9 |
Correct |
62 ms |
127136 KB |
Output is correct |
10 |
Correct |
60 ms |
127280 KB |
Output is correct |
11 |
Correct |
61 ms |
127412 KB |
Output is correct |
12 |
Correct |
60 ms |
127180 KB |
Output is correct |
13 |
Correct |
60 ms |
127280 KB |
Output is correct |
14 |
Correct |
65 ms |
127424 KB |
Output is correct |
15 |
Correct |
62 ms |
127364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
127172 KB |
Output is correct |
2 |
Correct |
61 ms |
127156 KB |
Output is correct |
3 |
Correct |
60 ms |
127084 KB |
Output is correct |
4 |
Correct |
66 ms |
127100 KB |
Output is correct |
5 |
Correct |
60 ms |
127052 KB |
Output is correct |
6 |
Correct |
61 ms |
127196 KB |
Output is correct |
7 |
Correct |
66 ms |
127056 KB |
Output is correct |
8 |
Correct |
61 ms |
127128 KB |
Output is correct |
9 |
Correct |
60 ms |
127176 KB |
Output is correct |
10 |
Correct |
64 ms |
127380 KB |
Output is correct |
11 |
Correct |
65 ms |
127324 KB |
Output is correct |
12 |
Correct |
70 ms |
127244 KB |
Output is correct |
13 |
Correct |
63 ms |
127232 KB |
Output is correct |
14 |
Correct |
62 ms |
127452 KB |
Output is correct |
15 |
Correct |
62 ms |
127436 KB |
Output is correct |
16 |
Correct |
61 ms |
127408 KB |
Output is correct |
17 |
Correct |
64 ms |
128000 KB |
Output is correct |
18 |
Correct |
70 ms |
128480 KB |
Output is correct |
19 |
Correct |
62 ms |
128736 KB |
Output is correct |
20 |
Correct |
62 ms |
129068 KB |
Output is correct |
21 |
Correct |
60 ms |
127500 KB |
Output is correct |
22 |
Correct |
61 ms |
128500 KB |
Output is correct |
23 |
Correct |
70 ms |
128460 KB |
Output is correct |
24 |
Correct |
64 ms |
128860 KB |
Output is correct |
25 |
Correct |
63 ms |
128708 KB |
Output is correct |
26 |
Correct |
70 ms |
128680 KB |
Output is correct |
27 |
Correct |
62 ms |
128620 KB |
Output is correct |
28 |
Correct |
64 ms |
128840 KB |
Output is correct |
29 |
Correct |
84 ms |
128672 KB |
Output is correct |
30 |
Correct |
64 ms |
128592 KB |
Output is correct |
31 |
Correct |
68 ms |
128668 KB |
Output is correct |
32 |
Correct |
68 ms |
128828 KB |
Output is correct |
33 |
Correct |
77 ms |
129492 KB |
Output is correct |
34 |
Correct |
69 ms |
129448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
127224 KB |
Output is correct |
2 |
Correct |
63 ms |
127076 KB |
Output is correct |
3 |
Correct |
61 ms |
127160 KB |
Output is correct |
4 |
Correct |
62 ms |
127052 KB |
Output is correct |
5 |
Correct |
60 ms |
127088 KB |
Output is correct |
6 |
Correct |
61 ms |
127048 KB |
Output is correct |
7 |
Correct |
61 ms |
127148 KB |
Output is correct |
8 |
Correct |
63 ms |
127160 KB |
Output is correct |
9 |
Correct |
64 ms |
127172 KB |
Output is correct |
10 |
Correct |
62 ms |
127352 KB |
Output is correct |
11 |
Correct |
61 ms |
127424 KB |
Output is correct |
12 |
Correct |
61 ms |
127260 KB |
Output is correct |
13 |
Correct |
60 ms |
127168 KB |
Output is correct |
14 |
Correct |
62 ms |
127384 KB |
Output is correct |
15 |
Correct |
64 ms |
127332 KB |
Output is correct |
16 |
Correct |
64 ms |
127436 KB |
Output is correct |
17 |
Correct |
62 ms |
127976 KB |
Output is correct |
18 |
Correct |
63 ms |
128504 KB |
Output is correct |
19 |
Correct |
63 ms |
128560 KB |
Output is correct |
20 |
Correct |
74 ms |
128972 KB |
Output is correct |
21 |
Correct |
62 ms |
127460 KB |
Output is correct |
22 |
Correct |
62 ms |
128552 KB |
Output is correct |
23 |
Correct |
66 ms |
128488 KB |
Output is correct |
24 |
Correct |
63 ms |
128848 KB |
Output is correct |
25 |
Correct |
62 ms |
128692 KB |
Output is correct |
26 |
Correct |
66 ms |
128664 KB |
Output is correct |
27 |
Correct |
63 ms |
128660 KB |
Output is correct |
28 |
Correct |
66 ms |
128840 KB |
Output is correct |
29 |
Correct |
79 ms |
128608 KB |
Output is correct |
30 |
Correct |
67 ms |
128540 KB |
Output is correct |
31 |
Correct |
68 ms |
128712 KB |
Output is correct |
32 |
Correct |
67 ms |
128960 KB |
Output is correct |
33 |
Correct |
78 ms |
129384 KB |
Output is correct |
34 |
Correct |
71 ms |
129496 KB |
Output is correct |
35 |
Correct |
83 ms |
131076 KB |
Output is correct |
36 |
Correct |
64 ms |
128492 KB |
Output is correct |
37 |
Correct |
76 ms |
132172 KB |
Output is correct |
38 |
Correct |
86 ms |
132792 KB |
Output is correct |
39 |
Correct |
84 ms |
132684 KB |
Output is correct |
40 |
Correct |
84 ms |
132656 KB |
Output is correct |
41 |
Correct |
82 ms |
132652 KB |
Output is correct |
42 |
Correct |
66 ms |
128864 KB |
Output is correct |
43 |
Correct |
75 ms |
128836 KB |
Output is correct |
44 |
Correct |
66 ms |
129272 KB |
Output is correct |
45 |
Correct |
100 ms |
135948 KB |
Output is correct |
46 |
Correct |
92 ms |
136136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
127104 KB |
Output is correct |
2 |
Correct |
65 ms |
127032 KB |
Output is correct |
3 |
Correct |
68 ms |
127160 KB |
Output is correct |
4 |
Correct |
63 ms |
127168 KB |
Output is correct |
5 |
Correct |
64 ms |
127176 KB |
Output is correct |
6 |
Correct |
62 ms |
127164 KB |
Output is correct |
7 |
Correct |
64 ms |
127096 KB |
Output is correct |
8 |
Correct |
61 ms |
127140 KB |
Output is correct |
9 |
Correct |
61 ms |
127208 KB |
Output is correct |
10 |
Correct |
65 ms |
127280 KB |
Output is correct |
11 |
Correct |
62 ms |
127308 KB |
Output is correct |
12 |
Correct |
64 ms |
127160 KB |
Output is correct |
13 |
Correct |
61 ms |
127272 KB |
Output is correct |
14 |
Correct |
63 ms |
127356 KB |
Output is correct |
15 |
Correct |
62 ms |
127432 KB |
Output is correct |
16 |
Correct |
65 ms |
127352 KB |
Output is correct |
17 |
Correct |
74 ms |
127944 KB |
Output is correct |
18 |
Correct |
63 ms |
128520 KB |
Output is correct |
19 |
Correct |
64 ms |
128780 KB |
Output is correct |
20 |
Correct |
63 ms |
129064 KB |
Output is correct |
21 |
Correct |
62 ms |
127520 KB |
Output is correct |
22 |
Correct |
74 ms |
128552 KB |
Output is correct |
23 |
Correct |
65 ms |
128492 KB |
Output is correct |
24 |
Correct |
66 ms |
128808 KB |
Output is correct |
25 |
Correct |
63 ms |
128716 KB |
Output is correct |
26 |
Correct |
64 ms |
128584 KB |
Output is correct |
27 |
Correct |
73 ms |
128580 KB |
Output is correct |
28 |
Correct |
64 ms |
128876 KB |
Output is correct |
29 |
Correct |
73 ms |
128592 KB |
Output is correct |
30 |
Correct |
66 ms |
128600 KB |
Output is correct |
31 |
Correct |
71 ms |
128708 KB |
Output is correct |
32 |
Correct |
66 ms |
128768 KB |
Output is correct |
33 |
Correct |
78 ms |
129444 KB |
Output is correct |
34 |
Correct |
71 ms |
129376 KB |
Output is correct |
35 |
Correct |
82 ms |
131176 KB |
Output is correct |
36 |
Correct |
67 ms |
128636 KB |
Output is correct |
37 |
Correct |
90 ms |
132160 KB |
Output is correct |
38 |
Correct |
87 ms |
132620 KB |
Output is correct |
39 |
Correct |
81 ms |
132536 KB |
Output is correct |
40 |
Correct |
85 ms |
132560 KB |
Output is correct |
41 |
Correct |
86 ms |
132556 KB |
Output is correct |
42 |
Correct |
81 ms |
128808 KB |
Output is correct |
43 |
Correct |
69 ms |
128844 KB |
Output is correct |
44 |
Correct |
65 ms |
129212 KB |
Output is correct |
45 |
Correct |
97 ms |
135956 KB |
Output is correct |
46 |
Correct |
103 ms |
136140 KB |
Output is correct |
47 |
Correct |
110 ms |
146084 KB |
Output is correct |
48 |
Correct |
88 ms |
147148 KB |
Output is correct |
49 |
Correct |
87 ms |
148172 KB |
Output is correct |
50 |
Correct |
84 ms |
149284 KB |
Output is correct |
51 |
Correct |
109 ms |
153736 KB |
Output is correct |
52 |
Correct |
124 ms |
153864 KB |
Output is correct |
53 |
Correct |
89 ms |
150400 KB |
Output is correct |
54 |
Correct |
80 ms |
148248 KB |
Output is correct |
55 |
Correct |
85 ms |
148172 KB |
Output is correct |
56 |
Correct |
107 ms |
155644 KB |
Output is correct |
57 |
Correct |
82 ms |
152860 KB |
Output is correct |
58 |
Correct |
106 ms |
148484 KB |
Output is correct |
59 |
Correct |
100 ms |
148512 KB |
Output is correct |
60 |
Correct |
101 ms |
149160 KB |
Output is correct |
61 |
Correct |
112 ms |
149072 KB |
Output is correct |
62 |
Correct |
193 ms |
153828 KB |
Output is correct |
63 |
Correct |
170 ms |
184856 KB |
Output is correct |
64 |
Correct |
187 ms |
198152 KB |
Output is correct |
65 |
Correct |
664 ms |
196640 KB |
Output is correct |
66 |
Execution timed out |
1104 ms |
187524 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |