fork download
  1. #include <stdio.h>
  2. #include <vector>
  3.  
  4. const char digitset[] = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  5.  
  6. void allocate_to_groups_impl( std::vector<char>& usage, std::vector<int>& order, const std::vector<int>& cumsum, int group, int place, int min )
  7. {
  8. if (place == cumsum[group]) {
  9. group++; min = 0;
  10. if (group == cumsum.size()) {
  11. for( std::vector<int>::iterator it = order.begin(); it != order.end(); ++it )
  12. putchar(digitset[*it]);
  13. putchar('\n');
  14. return;
  15. }
  16. }
  17.  
  18. for( int v = min, max = usage.size() + place - cumsum[group]; v <= max; ++v ) {
  19. if (!usage[v]) {
  20. order[place] = v;
  21. usage[v] = 1;
  22. allocate_to_groups_impl(usage, order, cumsum, group, place+1, v+1);
  23. usage[v] = 0;
  24. }
  25. }
  26. }
  27.  
  28. template<size_t Ngroups>
  29. void allocate_to_groups( int (&c)[Ngroups] )
  30. {
  31. size_t sum_of_c = 0;
  32. std::vector<int> cumsum_of_c;
  33. for( int* it = c; it < c + Ngroups; ++it )
  34. cumsum_of_c.push_back(sum_of_c += *it);
  35. std::vector<int> order(sum_of_c);
  36. std::vector<char> usage(sum_of_c);
  37.  
  38. allocate_to_groups_impl(usage, order, cumsum_of_c, 0, 0, 0);
  39. }
  40.  
  41. int main(void)
  42. {
  43. int c[] = { 2, 3, 2 };
  44. allocate_to_groups(c);
  45. };
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
1234567
1234657
1234756
1235647
1235746
1236745
1245637
1245736
1246735
1256734
1324567
1324657
1324756
1325647
1325746
1326745
1345627
1345726
1346725
1356724
1423567
1423657
1423756
1425637
1425736
1426735
1435627
1435726
1436725
1456723
1523467
1523647
1523746
1524637
1524736
1526734
1534627
1534726
1536724
1546723
1623457
1623547
1623745
1624537
1624735
1625734
1634527
1634725
1635724
1645723
1723456
1723546
1723645
1724536
1724635
1725634
1734526
1734625
1735624
1745623
2314567
2314657
2314756
2315647
2315746
2316745
2345617
2345716
2346715
2356714
2413567
2413657
2413756
2415637
2415736
2416735
2435617
2435716
2436715
2456713
2513467
2513647
2513746
2514637
2514736
2516734
2534617
2534716
2536714
2546713
2613457
2613547
2613745
2614537
2614735
2615734
2634517
2634715
2635714
2645713
2713456
2713546
2713645
2714536
2714635
2715634
2734516
2734615
2735614
2745613
3412567
3412657
3412756
3415627
3415726
3416725
3425617
3425716
3426715
3456712
3512467
3512647
3512746
3514627
3514726
3516724
3524617
3524716
3526714
3546712
3612457
3612547
3612745
3614527
3614725
3615724
3624517
3624715
3625714
3645712
3712456
3712546
3712645
3714526
3714625
3715624
3724516
3724615
3725614
3745612
4512367
4512637
4512736
4513627
4513726
4516723
4523617
4523716
4526713
4536712
4612357
4612537
4612735
4613527
4613725
4615723
4623517
4623715
4625713
4635712
4712356
4712536
4712635
4713526
4713625
4715623
4723516
4723615
4725613
4735612
5612347
5612437
5612734
5613427
5613724
5614723
5623417
5623714
5624713
5634712
5712346
5712436
5712634
5713426
5713624
5714623
5723416
5723614
5724613
5734612
6712345
6712435
6712534
6713425
6713524
6714523
6723415
6723514
6724513
6734512