Advertisement
7oSkaaa

Untitled

Aug 7th, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fixed(n) fixed << setprecision(n)
  6. #define ceil(n, m) (((n) + (m) - 1) / (m))
  7. #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
  8. #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
  9. #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
  10. #define all(vec) vec.begin(), vec.end()
  11. #define rall(vec) vec.rbegin(), vec.rend()
  12. #define sz(x) int(x.size())
  13. #define debug(x) cout << #x << ": " << (x) << "\n";
  14. #define fi first
  15. #define se second
  16. #define ll long long
  17. #define ull unsigned long long
  18. #define EPS 1e-9
  19. constexpr int INF = 1 << 30, Mod = 1e9 + 7;
  20. constexpr ll LINF = 1LL << 62;
  21. #define PI acos(-1)
  22. template < typename T = int > using Pair = pair < T, T >;
  23. vector < string > RET = {"NO", "YES"};
  24.  
  25. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  26.     for (auto &x : v) in >> x;
  27.     return in;
  28. }
  29.  
  30. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  31.     for (const T &x : v) out << x << ' ';
  32.     return out;
  33. }
  34.  
  35. ll is_pefrect_power(ll x){
  36.     ll sqx = sqrt(x);
  37.     return sqx * sqx == x;
  38. }
  39.  
  40. Pair < ll > get_pairs(ll x, ll y){
  41.     ll a = 1, b = -x, c = y;
  42.  
  43.     // Calculate discriminant
  44.     ll discriminant = b * b - 4 * a * c;
  45.  
  46.     // check if discriminant is perfect square
  47.     if(!is_pefrect_power(discriminant)) return {-LINF, -LINF};
  48.  
  49.     discriminant = sqrt(discriminant);
  50.  
  51.     // Check if roots are real or complex
  52.     if (discriminant > 0) {
  53.         ll root1 = -LINF, root2 = -LINF;
  54.         if((-b + discriminant) % (2 * a) == 0)
  55.             root1 = (-b + discriminant) / (2 * a);
  56.         if((-b - discriminant) % (2 * a) == 0)
  57.             root2 = (-b - discriminant) / (2 * a);
  58.         return {root1, root2};
  59.     } else if (discriminant == 0) {
  60.         double root = -b / (2 * a);
  61.         return {root, root};
  62.     } else {
  63.         return {-LINF, -LINF};
  64.     }
  65. }
  66.  
  67. void Solve(){
  68.     int n;
  69.     cin >> n;
  70.     vector < ll > a(n);
  71.     cin >> a;
  72.     map < ll, ll > freq;
  73.     for(auto &x : a) freq[x]++;
  74.     int q;
  75.     cin >> q;
  76.     auto sum = [&](ll x){
  77.         return (x * (x - 1)) / 2;
  78.     };
  79.     while(q--){
  80.         ll x, y;
  81.         cin >> x >> y;
  82.         auto [x1, x2] = get_pairs(x, y);
  83.         if(x1 == -LINF)
  84.             cout << 0 << " ";
  85.         else if(x1 == x2){
  86.             auto it = freq.find(x1);
  87.             if(it != freq.end())
  88.                 cout << sum(it -> se) << " ";
  89.             else
  90.                 cout << 0 << " ";
  91.         }else {
  92.             auto it1 = freq.find(x1);
  93.             auto it2 = freq.find(x2);
  94.             if(it1 != freq.end() && it2 != freq.end())
  95.                 cout << it1 -> se * it2 -> se << " ";
  96.             else
  97.                 cout << 0 << " ";
  98.         }
  99.     }
  100.     cout << "\n";
  101. }
  102.  
  103. int main(){
  104.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  105.     int test_cases = 1;
  106.     cin >> test_cases;
  107.     for(int tc = 1; tc <= test_cases; tc++){
  108.         // cout << "Case #" << tc << ": ";
  109.         Solve();
  110.     }
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement