#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MOD 998244353
const int N = 30005;
int b[N], p[N], f[N];
vector<int> power[N];
void solve()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
}
for (int i = 0; i < m; i++)
power[b[i]].push_back(p[i]);
memset(f, 63, sizeof f);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
q.push({f[b[0]] = 0, b[0]});
while (q.size())
{
auto [d, u] = q.top();
q.pop();
if (d != f[u])
{
continue;
}
for (auto pw : power[u])
{
int w = d;
for (int v = u + pw; v < n; v += pw)
{
w++;
if (f[v] > w)
{
f[v] = w;
q.push({w, v});
}
}
w = d;
for (int v = u - pw; v >= 0; v -= pw)
{
w++;
if (f[v] > w)
{
f[v] = w;
q.push({w, v});
}
}
}
}
if (f[b[1]] >= f[n])
{
cout << -1 << '\n';
return;
}
cout << f[b[1]] << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
// cout << fixed << setprecision(12);
for (ll i = 1; i <= t; i++)
{
solve();
}
}
| # | 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... |