fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. #define int long long
  5. const int MOD = 1000000007;
  6. const int INF = LLONG_MAX/2;
  7. const int MAXN = 100000;
  8. int primes[1000000];
  9.  
  10. void seive(){
  11. fill(primes, primes + 1000000, 1);
  12. primes[0] = primes[1] = 0;
  13. for(int i = 2 ; i*i < 1000000 ; i++){
  14. if(primes[i]){
  15. for(int j = i*i ; j < 1000000 ; j += i){
  16. primes[j] = 0;
  17. }
  18. }
  19. }
  20. }
  21. bool isPrime(int n){
  22. if(n <= 1) return false;
  23. for(int i = 2 ; i*i <= n ; i++){
  24. if(n % i == 0) return false;
  25. }
  26. return true;
  27. }
  28. int gcd(int a, int b){
  29. if(a == 0) return b;
  30. return gcd(b % a, a);
  31. }
  32. int power(int a, int b) {
  33. int res = 1;
  34. a %= MOD;
  35. while (b > 0) {
  36. if (b & 1) res = res * a % MOD;
  37. a = a * a % MOD;
  38. b >>= 1;
  39. }
  40. return res;
  41. }
  42.  
  43. // nCr % MOD for n < MOD
  44. int nCrModP(int n, int r) {
  45. if (r > n) return 0;
  46. if (r == 0 || r == n) return 1;
  47.  
  48. int numerator = 1, denominator = 1;
  49. for (int i = 0; i < r; i++) {
  50. numerator = (numerator * (n - i)) % MOD;
  51. denominator = (denominator * (i + 1)) % MOD;
  52. }
  53. return (numerator * power(denominator, MOD - 2)) % MOD;
  54. }
  55.  
  56. // Lucas's Theorem
  57. int lucas(int n, int r) {
  58. if (r == 0) return 1;
  59. return (lucas(n / MOD, r / MOD) * nCrModP(n % MOD, r % MOD)) % MOD;
  60. }
  61. void solve() {
  62. int n,target;
  63. cin>>n>>target;
  64. int A[n];
  65. for(int i = 0 ; i<n ; i++){
  66. cin>>A[i];
  67. }
  68. if(target%2!=0){
  69. cout<<0<<endl;
  70. }
  71. else{
  72. int totalcnt = 0;
  73. int c1 = 0,c2=0;
  74. for(int i = 0 ; i<n ; i++){
  75. if(A[i]>=0 && A[i]==(target/2)){
  76. c1++;
  77. }
  78. }
  79. for(int i = 0 ; i<n ; i++){
  80. if(A[i]>=0 && A[i]<(target/2)){
  81. c2++;
  82. }
  83. }
  84. totalcnt += (c1*c2);
  85. totalcnt += (c1*(c1-1))/2;
  86. c1 = 0,c2=0;
  87. for(int i = 0 ; i<n ; i++){
  88. if(A[i]<0 && A[i]==(-(target/2))){
  89. c1++;
  90. }
  91. }
  92. for(int i = 0 ; i<n ; i++){
  93. if(A[i]<0 && (A[i]>(-(target/2)))){
  94. c2++;
  95. }
  96. }
  97. totalcnt += (c1*c2);
  98. totalcnt += (c1*(c1-1))/2;
  99. c1 = 0 ,c2 =0;
  100. for(int i = 0 ; i<n ; i++){
  101. if(A[i]>=0 && A[i]==(target/2)){
  102. c1++;
  103. }
  104. }
  105. for(int i = 0 ; i<n ; i++){
  106. if(A[i]<0 && abs(A[i])<=(target/2)){
  107. c2++;
  108. }
  109. }
  110. totalcnt += (c1*c2);
  111. c1 = 0,c2=0;
  112. for(int i = 0 ; i<n ; i++){
  113. if(A[i]<0 && A[i]==(-(target/2))){
  114. c1++;
  115. }
  116. }
  117. for(int i = 0 ; i<n ; i++){
  118. if(A[i]>=0 && abs(A[i])<(target/2)){
  119. c2++;
  120. }
  121. }
  122. totalcnt += (c1*c2);
  123. cout<<totalcnt<<endl;
  124. }
  125. }
  126. signed main(){
  127. ios::sync_with_stdio(false); cin.tie(NULL);
  128. //int t;
  129. //cin >> t;
  130. //while(t--){
  131. solve();
  132. //}
  133. return 0;
  134. }
Success #stdin #stdout 0.01s 5316KB
stdin
4 4
1 4 -1 2
stdout
2