/*
Normie's Template v2.0
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul.)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
//---------END-------//
vector<pii(int)> buc[5200001];
vector<pii(int)> lens;
vector<vector<int>> dis;
int dis_real[30001];
int jp[30001],jb[30001];
int n,m,i,j,k,t,t1,u,v,a,b;
int cur_len,cur_start,cur_pos;
vector<int> spawn[30001];
map<int,int> table[30001];
int main()
{
fio;
cin>>n>>m;
for (i=1;i<=m;i++)
{
cin>>jb[i]>>jp[i];
spawn[jb[i]].push_back(jp[i]);
if (table[jb[i]%jp[i]][jp[i]])
{
if (i==1)
{
u=table[jb[i]%jp[i]][jp[i]]-1;
v=jb[i]/jp[i];
buc[0].push_back({u,v});
dis[u][v]=0;
}
}
else
{
lens.push_back({jb[i]%jp[i],jp[i]});
dis.push_back({});
for (j=jb[i]%jp[i];j<n;j+=jp[i]) dis[lens.size()-1].push_back(1e9);
table[jb[i]%jp[i]][jp[i]]=lens.size();
if (i==1)
{
u=table[jb[i]%jp[i]][jp[i]]-1;
v=jb[i]/jp[i];
buc[0].push_back({u,v});
dis[u][v]=0;
}
}
}
for (i=0;i<n;i++) dis_real[i]=1e9;
for (t=0;t<5200000;t++)
{
for (i=0;i<buc[t].size();i++)
{
cur_len=lens[buc[t][i].fi].se;
cur_start=lens[buc[t][i].fi].fi;
cur_pos=cur_start+cur_len*buc[t][i].se;
if (dis_real[cur_pos]==1e9)
{
dis_real[cur_pos]=t;
for (int g : spawn[cur_pos])
{
u=table[cur_pos%g][g]-1;
if (dis[u][cur_pos/g]==1e9)
{
dis[u][cur_pos/g]=t;
buc[t].push_back({u,cur_pos/g});
}
}
}
}
for (i=0;i<buc[t].size();i++)
{
if ((buc[t][i].se-1>=0)and(dis[buc[t][i].fi][buc[t][i].se-1]==1e9))
{
dis[buc[t][i].fi][buc[t][i].se-1]=t+1;
buc[t+1].push_back({buc[t][i].fi,buc[t][i].se-1});
}
if ((buc[t][i].se+1<dis[buc[t][i].fi].size())and(dis[buc[t][i].fi][buc[t][i].se+1]==1e9))
{
dis[buc[t][i].fi][buc[t][i].se+1]=t+1;
buc[t+1].push_back({buc[t][i].fi,buc[t][i].se+1});
}
}
}
if (dis_real[jb[2]]==1e9) cout<<-1; else cout<<dis_real[jb[2]];
}
Compilation message
skyscraper.cpp:20: warning: ignoring #pragma comment [-Wunknown-pragmas]
20 | #pragma comment(linker, "/stack:200000000")
|
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:115:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (i=0;i<buc[t].size();i++)
| ~^~~~~~~~~~~~~~
skyscraper.cpp:134:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for (i=0;i<buc[t].size();i++)
| ~^~~~~~~~~~~~~~
skyscraper.cpp:141:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | if ((buc[t][i].se+1<dis[buc[t][i].fi].size())and(dis[buc[t][i].fi][buc[t][i].se+1]==1e9))
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
124524 KB |
Output is correct |
2 |
Correct |
97 ms |
124524 KB |
Output is correct |
3 |
Correct |
97 ms |
124524 KB |
Output is correct |
4 |
Correct |
98 ms |
124652 KB |
Output is correct |
5 |
Correct |
100 ms |
124652 KB |
Output is correct |
6 |
Correct |
97 ms |
124544 KB |
Output is correct |
7 |
Correct |
96 ms |
124652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
124652 KB |
Output is correct |
2 |
Correct |
96 ms |
124524 KB |
Output is correct |
3 |
Correct |
97 ms |
124524 KB |
Output is correct |
4 |
Correct |
97 ms |
124508 KB |
Output is correct |
5 |
Correct |
96 ms |
124524 KB |
Output is correct |
6 |
Correct |
97 ms |
124524 KB |
Output is correct |
7 |
Correct |
96 ms |
124524 KB |
Output is correct |
8 |
Correct |
96 ms |
124524 KB |
Output is correct |
9 |
Correct |
96 ms |
124524 KB |
Output is correct |
10 |
Correct |
96 ms |
124652 KB |
Output is correct |
11 |
Correct |
98 ms |
124780 KB |
Output is correct |
12 |
Correct |
97 ms |
124652 KB |
Output is correct |
13 |
Correct |
97 ms |
124652 KB |
Output is correct |
14 |
Correct |
99 ms |
124908 KB |
Output is correct |
15 |
Correct |
98 ms |
124908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
124524 KB |
Output is correct |
2 |
Correct |
97 ms |
124544 KB |
Output is correct |
3 |
Correct |
97 ms |
124524 KB |
Output is correct |
4 |
Correct |
97 ms |
124524 KB |
Output is correct |
5 |
Correct |
96 ms |
124540 KB |
Output is correct |
6 |
Correct |
98 ms |
124652 KB |
Output is correct |
7 |
Correct |
97 ms |
124524 KB |
Output is correct |
8 |
Correct |
96 ms |
124524 KB |
Output is correct |
9 |
Correct |
96 ms |
124524 KB |
Output is correct |
10 |
Correct |
97 ms |
124652 KB |
Output is correct |
11 |
Correct |
101 ms |
124908 KB |
Output is correct |
12 |
Correct |
96 ms |
124652 KB |
Output is correct |
13 |
Correct |
97 ms |
124652 KB |
Output is correct |
14 |
Correct |
100 ms |
124908 KB |
Output is correct |
15 |
Correct |
100 ms |
124908 KB |
Output is correct |
16 |
Correct |
97 ms |
124652 KB |
Output is correct |
17 |
Correct |
100 ms |
125036 KB |
Output is correct |
18 |
Correct |
97 ms |
124780 KB |
Output is correct |
19 |
Correct |
98 ms |
124652 KB |
Output is correct |
20 |
Correct |
97 ms |
124808 KB |
Output is correct |
21 |
Correct |
97 ms |
124908 KB |
Output is correct |
22 |
Correct |
98 ms |
124780 KB |
Output is correct |
23 |
Correct |
98 ms |
124780 KB |
Output is correct |
24 |
Correct |
100 ms |
125124 KB |
Output is correct |
25 |
Correct |
98 ms |
125060 KB |
Output is correct |
26 |
Correct |
99 ms |
124908 KB |
Output is correct |
27 |
Correct |
97 ms |
124908 KB |
Output is correct |
28 |
Correct |
100 ms |
125676 KB |
Output is correct |
29 |
Correct |
100 ms |
125676 KB |
Output is correct |
30 |
Correct |
98 ms |
124908 KB |
Output is correct |
31 |
Correct |
98 ms |
125292 KB |
Output is correct |
32 |
Correct |
99 ms |
125036 KB |
Output is correct |
33 |
Correct |
103 ms |
126700 KB |
Output is correct |
34 |
Correct |
107 ms |
126700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
124524 KB |
Output is correct |
2 |
Correct |
97 ms |
124652 KB |
Output is correct |
3 |
Correct |
97 ms |
124556 KB |
Output is correct |
4 |
Correct |
97 ms |
124524 KB |
Output is correct |
5 |
Correct |
96 ms |
124524 KB |
Output is correct |
6 |
Correct |
97 ms |
124544 KB |
Output is correct |
7 |
Correct |
96 ms |
124524 KB |
Output is correct |
8 |
Correct |
96 ms |
124524 KB |
Output is correct |
9 |
Correct |
99 ms |
124652 KB |
Output is correct |
10 |
Correct |
96 ms |
124652 KB |
Output is correct |
11 |
Correct |
100 ms |
124908 KB |
Output is correct |
12 |
Correct |
98 ms |
124652 KB |
Output is correct |
13 |
Correct |
98 ms |
124652 KB |
Output is correct |
14 |
Correct |
99 ms |
124908 KB |
Output is correct |
15 |
Correct |
98 ms |
124908 KB |
Output is correct |
16 |
Correct |
99 ms |
124860 KB |
Output is correct |
17 |
Correct |
99 ms |
125036 KB |
Output is correct |
18 |
Correct |
98 ms |
124780 KB |
Output is correct |
19 |
Correct |
98 ms |
124652 KB |
Output is correct |
20 |
Correct |
97 ms |
124780 KB |
Output is correct |
21 |
Correct |
99 ms |
124780 KB |
Output is correct |
22 |
Correct |
97 ms |
124780 KB |
Output is correct |
23 |
Correct |
98 ms |
124780 KB |
Output is correct |
24 |
Correct |
98 ms |
125036 KB |
Output is correct |
25 |
Correct |
99 ms |
125036 KB |
Output is correct |
26 |
Correct |
98 ms |
124836 KB |
Output is correct |
27 |
Correct |
99 ms |
124908 KB |
Output is correct |
28 |
Correct |
100 ms |
125676 KB |
Output is correct |
29 |
Correct |
101 ms |
125676 KB |
Output is correct |
30 |
Correct |
99 ms |
125036 KB |
Output is correct |
31 |
Correct |
100 ms |
125292 KB |
Output is correct |
32 |
Correct |
99 ms |
125036 KB |
Output is correct |
33 |
Correct |
103 ms |
126700 KB |
Output is correct |
34 |
Correct |
105 ms |
126572 KB |
Output is correct |
35 |
Correct |
125 ms |
129312 KB |
Output is correct |
36 |
Correct |
102 ms |
125420 KB |
Output is correct |
37 |
Correct |
126 ms |
129312 KB |
Output is correct |
38 |
Correct |
144 ms |
130464 KB |
Output is correct |
39 |
Correct |
136 ms |
130464 KB |
Output is correct |
40 |
Correct |
136 ms |
130464 KB |
Output is correct |
41 |
Correct |
140 ms |
130592 KB |
Output is correct |
42 |
Correct |
105 ms |
125420 KB |
Output is correct |
43 |
Correct |
105 ms |
125344 KB |
Output is correct |
44 |
Correct |
104 ms |
125292 KB |
Output is correct |
45 |
Correct |
171 ms |
134816 KB |
Output is correct |
46 |
Correct |
171 ms |
134816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
124524 KB |
Output is correct |
2 |
Correct |
130 ms |
124524 KB |
Output is correct |
3 |
Correct |
96 ms |
124524 KB |
Output is correct |
4 |
Correct |
92 ms |
124524 KB |
Output is correct |
5 |
Correct |
99 ms |
124652 KB |
Output is correct |
6 |
Correct |
98 ms |
124524 KB |
Output is correct |
7 |
Correct |
99 ms |
124748 KB |
Output is correct |
8 |
Correct |
98 ms |
124524 KB |
Output is correct |
9 |
Correct |
100 ms |
124524 KB |
Output is correct |
10 |
Correct |
101 ms |
124652 KB |
Output is correct |
11 |
Correct |
101 ms |
124904 KB |
Output is correct |
12 |
Correct |
98 ms |
124684 KB |
Output is correct |
13 |
Correct |
103 ms |
124652 KB |
Output is correct |
14 |
Correct |
100 ms |
124908 KB |
Output is correct |
15 |
Correct |
99 ms |
124888 KB |
Output is correct |
16 |
Correct |
102 ms |
124652 KB |
Output is correct |
17 |
Correct |
100 ms |
125164 KB |
Output is correct |
18 |
Correct |
98 ms |
124780 KB |
Output is correct |
19 |
Correct |
98 ms |
124780 KB |
Output is correct |
20 |
Correct |
99 ms |
124780 KB |
Output is correct |
21 |
Correct |
98 ms |
124780 KB |
Output is correct |
22 |
Correct |
98 ms |
124752 KB |
Output is correct |
23 |
Correct |
97 ms |
124780 KB |
Output is correct |
24 |
Correct |
99 ms |
125036 KB |
Output is correct |
25 |
Correct |
100 ms |
125036 KB |
Output is correct |
26 |
Correct |
99 ms |
124908 KB |
Output is correct |
27 |
Correct |
99 ms |
124908 KB |
Output is correct |
28 |
Correct |
101 ms |
125676 KB |
Output is correct |
29 |
Correct |
101 ms |
125676 KB |
Output is correct |
30 |
Correct |
98 ms |
124908 KB |
Output is correct |
31 |
Correct |
98 ms |
125292 KB |
Output is correct |
32 |
Correct |
99 ms |
125036 KB |
Output is correct |
33 |
Correct |
104 ms |
126804 KB |
Output is correct |
34 |
Correct |
105 ms |
126572 KB |
Output is correct |
35 |
Correct |
127 ms |
129184 KB |
Output is correct |
36 |
Correct |
102 ms |
125420 KB |
Output is correct |
37 |
Correct |
129 ms |
129400 KB |
Output is correct |
38 |
Correct |
136 ms |
130464 KB |
Output is correct |
39 |
Correct |
134 ms |
130464 KB |
Output is correct |
40 |
Correct |
137 ms |
130464 KB |
Output is correct |
41 |
Correct |
133 ms |
130464 KB |
Output is correct |
42 |
Correct |
102 ms |
125420 KB |
Output is correct |
43 |
Correct |
104 ms |
125420 KB |
Output is correct |
44 |
Correct |
102 ms |
125292 KB |
Output is correct |
45 |
Correct |
170 ms |
134816 KB |
Output is correct |
46 |
Correct |
168 ms |
134816 KB |
Output is correct |
47 |
Correct |
175 ms |
138528 KB |
Output is correct |
48 |
Correct |
117 ms |
128928 KB |
Output is correct |
49 |
Correct |
113 ms |
128416 KB |
Output is correct |
50 |
Correct |
112 ms |
127560 KB |
Output is correct |
51 |
Correct |
153 ms |
133920 KB |
Output is correct |
52 |
Correct |
159 ms |
134304 KB |
Output is correct |
53 |
Correct |
137 ms |
130848 KB |
Output is correct |
54 |
Correct |
99 ms |
125292 KB |
Output is correct |
55 |
Correct |
99 ms |
125676 KB |
Output is correct |
56 |
Correct |
110 ms |
127084 KB |
Output is correct |
57 |
Correct |
119 ms |
131908 KB |
Output is correct |
58 |
Correct |
107 ms |
127340 KB |
Output is correct |
59 |
Correct |
110 ms |
128536 KB |
Output is correct |
60 |
Correct |
116 ms |
128492 KB |
Output is correct |
61 |
Correct |
112 ms |
128748 KB |
Output is correct |
62 |
Correct |
172 ms |
145824 KB |
Output is correct |
63 |
Correct |
295 ms |
160288 KB |
Output is correct |
64 |
Correct |
333 ms |
168480 KB |
Output is correct |
65 |
Correct |
476 ms |
183840 KB |
Output is correct |
66 |
Correct |
773 ms |
230248 KB |
Output is correct |
67 |
Correct |
783 ms |
230688 KB |
Output is correct |