-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1082A_VasyaAndBook.cpp
More file actions
68 lines (52 loc) · 1.29 KB
/
1082A_VasyaAndBook.cpp
File metadata and controls
68 lines (52 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//* 211007880 Jun/25/2023 16:17UTC+7 Minh4893IT A - Vasya and Book GNU C++17 Accepted 30 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ss stringstream
#define uset unordered_set
#define umap unordered_map
#define mmap multimap
#define mset multiset
#define pq priority_queue
#define endl "\n"
#define LLMAX INT64_MAX
#define GET(var) getline(cin, var)
#define EACH(x, a) for (auto &x : a)
#define IF(cond, t, f) (cond ? t : f)
#define OFILE(finp, fout) freopen(finp, "r", stdin), freopen(fout, "w", stdout)
#define FAST_IO ios_base::sync_with_stdio(false), cin.tie()
int calc(int start, int stop, int step)
{
if (abs(start - stop) % step)
return LLMAX;
return abs(start - stop) / step;
}
void solve()
{
int x, y, n, d;
cin >> n >> x >> y >> d;
if ((y - x) % d == 0)
{
cout << abs(x - y) / d << endl;
return;
}
int res1 = calc(1, y, d);
if (res1 != LLMAX)
res1 += (x - 1 + d - 1) / d;
int res2 = calc(n, y, d);
if (res2 != LLMAX)
res2 += (n - x + d - 1) / d;
if (min(res1, res2) != LLMAX)
cout << min(res1, res2) << endl;
else
cout << -1 << endl;
}
signed main()
{
FAST_IO;
int t;
cin >> t;
while (t--)
solve();
return 0;
}