Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fixed(n) fixed << setprecision(n)
- #define ceil(n, m) (((n) + (m) - 1) / (m))
- #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
- #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
- #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define sz(x) int(x.size())
- #define debug(x) cout << #x << ": " << (x) << "\n";
- #define fi first
- #define se second
- #define ll long long
- #define ull unsigned long long
- #define EPS 1e-9
- constexpr int INF = 1 << 30, Mod = 1e9 + 7;
- constexpr ll LINF = 1LL << 62;
- #define PI acos(-1)
- template < typename T = int > using Pair = pair < T, T >;
- vector < string > RET = {"NO", "YES"};
- template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
- for (auto &x : v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
- for (const T &x : v) out << x << ' ';
- return out;
- }
- ll is_pefrect_power(ll x){
- ll sqx = sqrt(x);
- return sqx * sqx == x;
- }
- Pair < ll > get_pairs(ll x, ll y){
- ll a = 1, b = -x, c = y;
- // Calculate discriminant
- ll discriminant = b * b - 4 * a * c;
- // check if discriminant is perfect square
- if(!is_pefrect_power(discriminant)) return {-LINF, -LINF};
- discriminant = sqrt(discriminant);
- // Check if roots are real or complex
- if (discriminant > 0) {
- ll root1 = -LINF, root2 = -LINF;
- if((-b + discriminant) % (2 * a) == 0)
- root1 = (-b + discriminant) / (2 * a);
- if((-b - discriminant) % (2 * a) == 0)
- root2 = (-b - discriminant) / (2 * a);
- return {root1, root2};
- } else if (discriminant == 0) {
- double root = -b / (2 * a);
- return {root, root};
- } else {
- return {-LINF, -LINF};
- }
- }
- void Solve(){
- int n;
- cin >> n;
- vector < ll > a(n);
- cin >> a;
- map < ll, ll > freq;
- for(auto &x : a) freq[x]++;
- int q;
- cin >> q;
- auto sum = [&](ll x){
- return (x * (x - 1)) / 2;
- };
- while(q--){
- ll x, y;
- cin >> x >> y;
- auto [x1, x2] = get_pairs(x, y);
- if(x1 == -LINF)
- cout << 0 << " ";
- else if(x1 == x2){
- auto it = freq.find(x1);
- if(it != freq.end())
- cout << sum(it -> se) << " ";
- else
- cout << 0 << " ";
- }else {
- auto it1 = freq.find(x1);
- auto it2 = freq.find(x2);
- if(it1 != freq.end() && it2 != freq.end())
- cout << it1 -> se * it2 -> se << " ";
- else
- cout << 0 << " ";
- }
- }
- cout << "\n";
- }
- int main(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- int test_cases = 1;
- cin >> test_cases;
- for(int tc = 1; tc <= test_cases; tc++){
- // cout << "Case #" << tc << ": ";
- Solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement