Advertisement
Guest User

Untitled

a guest
May 21st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int plusToN(int n) {
  6.     return (1 + n) * n / 2;
  7. }
  8.  
  9. int getAns(int j, int k) {
  10.     int halfup = plusToN(j);
  11.     if (k > halfup) k = j * j - k + 1;
  12.     //
  13.     int m = (int) floor(sqrt(k*2));
  14.     for (int i = m - 1;;++i) {
  15.         int low = plusToN(i), high = plusToN(i + 1);
  16.         if (low < k && k <= high) {
  17.             int index = k - low;
  18.             int maxNum = (i + 1) / 2;
  19.             if (index > maxNum) index = i + 1 - index + 1;
  20.             return index;
  21.         }
  22.     }
  23. }
  24.  
  25. int main() {
  26.     int times, j, k;
  27.     cin >> times;
  28.     while(times--) {
  29.         cin >> j >> k;
  30.         cout << getAns(j, k) << endl;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement