Advertisement
MiinaMagdy

10920 - Spiral Tap

Sep 11th, 2022
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define endl '\n'
  7. #define sz(x) int(x.size())
  8. #define all(x) x.begin(), x.end()
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  12.     ll sz, p;
  13.     while (cin >> sz >> p, sz || p) {
  14.         ll x, y;
  15.         x = y = (sz + 1) / 2;
  16.         auto calc = [&](ll rot) {
  17.             return 1 + 2 * rot * rot + 2 * (rot + 1) * (rot);
  18.         };
  19.         auto BS = [&]() {
  20.             ll l = 0, r = 1;
  21.             while (calc(r) <= p) r *= 2;
  22.             ll best = 0;
  23.             while (l <= r) { // 111111111100000000000
  24.                 ll mid = l + (r - l) / 2;
  25.                 (calc(mid) <= p ? l = mid + 1, best = mid : r = mid - 1);
  26.             }
  27.             return best;
  28.         };
  29.         int rot = BS();
  30.         y -= rot;
  31.         x += rot;
  32.  
  33.         ll sum = calc(rot);
  34.         if (sum < p) {
  35.             int con = rot * 2 + 1;
  36.             vector<ll> nums = {con, con, con + 1, con + 1};
  37.             int i = 0;
  38.             for (i = 0; i < 4 && sum + nums[i] <= p; i++) {
  39.                 sum += nums[i];
  40.                 if (i == 0) y += nums[i];
  41.                 if (i == 1) x -= nums[i];
  42.                 if (i == 2) y -= nums[i];
  43.                 if (i == 3) x += nums[i];
  44.             }
  45.             if (sum < p) {
  46.                 if (i == 0) y += p - sum;
  47.                 if (i == 1) x -= p - sum;
  48.                 if (i == 2) y -= p - sum;
  49.                 if (i == 3) x += p - sum;  
  50.             }
  51.         }
  52.         cout << "Line = " << y << ", column = " << x << ".\n";
  53.     }
  54. }
  55.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement