fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cctype>
  5. #include <ctime>
  6. #include <random>
  7. #include <iterator>
  8. #include <algorithm>
  9.  
  10. const int combs3[] = {
  11. 1,3, 5,1,3, 6,1,3, 7,1,3, 8,1,3, 9,1,3,10,1,4, 6,1,4, 7,
  12. 1,4, 8,1,4, 9,1,4,10,1,5, 7,1,5, 8,1,5, 9,1,5,10,1,6, 8,
  13. 1,6, 9,1,6,10,1,7, 9,1,7,10,1,8,10,2,4, 6,2,4, 7,2,4, 8,
  14. 2,4, 9,2,4,10,2,5, 7,2,5, 8,2,5, 9,2,5,10,2,6, 8,2,6, 9,
  15. 2,6,10,2,7, 9,2,7,10,2,8,10,3,5, 7,3,5, 8,3,5, 9,3,5,10,
  16. 3,6, 8,3,6, 9,3,6,10,3,7, 9,3,7,10,3,8,10,4,6, 8,4,6, 9,
  17. 4,6,10,4,7, 9,4,7,10,4,8,10,5,7, 9,5,7,10,5,8,10,6,8,10
  18. };
  19.  
  20.  
  21. std::vector<int> generate_random(const int n, const int m, const int k)
  22. {
  23. if ((n < m) || ((n / m) < k))
  24. {
  25. return std::vector<int>();
  26. }
  27.  
  28. std::random_device source;
  29. std::mt19937 generator(source());
  30. std::uniform_int_distribution<> distribution(1, n - (m - 1) * k);
  31.  
  32. std::vector<int> result_list;
  33. result_list.reserve(m);
  34.  
  35. for (int i = 0; i < m; ++i)
  36. {
  37. result_list.push_back(distribution(generator));
  38. }
  39.  
  40. std::sort(std::begin(result_list),std::end(result_list));
  41.  
  42. for (int i = 0; i < m; ++i)
  43. {
  44. result_list[i] += (i * k);
  45. }
  46.  
  47. return result_list;
  48. }
  49.  
  50. int main()
  51. {
  52. typedef std::vector<int> v_t;
  53.  
  54. std::vector<std::pair<v_t,std::size_t>> comb_list;
  55.  
  56. int i = 0;
  57.  
  58. while (i < sizeof(combs3) / sizeof(int))
  59. {
  60. comb_list.push_back(std::make_pair(v_t(combs3 + i,combs3 + i + 3),0));
  61. i += 3;
  62. }
  63.  
  64. for (std::size_t i = 0; i < comb_list.size(); ++i)
  65. {
  66. printf("%02d\t",i);
  67. std::copy(std::begin(comb_list[i].first),
  68. std::end(comb_list[i].first),
  69. std::ostream_iterator<int>(std::cout," "));
  70. std::cout << std::endl;
  71. }
  72.  
  73. const int rounds = 1000;
  74.  
  75. for (int r = 0; r < rounds; ++r)
  76. {
  77. const int n = 10;
  78. const int m = 3;
  79. const int k = 2;
  80. std::vector<int> comb_index_list = generate_random(n,m,k);
  81.  
  82. bool found = false;
  83. for (std::size_t i = 0; i < comb_list.size(); ++i)
  84. {
  85. if (std::equal(std::begin(comb_index_list),std::end(comb_index_list),std::begin(comb_list[i].first)))
  86. {
  87. comb_list[i].second++;
  88. found = true;
  89. break;
  90. }
  91. }
  92.  
  93. if (!found)
  94. {
  95. std::cout << "Error: combination not found: ";
  96. std::copy(std::begin(comb_index_list),
  97. std::end(comb_index_list),
  98. std::ostream_iterator<int>(std::cout," "));
  99. break;
  100. }
  101. }
  102.  
  103. for (std::size_t i = 0; i < comb_list.size(); ++i)
  104. {
  105. printf("comb[%02d] %10.6f\n",i,((1.0 * comb_list[i].second) / rounds));
  106. }
  107.  
  108. return 0;
  109. }
  110.  
Success #stdin #stdout 1.2s 3480KB
stdin
Standard input is empty
stdout
00	1 3 5 
01	1 3 6 
02	1 3 7 
03	1 3 8 
04	1 3 9 
05	1 3 10 
06	1 4 6 
07	1 4 7 
08	1 4 8 
09	1 4 9 
10	1 4 10 
11	1 5 7 
12	1 5 8 
13	1 5 9 
14	1 5 10 
15	1 6 8 
16	1 6 9 
17	1 6 10 
18	1 7 9 
19	1 7 10 
20	1 8 10 
21	2 4 6 
22	2 4 7 
23	2 4 8 
24	2 4 9 
25	2 4 10 
26	2 5 7 
27	2 5 8 
28	2 5 9 
29	2 5 10 
30	2 6 8 
31	2 6 9 
32	2 6 10 
33	2 7 9 
34	2 7 10 
35	2 8 10 
36	3 5 7 
37	3 5 8 
38	3 5 9 
39	3 5 10 
40	3 6 8 
41	3 6 9 
42	3 6 10 
43	3 7 9 
44	3 7 10 
45	3 8 10 
46	4 6 8 
47	4 6 9 
48	4 6 10 
49	4 7 9 
50	4 7 10 
51	4 8 10 
52	5 7 9 
53	5 7 10 
54	5 8 10 
55	6 8 10 
comb[00]    0.002000
comb[01]    0.011000
comb[02]    0.009000
comb[03]    0.007000
comb[04]    0.016000
comb[05]    0.010000
comb[06]    0.018000
comb[07]    0.038000
comb[08]    0.027000
comb[09]    0.030000
comb[10]    0.023000
comb[11]    0.014000
comb[12]    0.025000
comb[13]    0.018000
comb[14]    0.027000
comb[15]    0.014000
comb[16]    0.025000
comb[17]    0.024000
comb[18]    0.012000
comb[19]    0.030000
comb[20]    0.012000
comb[21]    0.007000
comb[22]    0.017000
comb[23]    0.019000
comb[24]    0.014000
comb[25]    0.009000
comb[26]    0.005000
comb[27]    0.030000
comb[28]    0.033000
comb[29]    0.026000
comb[30]    0.019000
comb[31]    0.048000
comb[32]    0.027000
comb[33]    0.015000
comb[34]    0.037000
comb[35]    0.013000
comb[36]    0.005000
comb[37]    0.014000
comb[38]    0.014000
comb[39]    0.011000
comb[40]    0.009000
comb[41]    0.023000
comb[42]    0.026000
comb[43]    0.020000
comb[44]    0.024000
comb[45]    0.013000
comb[46]    0.008000
comb[47]    0.013000
comb[48]    0.016000
comb[49]    0.021000
comb[50]    0.021000
comb[51]    0.011000
comb[52]    0.002000
comb[53]    0.017000
comb[54]    0.016000
comb[55]    0.005000