mfgnik

Untitled

Jan 16th, 2021
617
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <limits>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int64_t CalculateAll(int64_t m, int64_t n) {
  9.     return (1 + m * n) * m * n / 2;
  10. }
  11.  
  12. int64_t CalculateHorizontal(int64_t m, int64_t n, int64_t k) {
  13.     int64_t first_half = (1 + k*m) * k*m / 2;
  14.     return abs(2 * first_half - CalculateAll(m, n));
  15. }
  16.  
  17. int64_t CalculateVertically(int64_t m, int64_t n, int64_t k) {
  18.     int64_t first_column = (1  + m * (n - 1) + 1) * n / 2;
  19.     int64_t first_half = (2 * first_column + n * (k - 1)) * k / 2;
  20.     return abs(2 * first_half - CalculateAll(m, n));
  21. }
  22.  
  23. int main() {
  24.     int t;
  25.     std::cin >> t;
  26.     while (t--) {
  27.         int64_t m, n;
  28.         std::cin >> n >> m;
  29.         int64_t horizontal_result = std::numeric_limits<int64_t>::max();
  30.         int64_t vertical_result = std::numeric_limits<int64_t>::max();
  31.         int64_t min_horizontal = 0, min_vertically = 0;
  32.         for (int k = 1; k < n; ++k) {
  33.             int64_t new_horizontal = CalculateHorizontal(m , n, k);
  34.             if (new_horizontal < horizontal_result) {
  35.                 horizontal_result = new_horizontal;
  36.                 min_horizontal = k;
  37.             }
  38.         }
  39.         for (int k = 1; k < m; ++k) {
  40.             int64_t new_vertical = CalculateVertically(m , n, k);
  41.             if (new_vertical < vertical_result) {
  42.                 vertical_result = new_vertical;
  43.                 min_vertically = k;
  44.             }
  45.         }
  46.         if (vertical_result < horizontal_result) {
  47.             std::cout << "V " << min_vertically - 1 << "\n";
  48.         } else {
  49.             std::cout << "H " << min_horizontal - 1 << "\n";
  50.         }
  51.     }
  52. }
  53.  
RAW Paste Data