Advertisement
erfanul007

Untitled

Jan 14th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. typedef unsigned long long int ull;
  6. typedef vector<int> vi;
  7. typedef vector<ll> vll;
  8. typedef vector<vi> vvi;
  9. typedef pair<int,int> pii;
  10. typedef vector< pii > vpii;
  11. typedef set<int> sti;
  12.  
  13. #define pb push_back
  14. #define pob pop_back
  15. #define mp make_pair
  16. #define ff first
  17. #define ss second
  18. #define loop1(i,n) for(int i=1; i<=int(n); i++)
  19. #define loop(i,y) for(int i=0; i<int(y); i++)
  20. #define rloop(i,y) for(int i=int(y); i>0; i--)
  21. #define rloop1(i,y) for(int i=int(y)-1; i>=0; i--)
  22. #define read() freopen("input.txt", "r", stdin)
  23. #define write() freopen("output.txt", "w", stdout)
  24. #define cspf(i) printf("Case %d: ", i)
  25. #define all(v) v.begin(),v.end()
  26. /// Constants
  27. #define eps 1e-9
  28. #define PI acos(-1.0) // 3.1415926535897932
  29.  
  30. #include<stdio.h>
  31. #include<math.h>
  32. int main()
  33. {
  34. int t;
  35. cin>>t;
  36. loop1(i,t){
  37. ll n,x,y,d,a,b,w,sm,sm1,ds,ydf,yde,xdf,xde;
  38. cin>>n>>x>>y>>d;
  39. if(x==y){
  40. cout<<"0"<<endl;
  41. continue;
  42. }
  43. ds=abs(x-y);
  44. if(ds%d==0){
  45. sm=ds/d;
  46. cout<<sm<<endl;
  47. continue;
  48. }
  49. ydf=abs(y-1);
  50. yde=abs(y-n);
  51. xdf=abs(x-1);
  52. xde=abs(x-n);
  53. if(xdf%d==0 && xde%d==0){
  54. cout<<"-1"<<endl;
  55. continue;
  56. }
  57. if(ydf%d==0 && yde%d==0){
  58. a=ceil((double)xdf/d);
  59. b=ydf/d;
  60. sm=a+b;
  61. a=ceil((double)xde/d);
  62. b=yde/d;
  63. sm1=a+b;
  64. if(sm>sm1)
  65. sm=sm1;
  66. cout<<sm<<endl;
  67. continue;
  68. }
  69. if(ydf%d==0){
  70. a=ceil((double)xdf/d);
  71. b=ydf/d;
  72. sm=a+b;
  73. cout<<sm<<endl;
  74. continue;
  75. }
  76. if(yde%d==0){
  77. a=ceil((double)xde/d);
  78. b=yde/d;
  79. sm=a+b;
  80. cout<<sm<<endl;
  81. continue;
  82. }
  83. cout<<"-1"<<endl;
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement