Advertisement
sajid161

17:1

May 2nd, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll function_value(ll n)
  5. {
  6.     return (n*(n+1))/2;
  7. }
  8. ll find_num(ll y)
  9. {
  10.     ll l=1,r=100000;
  11.     bool flg=0;
  12.     while(l<=r)
  13.     {
  14.         ll mid=(l+r)/2;
  15.         if(function_value(mid)==y)
  16.         {
  17.             flg=1;
  18.             return mid;
  19.         }
  20.         else if(function_value(mid)<y)
  21.         {
  22.             l=mid+1;
  23.         }
  24.         else r=mid-1;
  25.  
  26.     }
  27.     if(flg==0) return -1;
  28.  
  29. }
  30. int main()
  31. {
  32.     int t;
  33.     cin>>t;
  34.     while(t--)
  35.     {
  36.         ll y;
  37.         cin>>y;
  38.         if(find_num(y)==-1) cout<<"NAI\n";
  39.         else cout<<find_num(y)<<"\n";
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement