fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n;
  10. cin >> n;
  11. map<ll, ll> m;
  12. for(int i = 0; i < n; i++){
  13. ll x;
  14. cin >> x;
  15. m[x]++;
  16. }
  17.  
  18. int q;
  19. cin >> q;
  20. while(q--){
  21. ll x, y;
  22. cin >> x >> y;
  23. if(x * x - 4 * y < 0){
  24. cout << 0 << " ";
  25. continue;
  26. }
  27. double p = sqrtl(x * x - 4 * y);
  28. // cout << p << "\n";
  29. double root1 = x + p;
  30. double root2 = x - p;
  31.  
  32. if((root1 - (ll)root1) <= 1e-6 && (root2 - (ll)root2) <= 1e-6){
  33. ll r1 = root1, r2 = root2;
  34.  
  35. if(r1 % 2 != 0 || r2 % 2 != 0){
  36. cout << 0 << " ";
  37. }else{
  38. r1 /= 2;
  39. r2 /= 2;
  40. if(r1 + r2 != x || r1 * r2 != y){
  41. cout << 0 << " ";
  42. }
  43. else if(r1 == r2){
  44. cout << m[r1] * (m[r1] - 1) / 2 << " ";
  45. }else{
  46. cout << m[r1] * m[r2] << " ";
  47. }
  48. }
  49. }else{
  50. cout << 0 << " ";
  51. }
  52.  
  53. }
  54. cout << "\n";
  55. }
  56.  
  57. int main(){
  58. ios_base::sync_with_stdio(false);
  59. cin.tie(nullptr);
  60.  
  61. int t = 1;
  62. cin >> t;
  63. cout << fixed << setprecision(15);
  64.  
  65. for(int i = 1; i <= t; i++){
  66. solve();
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5280KB
stdin
3
3
1 3 2
4
3 2
5 6
3 1
5 5
4
1 1 1 1
1
2 1
6
1 4 -2 3 3 3
3
2 -8
-1 -2
7 12
stdout
1 1 0 0 
6 
1 1 3