This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
#pragma GCC target("popcnt")
const int mxN=30001;
const int mod=998244353;
const int mxlogN=40;
const int mxK=26;
const int inf=1e9;
const int K=600;
vector<int> who[mxN];
int n, m, a[mxN], b[mxN];
vector<int> who2[mxN];
vector<int> have;
int pos(int x)
{
return lower_bound(have.begin(),have.end(),x)-have.begin();
}
int id(int x, int p)
{
return (p-1)*n+x;
}
pair<int,int> rev(int i)
{
i=have[i];
return {i%n,i/n+1};
}
int idx(int x, int p)
{
return pos(id(x,p));
}
bool vis[6000000];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i<m; i++) cin >> a[i] >> b[i];
for(int i=0; i<m; i++) who2[b[i]].push_back(a[i]%b[i]);
for(int i=1; i<mxN; i++)
{
sort(who2[i].begin(),who2[i].end());
who2[i].erase(unique(who2[i].begin(),who2[i].end()),who2[i].end());
for(int k=0; k*i<n; k++)
{
for(int j:who2[i])
{
if(j+k*i<n) have.push_back(id(j+k*i,i));
}
}
}
int sz=have.size();
if(sz>=6000000) return 1;
for(int i=1; i<m; i++) who[a[i]].push_back(i);
vector<int> q, q2;
q.push_back(idx(a[0],b[0]));
vis[idx(a[0],b[0])]=1;
int d=0;
while(q.size())
{
int szz=q.size();
for(int o=0; o<szz; o++)
{
int wh=q[o];
auto par=rev(wh);
int x=par.first, p=par.second;
while(who[x].size())
{
int j=who[x].back(); who[x].pop_back();
if(j==1)
{
cout << d << "\n";
return 0;
}
int id2=idx(a[j],b[j]);
if(!vis[id2])
{
q.push_back(id2);
vis[id2]=1;
}
}
}
for(int wh:q)
{
auto par=rev(wh);
int x=par.first, p=par.second;
int id2;
if(x-p>=0)
{
id2=idx(x-p,p);
if(!vis[id2])
{
q2.push_back(id2);
vis[id2]=1;
}
}
if(x+p<n)
{
id2=idx(x+p,p);
if(!vis[id2])
{
q2.push_back(id2);
vis[id2]=1;
}
}
}
q=q2;
q2.clear();
d++;
}
cout << "-1" << "\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:77:30: warning: unused variable 'p' [-Wunused-variable]
77 | int x=par.first, p=par.second;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |