fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <array>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. const size_t N = 8;
  9. typedef array<size_t, N> perm;
  10. typedef set<perm> perms;
  11.  
  12. perms s;
  13.  
  14. void init (perms& s) {
  15. s.clear();
  16. perm ta;
  17. for (size_t i = 0; i < N; ++i) ta[i] = i;
  18. s.insert(ta);
  19. while(next_permutation(ta.begin(), ta.end()))
  20. s.insert(ta);
  21. }
  22.  
  23. size_t sieve (perms& s, pair<size_t, size_t> p) {
  24. vector<perm> for_insert;
  25. vector<perms::iterator> for_erase;
  26.  
  27. for (auto sit = s.begin(); sit != s.end(); ++sit) {
  28. if ((*sit)[p.first] > (*sit)[p.second]) {
  29. for_erase.push_back(sit);
  30. for_insert.push_back(*sit);
  31. swap(for_insert.back()[p.first], for_insert.back()[p.second]);
  32. }
  33. }
  34.  
  35. for (auto& eit: for_erase)
  36. s.erase(eit);
  37.  
  38. for (auto& a: for_insert) {
  39. bool t = s.insert(a).second;
  40. //if (t) cout << "INSERT!" << endl;
  41. }
  42.  
  43. return s.size();
  44. }
  45.  
  46. void show (const perms& s) {
  47. cout << s.size() << " total:" << endl;
  48. for (auto& a: s) {
  49. for (auto& e: a) cout << e;
  50. cout << endl;
  51. }
  52. }
  53.  
  54. int main() {
  55. init(s);
  56. cout << s.size() << endl;
  57.  
  58. cout << sieve(s, {0, 4}) << endl;
  59. cout << sieve(s, {0, 3}) << endl;
  60. cout << sieve(s, {2, 7}) << endl;
  61. cout << sieve(s, {6, 1}) << endl;
  62. cout << sieve(s, {4, 3}) << endl;
  63. cout << sieve(s, {6, 5}) << endl;
  64. cout << sieve(s, {0, 1}) << endl;
  65.  
  66.  
  67. show(s);
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.05s 10128KB
stdin
01
23
45
67
stdout
40320
20160
13440
6720
3360
1680
1120
980
980 total:
02354617
02354716
02364517
02364715
02365417
02365714
02374516
02374615
02375416
02375614
02376415
02376514
02453617
02453716
02463517
02463715
02465317
02473516
02473615
02475316
02476315
02543617
02543716
02563417
02564317
02573416
02574316
02643517
02653417
02654317
03154627
03154726
03164527
03164725
03165427
03165724
03174526
03174625
03175426
03175624
03176425
03176524
03254617
03254716
03264517
03264715
03265417
03265714
03274516
03274615
03275416
03275614
03276415
03276514
03451627
03451726
03452617
03452716
03461527
03461725
03462517
03462715
03465217
03471526
03471625
03472516
03472615
03475216
03476215
03541627
03541726
03542617
03542716
03561427
03562417
03564217
03571426
03572416
03574216
03641527
03642517
03651427
03652417
03654217
04152637
04152736
04153627
04153726
04162537
04162735
04163527
04163725
04165327
04165723
04165732
04172536
04172635
04173526
04173625
04175326
04175623
04175632
04176325
04176523
04176532
04251637
04251736
04253617
04253716
04261537
04261735
04263517
04263715
04265317
04265713
04271536
04271635
04273516
04273615
04275316
04275613
04276315
04276513
04351627
04351726
04352617
04352716
04361527
04361725
04362517
04362715
04365217
04371526
04371625
04372516
04372615
04375216
04376215
04521637
04521736
04531627
04531726
04532617
04532716
04561327
04562317
04563217
04571326
04572316
04573216
04621537
04631527
04632517
04651327
04652317
04653217
05132647
05132746
05142637
05142736
05143627
05143726
05162437
05162734
05162743
05163427
05163724
05163742
05164327
05164723
05164732
05172436
05172634
05172643
05173426
05173624
05173642
05174326
05174623
05174632
05176324
05176423
05176432
05231647
05231746
05241637
05241736
05243617
05243716
05261437
05261734
05261743
05263417
05263714
05264317
05264713
05271436
05271634
05271643
05273416
05273614
05274316
05274613
05276314
05276413
05321647
05321746
05341627
05341726
05342617
05342716
05361427
05361724
05362417
05362714
05364217
05371426
05371624
05372416
05372614
05374216
05376214
05421637
05421736
05431627
05431726
05432617
05432716
05461327
05462317
05463217
05471326
05472316
05473216
05621437
05631427
05632417
05641327
05642317
05643217
06132547
06132745
06132754
06142537
06142735
06142753
06143527
06143725
06143752
06152437
06152734
06152743
06153427
06153724
06153742
06154327
06154723
06154732
06172435
06172534
06172543
06173425
06173524
06173542
06174325
06174523
06174532
06175324
06175423
06175432
06231547
06231745
06231754
06241537
06241735
06241753
06243517
06243715
06251437
06251734
06251743
06253417
06253714
06254317
06254713
06271435
06271534
06271543
06273415
06273514
06274315
06274513
06275314
06275413
06321547
06321745
06321754
06341527
06341725
06342517
06342715
06351427
06351724
06352417
06352714
06354217
06371425
06371524
06372415
06372514
06374215
06375214
06421537
06421735
06431527
06431725
06432517
06432715
06451327
06452317
06453217
06471325
06472315
06473215
06521437
06531427
06532417
06541327
06542317
06543217
07132546
07132645
07132654
07142536
07142635
07142653
07143526
07143625
07143652
07152436
07152634
07152643
07153426
07153624
07153642
07154326
07154623
07154632
07162435
07162534
07162543
07163425
07163524
07163542
07164325
07164523
07164532
07165324
07165423
07165432
07231546
07231645
07231654
07241536
07241635
07241653
07243516
07243615
07251436
07251634
07251643
07253416
07253614
07254316
07254613
07261435
07261534
07261543
07263415
07263514
07264315
07264513
07265314
07265413
07321546
07321645
07321654
07341526
07341625
07342516
07342615
07351426
07351624
07352416
07352614
07354216
07361425
07361524
07362415
07362514
07364215
07365214
07421536
07421635
07431526
07431625
07432516
07432615
07451326
07452316
07453216
07461325
07462315
07463215
07521436
07531426
07532416
07541326
07542316
07543216
12354607
12354706
12364507
12364705
12365407
12365704
12374506
12374605
12375406
12375604
12376405
12376504
12453607
12453706
12463507
12463705
12465307
12473506
12473605
12475306
12476305
12543607
12543706
12563407
12564307
12573406
12574306
12643507
12653407
12654307
13054627
13054726
13064527
13064725
13065427
13065724
13074526
13074625
13075426
13075624
13076425
13076524
13254607
13254706
13264507
13264705
13265407
13265704
13274506
13274605
13275406
13275604
13276405
13276504
13452607
13452706
13462507
13462705
13465207
13472506
13472605
13475206
13476205
13542607
13542706
13562407
13564207
13572406
13574206
13642507
13652407
13654207
14052637
14052736
14053627
14053726
14062537
14062735
14063527
14063725
14065327
14065723
14065732
14072536
14072635
14073526
14073625
14075326
14075623
14075632
14076325
14076523
14076532
14253607
14253706
14263507
14263705
14265307
14265703
14273506
14273605
14275306
14275603
14276305
14276503
14352607
14352706
14362507
14362705
14365207
14372506
14372605
14375206
14376205
14532607
14532706
14562307
14563207
14572306
14573206
14632507
14652307
14653207
15032647
15032746
15042637
15042736
15043627
15043726
15062437
15062734
15062743
15063427
15063724
15063742
15064327
15064723
15064732
15072436
15072634
15072643
15073426
15073624
15073642
15074326
15074623
15074632
15076324
15076423
15076432
15243607
15243706
15263407
15263704
15264307
15264703
15273406
15273604
15274306
15274603
15276304
15276403
15342607
15342706
15362407
15362704
15364207
15372406
15372604
15374206
15376204
15432607
15432706
15462307
15463207
15472306
15473206
15632407
15642307
15643207
16032547
16032745
16032754
16042537
16042735
16042753
16043527
16043725
16043752
16052437
16052734
16052743
16053427
16053724
16053742
16054327
16054723
16054732
16072435
16072534
16072543
16073425
16073524
16073542
16074325
16074523
16074532
16075324
16075423
16075432
16243507
16243705
16253407
16253704
16254307
16254703
16273405
16273504
16274305
16274503
16275304
16275403
16342507
16342705
16352407
16352704
16354207
16372405
16372504
16374205
16375204
16432507
16432705
16452307
16453207
16472305
16473205
16532407
16542307
16543207
17032546
17032645
17032654
17042536
17042635
17042653
17043526
17043625
17043652
17052436
17052634
17052643
17053426
17053624
17053642
17054326
17054623
17054632
17062435
17062534
17062543
17063425
17063524
17063542
17064325
17064523
17064532
17065324
17065423
17065432
17243506
17243605
17253406
17253604
17254306
17254603
17263405
17263504
17264305
17264503
17265304
17265403
17342506
17342605
17352406
17352604
17354206
17362405
17362504
17364205
17365204
17432506
17432605
17452306
17453206
17462305
17463205
17532406
17542306
17543206
23054617
23054716
23064517
23064715
23065417
23065714
23074516
23074615
23075416
23075614
23076415
23076514
23154607
23154706
23164507
23164705
23165407
23165704
23174506
23174605
23175406
23175604
23176405
23176504
23465107
23475106
23476105
23564107
23574106
23654107
24053617
24053716
24063517
24063715
24065317
24065713
24065731
24073516
24073615
24075316
24075613
24075631
24076315
24076513
24076531
24153607
24153706
24163507
24163705
24165307
24165703
24173506
24173605
24175306
24175603
24176305
24176503
24365107
24375106
24376105
24563107
24573106
24653107
25043617
25043716
25063417
25063714
25063741
25064317
25064713
25064731
25073416
25073614
25073641
25074316
25074613
25074631
25076314
25076413
25076431
25143607
25143706
25163407
25163704
25164307
25164703
25173406
25173604
25174306
25174603
25176304
25176403
25364107
25374106
25376104
25463107
25473106
25643107
26043517
26043715
26043751
26053417
26053714
26053741
26054317
26054713
26054731
26073415
26073514
26073541
26074315
26074513
26074531
26075314
26075413
26075431
26143507
26143705
26153407
26153704
26154307
26154703
26173405
26173504
26174305
26174503
26175304
26175403
26354107
26374105
26375104
26453107
26473105
26543107
27043516
27043615
27043651
27053416
27053614
27053641
27054316
27054613
27054631
27063415
27063514
27063541
27064315
27064513
27064531
27065314
27065413
27065431
27143506
27143605
27153406
27153604
27154306
27154603
27163405
27163504
27164305
27164503
27165304
27165403
27354106
27364105
27365104
27453106
27463105
27543106
34065217
34065712
34065721
34075216
34075612
34075621
34076215
34076512
34076521
34165207
34165702
34175206
34175602
34176205
34176502
34265107
34275106
34276105
35064217
35064712
35064721
35074216
35074612
35074621
35076214
35076412
35076421
35164207
35164702
35174206
35174602
35176204
35176402
35264107
35274106
35276104
36054217
36054712
36054721
36074215
36074512
36074521
36075214
36075412
36075421
36154207
36154702
36174205
36174502
36175204
36175402
36254107
36274105
36275104
37054216
37054612
37054621
37064215
37064512
37064521
37065214
37065412
37065421
37154206
37154602
37164205
37164502
37165204
37165402
37254106
37264105
37265104
45076213
45076312
45076321
45176203
45176302
45276103
46075213
46075312
46075321
46175203
46175302
46275103
47065213
47065312
47065321
47165203
47165302
47265103