Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. typedef float num;
  10.  
  11. template<typename container> void dump(container c){
  12. dump(c.begin(), c.end());
  13. }
  14.  
  15. template<typename iterator> void dump(iterator begin, iterator end){
  16. for(iterator i = begin; i != end; i++){
  17. cout.width(2);
  18. cout << *i << ' ';
  19. }
  20. }
  21.  
  22. template<typename T>
  23. class expression {
  24. public:
  25. enum precedence_t {
  26. p_plusmin,
  27. p_muldiv,
  28. p_none
  29. } predence;
  30.  
  31.  
  32.  
  33. }
  34.  
  35. inline ostream & operator<<(ostream & out, const expression & e){
  36. e.write(out);
  37. return out;
  38. }
  39.  
  40. pair<num,pair<char,string> > operator+(pair<num,pair<char,string> > a, pair<num,pair<char,string> > b){
  41. return make_pair(a.first+b.first,make_pair('+',a.second.second+"+"+b.second.second));
  42. }
  43. pair<num,pair<char,string> > operator-(pair<num,pair<char,string> > a, pair<num,pair<char,string> > b){
  44. if (b.second.first == '+') b.second.second = "\\left(" + b.second.second + "\\right)";
  45. return make_pair(a.first-b.first,make_pair('+',a.second.second+"-"+b.second.second));
  46. }
  47. pair<num,pair<char,string> > operator*(pair<num,pair<char,string> > a, pair<num,pair<char,string> > b){
  48. if (a.second.first == '+') a.second.second = "\\left(" + a.second.second + "\\right)";
  49. if (b.second.first == '+') b.second.second = "\\left(" + b.second.second + "\\right)";
  50. return make_pair(a.first*b.first,make_pair('*',a.second.second+"\\times"+b.second.second));
  51. }
  52. pair<num,pair<char,string> > operator/(pair<num,pair<char,string> > a, pair<num,pair<char,string> > b){
  53. return make_pair(a.first/b.first,make_pair('*',"\\frac{"+a.second.second+"}{"+b.second.second+"}"));
  54. }
  55.  
  56. template<typename inset, typename outset>
  57. void allexpressions(const inset numbers, outset & results, size_t max_numbers = (size_t)-1){
  58. assert(results.empty());
  59. if (numbers.size() == 1){
  60. results.insert(*numbers.begin());
  61. } else {
  62. static map<inset,outset> cache;
  63. if (numbers.size() < max_numbers){
  64. typename map<inset,outset>::const_iterator cache_i = cache.find(numbers);
  65. if (cache_i != cache.end()){
  66. results = cache_i->second;
  67. return;
  68. }
  69. }
  70. for(int mask = 1; mask < (1 << (numbers.size()-1)); mask++){
  71. inset a_in, b_in;
  72. typename inset::const_iterator i = numbers.begin();
  73. for(size_t n = 0; i != numbers.end(); n++) (mask & (1<<n) ? a_in : b_in).insert(*i++);
  74. outset a_out, b_out;
  75. allexpressions(a_in, a_out);
  76. allexpressions(b_in, b_out);
  77. for(typename outset::const_iterator a = a_out.begin(); a != a_out.end(); a++)
  78. for(typename outset::const_iterator b = b_out.begin(); b != b_out.end(); b++){
  79. results.insert(*a + *b);
  80. results.insert(*b + *a);
  81. results.insert(*a - *b);
  82. results.insert(*b - *a);
  83. results.insert(*a * *b);
  84. results.insert(*b * *a);
  85. results.insert(*a / *b);
  86. results.insert(*b / *a);
  87. }
  88. }
  89. if (numbers.size() < max_numbers) cache.insert(make_pair(numbers,results));
  90. return;
  91. }
  92. }
  93.  
  94. template<typename inset, typename outset>
  95. typename outset::key_type maximumpossiblevalue(const inset numbers){
  96. outset values;
  97. allexpressions(numbers,values,numbers.size());
  98. typename outset::key_type max = 0;
  99. while(values.find(++max) != values.end());
  100. return max-1;
  101. }
  102.  
  103. num maxsum = 0;
  104.  
  105. size_t countsumsets(num sum, size_t size, multiset<num> values){
  106. num max = values.empty() ? sum/* - (size - 1)*/ : *values.begin();
  107. if (size == 1){
  108. if (sum >= 0 && sum <= max){
  109. values.insert(sum);
  110. num sum = maximumpossiblevalue<multiset<num>,set<num> >(values);
  111. dump(values);
  112. cout << ": " << sum << endl;
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. size_t count = 0;
  118. for(num i = max; i >= 0; i--){
  119. multiset<num> x = values;
  120. x.insert(i);
  121. count += countsumsets(sum-i, size-1, x);
  122. }
  123. return count;
  124. }
  125.  
  126. void printalllatexexpressions(multimap<num,pair<char,string> > values){
  127. set<pair<num,pair<char,string> > > expressions;
  128. allexpressions(values,expressions,values.size());
  129. size_t c = 0;
  130. num max = 0;
  131. cout << "\\documentclass{article}" << endl
  132. << "\\pagestyle{empty}" << endl
  133. << "\\usepackage{amsmath}" << endl
  134. << "\\begin{document}" << endl;
  135. //<< "\\Huge" << endl;
  136. /*cout << "\\begin{align*}" << endl;*/
  137. set<pair<num,pair<char,string> > >::iterator i = expressions.begin();
  138. while(true){
  139. max++;
  140. while(i != expressions.end() && i->first < max) i++;
  141. if (i == expressions.end() || i->first != max) break;
  142. if (c++) cout << "\\newpage" << endl;
  143. cout << "\\vbox to \\vsize{\\vfill"
  144. << "$\\displaystyle" << i->first << "=" << i->second.second << "$";
  145. while(++i != expressions.end() && i->first == max){
  146. cout << "$\\displaystyle=" << i->second.second << "$";
  147. }
  148. cout << "\\vfill}" << endl;
  149. /*if (c % 30 == 0){
  150. cout << endl << "\\end{align*}" << endl << "\\begin{align*}" << endl;
  151. } else {
  152. cout << "\\\\" << endl;
  153. }*/
  154. }
  155. cout << "\\end{document}" << endl;
  156. /*cout << "\\end{align*}" << endl;*/
  157. }
  158.  
  159. int main(){
  160. multiset<num> x;
  161. cout << countsumsets(50,5,x) << endl;
  162. /*multimap<num,pair<char,string> > x;
  163. x.insert(make_pair((num)2,make_pair(' ',string("2"))));
  164. x.insert(make_pair((num)4,make_pair(' ',string("4"))));
  165. x.insert(make_pair((num)9,make_pair(' ',string("9"))));
  166. x.insert(make_pair((num)12,make_pair(' ',string("12"))));
  167. x.insert(make_pair((num)23,make_pair(' ',string("23"))));
  168. printalllatexexpressions(x);*/
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement