#include <iostream> #include <set> #include <array> #include <algorithm> #include <vector> using namespace std; const size_t N = 8; typedef array<size_t, N> perm; typedef set<perm> perms; perms s; void init (perms& s) { s.clear(); perm ta; for (size_t i = 0; i < N; ++i) ta[i] = i+1; s.insert(ta); while(next_permutation(ta.begin(), ta.end())) s.insert(ta); } size_t sieve (perms& s, pair<size_t, size_t> p) { vector<perm> for_insert; vector<perms::iterator> for_erase; for (auto sit = s.begin(); sit != s.end(); ++sit) { auto first = p.first - 1; auto second = p.second - 1; if ((*sit)[first] > (*sit)[second]) { for_erase.push_back(sit); for_insert.push_back(*sit); swap(for_insert.back()[first], for_insert.back()[second]); } } for (auto& eit: for_erase) s.erase(eit); for (auto& a: for_insert) { bool t = s.insert(a).second; //if (t) cout << "INSERT!" << endl; } return s.size(); } void show (const perms& s) { cout << s.size() << " total:" << endl; for (auto& a: s) { for (auto& e: a) cout << e; cout << endl; } } int main() { init(s); cout << s.size() << endl; cout << sieve(s, {1, 2}) << endl; cout << sieve(s, {3, 4}) << endl; cout << sieve(s, {5, 6}) << endl; cout << sieve(s, {7, 8}) << endl; cout << sieve(s, {5, 7}) << endl; cout << sieve(s, {3, 5}) << endl; cout << sieve(s, {1, 3}) << endl; show(s); return 0; }
Standard input is empty
40320 20160 10080 5040 2520 1680 1232 968 968 total: 12345678 12345768 12345867 12345876 12354678 12354768 12354867 12354876 12364578 12364758 12364857 12364875 12365478 12365748 12365847 12365874 12374568 12374658 12374856 12374865 12375468 12375648 12375846 12375864 12376458 12376548 12376845 12376854 12384567 12384657 12384756 12384765 12385467 12385647 12385746 12385764 12386457 12386547 12386745 12386754 12387456 12387546 12387645 12387654 13245678 13245768 13245867 13245876 13254678 13254768 13254867 13254876 13264578 13264758 13264857 13264875 13265478 13265748 13265847 13265874 13274568 13274658 13274856 13274865 13275468 13275648 13275846 13275864 13276458 13276548 13276845 13276854 13284567 13284657 13284756 13284765 13285467 13285647 13285746 13285764 13286457 13286547 13286745 13286754 13287456 13287546 13287645 13287654 14235678 14235768 14235867 14235876 14253678 14253768 14253867 14253876 14263578 14263758 14263857 14263875 14265378 14265738 14265837 14265873 14273568 14273658 14273856 14273865 14275368 14275638 14275836 14275863 14276358 14276538 14276835 14276853 14283567 14283657 14283756 14283765 14285367 14285637 14285736 14285763 14286357 14286537 14286735 14286753 14287356 14287536 14287635 14287653 14325678 14325768 14325867 14325876 14352678 14352768 14352867 14352876 14362578 14362758 14362857 14362875 14365278 14365728 14365827 14365872 14372568 14372658 14372856 14372865 14375268 14375628 14375826 14375862 14376258 14376528 14376825 14376852 14382567 14382657 14382756 14382765 14385267 14385627 14385726 14385762 14386257 14386527 14386725 14386752 14387256 14387526 14387625 14387652 15234678 15234768 15234867 15234876 15243678 15243768 15243867 15243876 15263478 15263748 15263847 15263874 15264378 15264738 15264837 15264873 15273468 15273648 15273846 15273864 15274368 15274638 15274836 15274863 15276348 15276438 15276834 15276843 15283467 15283647 15283746 15283764 15284367 15284637 15284736 15284763 15286347 15286437 15286734 15286743 15287346 15287436 15287634 15287643 15324678 15324768 15324867 15324876 15342678 15342768 15342867 15342876 15362478 15362748 15362847 15362874 15364278 15364728 15364827 15364872 15372468 15372648 15372846 15372864 15374268 15374628 15374826 15374862 15376248 15376428 15376824 15376842 15382467 15382647 15382746 15382764 15384267 15384627 15384726 15384762 15386247 15386427 15386724 15386742 15387246 15387426 15387624 15387642 15423678 15423768 15423867 15423876 15432678 15432768 15432867 15432876 15462378 15462738 15462837 15462873 15463278 15463728 15463827 15463872 15472368 15472638 15472836 15472863 15473268 15473628 15473826 15473862 15476238 15476328 15476823 15476832 15482367 15482637 15482736 15482763 15483267 15483627 15483726 15483762 15486237 15486327 15486723 15486732 15487236 15487326 15487623 15487632 16234578 16234758 16234857 16234875 16243578 16243758 16243857 16243875 16253478 16253748 16253847 16253874 16254378 16254738 16254837 16254873 16273458 16273548 16273845 16273854 16274358 16274538 16274835 16274853 16275348 16275438 16275834 16275843 16283457 16283547 16283745 16283754 16284357 16284537 16284735 16284753 16285347 16285437 16285734 16285743 16287345 16287435 16287534 16287543 16324578 16324758 16324857 16324875 16342578 16342758 16342857 16342875 16352478 16352748 16352847 16352874 16354278 16354728 16354827 16354872 16372458 16372548 16372845 16372854 16374258 16374528 16374825 16374852 16375248 16375428 16375824 16375842 16382457 16382547 16382745 16382754 16384257 16384527 16384725 16384752 16385247 16385427 16385724 16385742 16387245 16387425 16387524 16387542 16423578 16423758 16423857 16423875 16432578 16432758 16432857 16432875 16452378 16452738 16452837 16452873 16453278 16453728 16453827 16453872 16472358 16472538 16472835 16472853 16473258 16473528 16473825 16473852 16475238 16475328 16475823 16475832 16482357 16482537 16482735 16482753 16483257 16483527 16483725 16483752 16485237 16485327 16485723 16485732 16487235 16487325 16487523 16487532 16523478 16523748 16523847 16523874 16532478 16532748 16532847 16532874 16542378 16542738 16542837 16542873 16543278 16543728 16543827 16543872 16572348 16572438 16572834 16572843 16573248 16573428 16573824 16573842 16574238 16574328 16574823 16574832 16582347 16582437 16582734 16582743 16583247 16583427 16583724 16583742 16584237 16584327 16584723 16584732 16587234 16587324 16587423 16587432 17234568 17234658 17234856 17234865 17243568 17243658 17243856 17243865 17253468 17253648 17253846 17253864 17254368 17254638 17254836 17254863 17263458 17263548 17263845 17263854 17264358 17264538 17264835 17264853 17265348 17265438 17265834 17265843 17283456 17283546 17283645 17283654 17284356 17284536 17284635 17284653 17285346 17285436 17285634 17285643 17286345 17286435 17286534 17286543 17324568 17324658 17324856 17324865 17342568 17342658 17342856 17342865 17352468 17352648 17352846 17352864 17354268 17354628 17354826 17354862 17362458 17362548 17362845 17362854 17364258 17364528 17364825 17364852 17365248 17365428 17365824 17365842 17382456 17382546 17382645 17382654 17384256 17384526 17384625 17384652 17385246 17385426 17385624 17385642 17386245 17386425 17386524 17386542 17423568 17423658 17423856 17423865 17432568 17432658 17432856 17432865 17452368 17452638 17452836 17452863 17453268 17453628 17453826 17453862 17462358 17462538 17462835 17462853 17463258 17463528 17463825 17463852 17465238 17465328 17465823 17465832 17482356 17482536 17482635 17482653 17483256 17483526 17483625 17483652 17485236 17485326 17485623 17485632 17486235 17486325 17486523 17486532 17523468 17523648 17523846 17523864 17532468 17532648 17532846 17532864 17542368 17542638 17542836 17542863 17543268 17543628 17543826 17543862 17562348 17562438 17562834 17562843 17563248 17563428 17563824 17563842 17564238 17564328 17564823 17564832 17582346 17582436 17582634 17582643 17583246 17583426 17583624 17583642 17584236 17584326 17584623 17584632 17586234 17586324 17586423 17586432 17623458 17623548 17623845 17623854 17632458 17632548 17632845 17632854 17642358 17642538 17642835 17642853 17643258 17643528 17643825 17643852 17652348 17652438 17652834 17652843 17653248 17653428 17653824 17653842 17654238 17654328 17654823 17654832 17682345 17682435 17682534 17682543 17683245 17683425 17683524 17683542 17684235 17684325 17684523 17684532 17685234 17685324 17685423 17685432 18234567 18234657 18234756 18234765 18243567 18243657 18243756 18243765 18253467 18253647 18253746 18253764 18254367 18254637 18254736 18254763 18263457 18263547 18263745 18263754 18264357 18264537 18264735 18264753 18265347 18265437 18265734 18265743 18273456 18273546 18273645 18273654 18274356 18274536 18274635 18274653 18275346 18275436 18275634 18275643 18276345 18276435 18276534 18276543 18324567 18324657 18324756 18324765 18342567 18342657 18342756 18342765 18352467 18352647 18352746 18352764 18354267 18354627 18354726 18354762 18362457 18362547 18362745 18362754 18364257 18364527 18364725 18364752 18365247 18365427 18365724 18365742 18372456 18372546 18372645 18372654 18374256 18374526 18374625 18374652 18375246 18375426 18375624 18375642 18376245 18376425 18376524 18376542 18423567 18423657 18423756 18423765 18432567 18432657 18432756 18432765 18452367 18452637 18452736 18452763 18453267 18453627 18453726 18453762 18462357 18462537 18462735 18462753 18463257 18463527 18463725 18463752 18465237 18465327 18465723 18465732 18472356 18472536 18472635 18472653 18473256 18473526 18473625 18473652 18475236 18475326 18475623 18475632 18476235 18476325 18476523 18476532 18523467 18523647 18523746 18523764 18532467 18532647 18532746 18532764 18542367 18542637 18542736 18542763 18543267 18543627 18543726 18543762 18562347 18562437 18562734 18562743 18563247 18563427 18563724 18563742 18564237 18564327 18564723 18564732 18572346 18572436 18572634 18572643 18573246 18573426 18573624 18573642 18574236 18574326 18574623 18574632 18576234 18576324 18576423 18576432 18623457 18623547 18623745 18623754 18632457 18632547 18632745 18632754 18642357 18642537 18642735 18642753 18643257 18643527 18643725 18643752 18652347 18652437 18652734 18652743 18653247 18653427 18653724 18653742 18654237 18654327 18654723 18654732 18672345 18672435 18672534 18672543 18673245 18673425 18673524 18673542 18674235 18674325 18674523 18674532 18675234 18675324 18675423 18675432 18723456 18723546 18723645 18723654 18732456 18732546 18732645 18732654 18742356 18742536 18742635 18742653 18743256 18743526 18743625 18743652 18752346 18752436 18752634 18752643 18753246 18753426 18753624 18753642 18754236 18754326 18754623 18754632 18762345 18762435 18762534 18762543 18763245 18763425 18763524 18763542 18764235 18764325 18764523 18764532 18765234 18765324 18765423 18765432