// Arayi
#include <bits/stdc++.h>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
char dc[] = {'R', 'D', 'L', 'U'};
const int N = 2e6 + 20;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = 1e-13;
const int T = 128;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1)
ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream &operator<<(ostream &c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template <class T>
void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
int n, m;
int b[N], p1[N];
int d[N][T];
vii fp[N];
int main()
{
fastio;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p1[i];
fp[b[i]].ad(i);
d[i][0] = mod;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < T; j++)
{
d[i][j] = mod;
}
}
priority_queue<pair<int, pair<int, int>>> q;
q.push(MP(0, MP(0, 0)));
if (p1[0] < T)
q.push(MP(0, MP(b[0], p1[0])));
while (!q.empty())
{
auto p = q.top();
q.pop();
if (d[p.sc.fr][p.sc.sc] < mod)
continue;
d[p.sc.fr][p.sc.sc] = -p.fr;
// cout << p.fr << " " << p.sc << endl;
if (p.sc.sc == 0)
{
if (p1[p.sc.fr] < T)
continue;
for (int i = b[p.sc.fr]; i < n; i += p1[p.sc.fr])
{
for (auto i1 : fp[i])
{
if (d[i1][0] < mod)
continue;
if (p1[i1] < T && d[i][p1[i1]] == mod)
q.push(MP(p.fr - ((i - b[p.sc.fr]) / p1[p.sc.fr]), MP(i, p1[i1])));
q.push(MP(p.fr - ((i - b[p.sc.fr]) / p1[p.sc.fr]), MP(i1, 0)));
}
}
for (int i = b[p.sc.fr] - p1[p.sc.fr]; i >= 0; i -= p1[p.sc.fr])
{
for (auto i1 : fp[i])
{
if (d[i1][0] < mod)
continue;
if (p1[i1] < T && d[i][p1[i1]] == mod)
q.push(MP(p.fr - ((b[p.sc.fr] - i) / p1[p.sc.fr]), MP(i, p1[i1])));
q.push(MP(p.fr - ((b[p.sc.fr] - i) / p1[p.sc.fr]), MP(i1, 0)));
}
}
}
else
{
for (auto i1 : fp[p.sc.fr])
{
if (d[i1][0] < mod)
continue;
q.push(MP(p.fr, MP(i1, 0)));
if (p1[i1] < T && d[p.sc.fr][p1[i1]] == mod)
q.push(MP(p.fr, MP(p.sc.fr, p1[i1])));
}
if (p.sc.fr + p.sc.sc < n && d[p.sc.fr + p.sc.sc][p.sc.sc] == mod)
q.push(MP(p.fr - 1, MP(p.sc.fr + p.sc.sc, p.sc.sc)));
if (p.sc.fr - p.sc.sc >= 0 && d[p.sc.fr - p.sc.sc][p.sc.sc] == mod)
q.push(MP(p.fr - 1, MP(p.sc.fr - p.sc.sc, p.sc.sc)));
}
}
if (d[1][0] == mod)
cout << -1 << endl;
else
cout << d[1][0] << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47316 KB |
Output is correct |
3 |
Correct |
26 ms |
47232 KB |
Output is correct |
4 |
Correct |
25 ms |
47276 KB |
Output is correct |
5 |
Correct |
30 ms |
47280 KB |
Output is correct |
6 |
Correct |
24 ms |
47316 KB |
Output is correct |
7 |
Correct |
23 ms |
47316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47204 KB |
Output is correct |
2 |
Correct |
23 ms |
47316 KB |
Output is correct |
3 |
Correct |
23 ms |
47244 KB |
Output is correct |
4 |
Correct |
23 ms |
47236 KB |
Output is correct |
5 |
Correct |
25 ms |
47240 KB |
Output is correct |
6 |
Correct |
24 ms |
47316 KB |
Output is correct |
7 |
Correct |
23 ms |
47296 KB |
Output is correct |
8 |
Correct |
24 ms |
47256 KB |
Output is correct |
9 |
Correct |
23 ms |
47316 KB |
Output is correct |
10 |
Correct |
23 ms |
47500 KB |
Output is correct |
11 |
Correct |
25 ms |
48264 KB |
Output is correct |
12 |
Correct |
25 ms |
48352 KB |
Output is correct |
13 |
Correct |
24 ms |
48392 KB |
Output is correct |
14 |
Correct |
27 ms |
48264 KB |
Output is correct |
15 |
Correct |
31 ms |
48312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47248 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
26 ms |
47260 KB |
Output is correct |
4 |
Correct |
24 ms |
47312 KB |
Output is correct |
5 |
Correct |
24 ms |
47300 KB |
Output is correct |
6 |
Correct |
24 ms |
47188 KB |
Output is correct |
7 |
Correct |
24 ms |
47316 KB |
Output is correct |
8 |
Correct |
29 ms |
47308 KB |
Output is correct |
9 |
Correct |
29 ms |
47316 KB |
Output is correct |
10 |
Correct |
27 ms |
47512 KB |
Output is correct |
11 |
Correct |
25 ms |
48392 KB |
Output is correct |
12 |
Correct |
25 ms |
48332 KB |
Output is correct |
13 |
Correct |
27 ms |
48336 KB |
Output is correct |
14 |
Correct |
27 ms |
48408 KB |
Output is correct |
15 |
Correct |
26 ms |
48340 KB |
Output is correct |
16 |
Correct |
24 ms |
47700 KB |
Output is correct |
17 |
Correct |
28 ms |
48212 KB |
Output is correct |
18 |
Correct |
26 ms |
48176 KB |
Output is correct |
19 |
Correct |
26 ms |
48232 KB |
Output is correct |
20 |
Correct |
29 ms |
48396 KB |
Output is correct |
21 |
Correct |
26 ms |
47832 KB |
Output is correct |
22 |
Correct |
25 ms |
48120 KB |
Output is correct |
23 |
Correct |
26 ms |
48104 KB |
Output is correct |
24 |
Correct |
27 ms |
48344 KB |
Output is correct |
25 |
Correct |
31 ms |
48372 KB |
Output is correct |
26 |
Correct |
28 ms |
48300 KB |
Output is correct |
27 |
Correct |
29 ms |
48460 KB |
Output is correct |
28 |
Correct |
26 ms |
48284 KB |
Output is correct |
29 |
Correct |
39 ms |
48256 KB |
Output is correct |
30 |
Correct |
30 ms |
48212 KB |
Output is correct |
31 |
Correct |
27 ms |
48316 KB |
Output is correct |
32 |
Correct |
28 ms |
48232 KB |
Output is correct |
33 |
Correct |
38 ms |
48468 KB |
Output is correct |
34 |
Correct |
42 ms |
48320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47208 KB |
Output is correct |
2 |
Correct |
24 ms |
47216 KB |
Output is correct |
3 |
Correct |
23 ms |
47280 KB |
Output is correct |
4 |
Correct |
26 ms |
47308 KB |
Output is correct |
5 |
Correct |
25 ms |
47204 KB |
Output is correct |
6 |
Correct |
23 ms |
47232 KB |
Output is correct |
7 |
Correct |
23 ms |
47316 KB |
Output is correct |
8 |
Correct |
24 ms |
47312 KB |
Output is correct |
9 |
Correct |
24 ms |
47308 KB |
Output is correct |
10 |
Correct |
31 ms |
47444 KB |
Output is correct |
11 |
Correct |
25 ms |
48340 KB |
Output is correct |
12 |
Correct |
25 ms |
48448 KB |
Output is correct |
13 |
Correct |
25 ms |
48284 KB |
Output is correct |
14 |
Correct |
25 ms |
48348 KB |
Output is correct |
15 |
Correct |
25 ms |
48340 KB |
Output is correct |
16 |
Correct |
24 ms |
47720 KB |
Output is correct |
17 |
Correct |
26 ms |
48204 KB |
Output is correct |
18 |
Correct |
24 ms |
48160 KB |
Output is correct |
19 |
Correct |
24 ms |
48204 KB |
Output is correct |
20 |
Correct |
29 ms |
48340 KB |
Output is correct |
21 |
Correct |
27 ms |
47876 KB |
Output is correct |
22 |
Correct |
24 ms |
48212 KB |
Output is correct |
23 |
Correct |
26 ms |
48136 KB |
Output is correct |
24 |
Correct |
29 ms |
48340 KB |
Output is correct |
25 |
Correct |
26 ms |
48284 KB |
Output is correct |
26 |
Correct |
31 ms |
48384 KB |
Output is correct |
27 |
Correct |
28 ms |
48504 KB |
Output is correct |
28 |
Correct |
27 ms |
48340 KB |
Output is correct |
29 |
Correct |
41 ms |
48328 KB |
Output is correct |
30 |
Correct |
28 ms |
48308 KB |
Output is correct |
31 |
Correct |
34 ms |
48340 KB |
Output is correct |
32 |
Correct |
29 ms |
48348 KB |
Output is correct |
33 |
Correct |
42 ms |
48468 KB |
Output is correct |
34 |
Correct |
38 ms |
48384 KB |
Output is correct |
35 |
Correct |
216 ms |
63816 KB |
Output is correct |
36 |
Correct |
33 ms |
49876 KB |
Output is correct |
37 |
Correct |
188 ms |
63532 KB |
Output is correct |
38 |
Correct |
287 ms |
69088 KB |
Output is correct |
39 |
Correct |
297 ms |
69088 KB |
Output is correct |
40 |
Correct |
267 ms |
69060 KB |
Output is correct |
41 |
Correct |
296 ms |
69076 KB |
Output is correct |
42 |
Correct |
40 ms |
63272 KB |
Output is correct |
43 |
Correct |
72 ms |
63584 KB |
Output is correct |
44 |
Correct |
39 ms |
63272 KB |
Output is correct |
45 |
Correct |
458 ms |
65920 KB |
Output is correct |
46 |
Correct |
479 ms |
66040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
25 ms |
47304 KB |
Output is correct |
4 |
Correct |
29 ms |
47212 KB |
Output is correct |
5 |
Correct |
27 ms |
47264 KB |
Output is correct |
6 |
Correct |
30 ms |
47216 KB |
Output is correct |
7 |
Correct |
23 ms |
47316 KB |
Output is correct |
8 |
Correct |
24 ms |
47308 KB |
Output is correct |
9 |
Correct |
31 ms |
47316 KB |
Output is correct |
10 |
Correct |
29 ms |
47528 KB |
Output is correct |
11 |
Correct |
27 ms |
48340 KB |
Output is correct |
12 |
Correct |
26 ms |
48304 KB |
Output is correct |
13 |
Correct |
23 ms |
48356 KB |
Output is correct |
14 |
Correct |
26 ms |
48348 KB |
Output is correct |
15 |
Correct |
24 ms |
48340 KB |
Output is correct |
16 |
Correct |
24 ms |
47648 KB |
Output is correct |
17 |
Correct |
29 ms |
48340 KB |
Output is correct |
18 |
Correct |
25 ms |
48108 KB |
Output is correct |
19 |
Correct |
32 ms |
48340 KB |
Output is correct |
20 |
Correct |
32 ms |
48280 KB |
Output is correct |
21 |
Correct |
33 ms |
47852 KB |
Output is correct |
22 |
Correct |
26 ms |
48232 KB |
Output is correct |
23 |
Correct |
26 ms |
48076 KB |
Output is correct |
24 |
Correct |
26 ms |
48204 KB |
Output is correct |
25 |
Correct |
29 ms |
48340 KB |
Output is correct |
26 |
Correct |
35 ms |
48328 KB |
Output is correct |
27 |
Correct |
29 ms |
48468 KB |
Output is correct |
28 |
Correct |
36 ms |
48340 KB |
Output is correct |
29 |
Correct |
38 ms |
48244 KB |
Output is correct |
30 |
Correct |
26 ms |
48228 KB |
Output is correct |
31 |
Correct |
27 ms |
48268 KB |
Output is correct |
32 |
Correct |
25 ms |
48272 KB |
Output is correct |
33 |
Correct |
40 ms |
48340 KB |
Output is correct |
34 |
Correct |
47 ms |
48340 KB |
Output is correct |
35 |
Correct |
201 ms |
63836 KB |
Output is correct |
36 |
Correct |
30 ms |
49852 KB |
Output is correct |
37 |
Correct |
241 ms |
63648 KB |
Output is correct |
38 |
Correct |
311 ms |
69052 KB |
Output is correct |
39 |
Correct |
257 ms |
69036 KB |
Output is correct |
40 |
Correct |
318 ms |
69032 KB |
Output is correct |
41 |
Correct |
282 ms |
69116 KB |
Output is correct |
42 |
Correct |
37 ms |
63304 KB |
Output is correct |
43 |
Correct |
66 ms |
63660 KB |
Output is correct |
44 |
Correct |
43 ms |
63332 KB |
Output is correct |
45 |
Correct |
461 ms |
65996 KB |
Output is correct |
46 |
Correct |
467 ms |
66012 KB |
Output is correct |
47 |
Correct |
253 ms |
66256 KB |
Output is correct |
48 |
Correct |
36 ms |
59504 KB |
Output is correct |
49 |
Correct |
39 ms |
60516 KB |
Output is correct |
50 |
Correct |
40 ms |
61780 KB |
Output is correct |
51 |
Correct |
109 ms |
64072 KB |
Output is correct |
52 |
Correct |
99 ms |
63992 KB |
Output is correct |
53 |
Correct |
50 ms |
63168 KB |
Output is correct |
54 |
Correct |
33 ms |
62232 KB |
Output is correct |
55 |
Correct |
33 ms |
62344 KB |
Output is correct |
56 |
Correct |
48 ms |
63504 KB |
Output is correct |
57 |
Correct |
88 ms |
62448 KB |
Output is correct |
58 |
Correct |
48 ms |
63788 KB |
Output is correct |
59 |
Correct |
47 ms |
63264 KB |
Output is correct |
60 |
Correct |
61 ms |
64388 KB |
Output is correct |
61 |
Correct |
709 ms |
75252 KB |
Output is correct |
62 |
Correct |
77 ms |
63488 KB |
Output is correct |
63 |
Correct |
181 ms |
63672 KB |
Output is correct |
64 |
Correct |
219 ms |
64472 KB |
Output is correct |
65 |
Correct |
386 ms |
66004 KB |
Output is correct |
66 |
Correct |
862 ms |
66008 KB |
Output is correct |
67 |
Correct |
882 ms |
66116 KB |
Output is correct |