Ankit_132

F

Aug 7th, 2023
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 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. #define ff     first
  9. #define ss     second
  10. #define pb     push_back
  11.  
  12. int main()
  13. {
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(NULL);
  16.     cout.tie(NULL);
  17.  
  18.     _test
  19.     {
  20.         int n;
  21.         cin>>n;
  22.  
  23.         vector<int> a(n);
  24.         for(auto &e: a)
  25.             cin>>e;
  26.  
  27.         int q;
  28.         cin>>q;
  29.  
  30.         set<int> roots;
  31.  
  32.         map<int, ll int> cnt;
  33.         for(auto e: a)
  34.             cnt[e]++;
  35.  
  36.         int aj;
  37.  
  38.         while(q--)
  39.         {
  40.             ll int x, y;
  41.             cin>>x>>y;
  42.  
  43.             if(x*x < 4*y)
  44.             {
  45.                 cout<<0<<"\n";
  46.                 continue;
  47.             }
  48.  
  49.             ll int val = x*x - 4*y;
  50.             ll int sq = sqrtl(val);
  51.  
  52.             if(val != sq*sq)
  53.             {
  54.                 cout<<0<<"\n";
  55.                 continue;
  56.             }
  57.  
  58.             roots.clear();
  59.  
  60.             if((x+sq)%2 == 0)  roots.insert((x + sq)/2);
  61.             if((x-sq)%2 == 0)  roots.insert((x - sq)/2);
  62.  
  63.             ll int ans = 0;
  64.  
  65.             for(auto ai: roots)
  66.             {
  67.                 aj = x-ai;
  68.  
  69.                 ans += (cnt[ai]*cnt[aj]);
  70.  
  71.                 if(ai == aj)    ans -= cnt[ai];
  72.             }
  73.  
  74.             cout<<ans/2<<" ";
  75.         }
  76.         cout<<"\n";
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment