Advertisement
a53

meet-in-the-middle

a53
Apr 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /// https://www.infoarena.ro/blog/meet-in-the-middle
  2. /// ptr SET http://www.cplusplus.com/reference/set/set/
  3. #include <iostream>
  4. #include <set>
  5. using namespace std;
  6. set <int> S,S1,S2;
  7.  
  8. int main()
  9. {
  10. int b,sum;
  11. cin>>b>>sum;
  12. for(int p1=1;p1<=b;++p1)
  13. for(int p2=1;p2<=b;++p2)
  14. S.insert(p1+p2),S1.insert(p1),S2.insert(p2);
  15. std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret1,ret2;
  16. for(int p3=1;p3<b;++p3)
  17. for(int p4=1;p4<b;++p4)
  18. if(sum-(p3+p4)<=*S.rbegin())
  19. {
  20. ret1=S1.equal_range(p3);
  21. ret2=S2.equal_range(p4);
  22. cout<<*ret1.first<<' '<<*ret2.first<<' '<<p3<<' '<<p4<<endl;
  23. }
  24. return 0;
  25. }
  26.  
  27. /*
  28. def 4sum(A):
  29. sums = {}
  30. for a in A:
  31. for b in A:
  32. sums[a + b] = (a, b)
  33.  
  34. for c in A:
  35. for d in A:
  36. if -(c + d) in sums:
  37. print (sums[-(c + d)][0], sums[-(c + d)][1], c, d)
  38. return
  39.  
  40. print "No solution."
  41. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement