fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // leetcode 323
  7.  
  8. int threeSumMulti(vector<int>& nums, int target) {
  9. int cnt = 0;
  10. int mod=1e9+7;
  11. unordered_map<int, int> mpp;
  12.  
  13.  
  14. for (int k = 2; k < nums.size(); k++) {
  15. int x = target - nums[k];
  16.  
  17.  
  18. for (int j = 0; j < k; j++) {
  19. int find = x - nums[j];
  20. if (mpp.count(find)) {
  21. cnt = (cnt + mpp[find])%mod;
  22.  
  23.  
  24. }
  25. mpp[nums[j]]++;
  26.  
  27. }
  28. mpp.clear();
  29.  
  30. }
  31. return cnt;
  32. }
  33.  
  34.  
  35.  
  36. int main() {
  37. int n,target;
  38. cin>>n>>target;
  39. vector<int> nums(n);
  40. for(int i=0;i<n;i++){
  41. cin>>nums[i];
  42. }
  43.  
  44. int a = threeSumMulti(nums,target);
  45. cout<<a;
  46.  
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5324KB
stdin
10 8
1 1 2 2 3 3 4 4 5 5
stdout
20