fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define dd double
  5. #define ld long double
  6. #define pi 3.14159265359
  7. #define ull unsigned long long
  8. #define yes cout<<"YES\n"
  9. #define no cout<<"NO\n"
  10. #define el "\n"
  11. #define Arwa ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  12. #define fix(x) cout<<fixed<<setprecision(x)
  13. ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
  14. ll lcm(int a,int b) {return a/gcd(a,b)*b;}
  15. bool prime(ll n)
  16. {
  17. if(n==2) return true;
  18. if(n%2==0||n==1) return false;
  19. else { for(ll i=3; i*i<=n; i+=2) {if(n%i==0) return false;}}return true;
  20. }
  21. //here we go again
  22. int main()
  23. {
  24. Arwa
  25. int t=1;
  26. //cin>>t;
  27. while(t--)
  28. {
  29. int n,k,neg[1000001]={},pos[1000001]={};
  30. ll c=0;
  31. cin>>n>>k;
  32. int arr[n];
  33. for(int i=0;i<n;i++)
  34. cin>>arr[i];
  35. for(int i=0;i<n;i++)
  36. {
  37. if(arr[i]<0)
  38. neg[abs(arr[i])]++;
  39. else pos[arr[i]]++;
  40. }
  41. for(int i=0;i<n;i++)
  42. {
  43. if(neg[abs(arr[i])]&&arr[i]<0)
  44. {
  45. if(pos[k-arr[i]]&&k-arr[i]>=0)
  46. {
  47. c+=pos[k-arr[i]];
  48. neg[abs(arr[i])]--;
  49. }
  50. if(neg[k-arr[i]]&&k-arr[i]<0)
  51. {
  52. c+=neg[abs(k-arr[i])];
  53. neg[abs(arr[i])]--;
  54. }
  55. }
  56. if(pos[arr[i]]&&arr[i]>=0)
  57. {
  58. if(pos[k-arr[i]]&&k-arr[i]>=0)
  59. {
  60. c+=pos[k-arr[i]];
  61. pos[arr[i]]--;
  62. }
  63. if(neg[abs(k-arr[i])]&&k-arr[i]<0)
  64. {
  65. c+=neg[abs(k-arr[i])];
  66. pos[arr[i]]--;
  67. }
  68. }
  69. }
  70. cout<<c<<el;
  71. }
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 11344KB
stdin
Standard input is empty
stdout
0