Ankit_132

E

Jul 21st, 2023
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     ios_base::sync_with_stdio(false);
  12.     cin.tie(NULL);
  13.     cout.tie(NULL);
  14.  
  15.     _test
  16.     {
  17.         ll int n, c;
  18.         cin>>n>>c;
  19.  
  20.         vector<ll int> s(n);
  21.         for(auto &e: s)     cin>>e;
  22.  
  23.         __int128 low=0, high=1e9;
  24.  
  25.         while(low <= high)
  26.         {
  27.             __int128 mid = (low + high)/2;
  28.  
  29.             __int128 val = 0;
  30.  
  31.             for(auto e: s)
  32.                 val += (e+mid*2ll)*1ll*(e+mid*2ll);
  33.  
  34.             if(val == c)
  35.             {
  36.                 cout<<(ll)mid<<"\n";
  37.                 break;
  38.             }
  39.             else if(val < c)
  40.                 low = mid+1;
  41.             else
  42.                 high = mid-1;
  43.         }
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment