Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- typedef unsigned long long int ull;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<vi> vvi;
- typedef pair<int,int> pii;
- typedef vector< pii > vpii;
- typedef set<int> sti;
- #define pb push_back
- #define pob pop_back
- #define mp make_pair
- #define ff first
- #define ss second
- #define loop1(i,n) for(int i=1; i<=int(n); i++)
- #define loop(i,y) for(int i=0; i<int(y); i++)
- #define rloop(i,y) for(int i=int(y); i>0; i--)
- #define rloop1(i,y) for(int i=int(y)-1; i>=0; i--)
- #define read() freopen("input.txt", "r", stdin)
- #define write() freopen("output.txt", "w", stdout)
- #define cspf(i) printf("Case %d: ", i)
- #define all(v) v.begin(),v.end()
- /// Constants
- #define eps 1e-9
- #define PI acos(-1.0) // 3.1415926535897932
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- int t;
- cin>>t;
- loop1(i,t){
- ll n,x,y,d,a,b,w,sm,sm1,ds,ydf,yde,xdf,xde;
- cin>>n>>x>>y>>d;
- if(x==y){
- cout<<"0"<<endl;
- continue;
- }
- ds=abs(x-y);
- if(ds%d==0){
- sm=ds/d;
- cout<<sm<<endl;
- continue;
- }
- ydf=abs(y-1);
- yde=abs(y-n);
- xdf=abs(x-1);
- xde=abs(x-n);
- if(xdf%d==0 && xde%d==0){
- cout<<"-1"<<endl;
- continue;
- }
- if(ydf%d==0 && yde%d==0){
- a=ceil((double)xdf/d);
- b=ydf/d;
- sm=a+b;
- a=ceil((double)xde/d);
- b=yde/d;
- sm1=a+b;
- if(sm>sm1)
- sm=sm1;
- cout<<sm<<endl;
- continue;
- }
- if(ydf%d==0){
- a=ceil((double)xdf/d);
- b=ydf/d;
- sm=a+b;
- cout<<sm<<endl;
- continue;
- }
- if(yde%d==0){
- a=ceil((double)xde/d);
- b=yde/d;
- sm=a+b;
- cout<<sm<<endl;
- continue;
- }
- cout<<"-1"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement