#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, 5}) << endl; cout << sieve(s, {2, 6}) << endl; cout << sieve(s, {3, 7}) << endl; cout << sieve(s, {4, 8}) << endl; cout << sieve(s, {3, 4}) << endl; cout << sieve(s, {2, 3}) << endl; cout << sieve(s, {1, 2}) << endl; show(s); return 0; }
Standard input is empty
40320 20160 10080 5040 2520 1680 1232 968 968 total: 12345678 12345687 12345768 12345786 12345867 12345876 12346578 12346587 12346758 12346785 12346857 12346875 12347568 12347586 12347658 12347685 12347856 12347865 12348567 12348576 12348657 12348675 12348756 12348765 12354678 12354687 12354768 12354786 12354867 12354876 12356478 12356487 12356748 12356784 12356847 12356874 12357468 12357486 12357648 12357684 12357846 12357864 12358467 12358476 12358647 12358674 12358746 12358764 12364578 12364587 12364758 12364785 12364857 12364875 12365478 12365487 12365748 12365784 12365847 12365874 12367458 12367485 12367548 12367584 12368457 12368475 12368547 12368574 12374568 12374586 12374658 12374685 12375468 12375486 12375648 12375684 12376458 12376485 12376548 12376584 12435678 12435687 12435768 12435786 12435867 12435876 12436578 12436587 12436758 12436785 12436857 12436875 12437568 12437586 12437658 12437685 12437856 12437865 12438567 12438576 12438657 12438675 12438756 12438765 12453678 12453687 12453768 12453786 12453867 12453876 12456378 12456387 12456738 12456783 12456837 12456873 12457368 12457386 12457638 12457683 12457836 12457863 12458367 12458376 12458637 12458673 12458736 12458763 12463578 12463587 12463758 12463785 12463857 12463875 12465378 12465387 12465738 12465783 12465837 12465873 12467358 12467385 12467538 12467583 12468357 12468375 12468537 12468573 12473568 12473586 12473658 12473685 12475368 12475386 12475638 12475683 12476358 12476385 12476538 12476583 12534678 12534687 12534768 12534786 12534867 12534876 12536748 12536784 12536847 12536874 12537648 12537684 12537846 12537864 12538647 12538674 12538746 12538764 12543678 12543687 12543768 12543786 12543867 12543876 12546738 12546783 12546837 12546873 12547638 12547683 12547836 12547863 12548637 12548673 12548736 12548763 12563478 12563487 12563748 12563784 12563847 12563874 12564378 12564387 12564738 12564783 12564837 12564873 12573468 12573486 12573648 12573684 12574368 12574386 12574638 12574683 12634758 12634785 12634857 12634875 12635748 12635784 12635847 12635874 12637845 12637854 12638745 12638754 12643758 12643785 12643857 12643875 12645738 12645783 12645837 12645873 12647835 12647853 12648735 12648753 12653748 12653784 12653847 12653874 12654738 12654783 12654837 12654873 12734856 12734865 12735846 12735864 12736845 12736854 12743856 12743865 12745836 12745863 12746835 12746853 12753846 12753864 12754836 12754863 13245678 13245687 13245768 13245786 13245867 13245876 13246578 13246587 13246758 13246785 13246857 13246875 13247568 13247586 13247658 13247685 13247856 13247865 13248567 13248576 13248657 13248675 13248756 13248765 13254678 13254687 13254768 13254786 13254867 13254876 13256478 13256487 13256748 13256784 13256847 13256874 13257468 13257486 13257648 13257684 13257846 13257864 13258467 13258476 13258647 13258674 13258746 13258764 13264578 13264587 13264758 13264785 13264857 13264875 13265478 13265487 13265748 13265784 13265847 13265874 13267458 13267485 13267548 13267584 13268457 13268475 13268547 13268574 13274568 13274586 13274658 13274685 13275468 13275486 13275648 13275684 13276458 13276485 13276548 13276584 13425678 13425687 13425768 13425786 13425867 13425876 13426578 13426587 13426758 13426785 13426857 13426875 13427568 13427586 13427658 13427685 13427856 13427865 13428567 13428576 13428657 13428675 13428756 13428765 13452678 13452687 13452768 13452786 13452867 13452876 13456278 13456287 13456728 13456782 13456827 13456872 13457268 13457286 13457628 13457682 13457826 13457862 13458267 13458276 13458627 13458672 13458726 13458762 13462578 13462587 13462758 13462785 13462857 13462875 13465278 13465287 13465728 13465782 13465827 13465872 13467258 13467285 13467528 13467582 13468257 13468275 13468527 13468572 13472568 13472586 13472658 13472685 13475268 13475286 13475628 13475682 13476258 13476285 13476528 13476582 13524678 13524687 13524768 13524786 13524867 13524876 13526748 13526784 13526847 13526874 13527648 13527684 13527846 13527864 13528647 13528674 13528746 13528764 13542678 13542687 13542768 13542786 13542867 13542876 13546728 13546782 13546827 13546872 13547628 13547682 13547826 13547862 13548627 13548672 13548726 13548762 13562478 13562487 13562748 13562784 13562847 13562874 13564278 13564287 13564728 13564782 13564827 13564872 13572468 13572486 13572648 13572684 13574268 13574286 13574628 13574682 13624758 13624785 13624857 13624875 13625748 13625784 13625847 13625874 13627845 13627854 13628745 13628754 13642758 13642785 13642857 13642875 13645728 13645782 13645827 13645872 13647825 13647852 13648725 13648752 13652748 13652784 13652847 13652874 13654728 13654782 13654827 13654872 13724856 13724865 13725846 13725864 13726845 13726854 13742856 13742865 13745826 13745862 13746825 13746852 13752846 13752864 13754826 13754862 14235678 14235687 14235768 14235786 14235867 14235876 14236578 14236587 14236758 14236785 14236857 14236875 14237568 14237586 14237658 14237685 14237856 14237865 14238567 14238576 14238657 14238675 14238756 14238765 14256378 14256387 14256738 14256783 14256837 14256873 14257368 14257386 14257638 14257683 14257836 14257863 14258367 14258376 14258637 14258673 14258736 14258763 14265378 14265387 14265738 14265783 14265837 14265873 14267358 14267385 14267538 14267583 14268357 14268375 14268537 14268573 14275368 14275386 14275638 14275683 14276358 14276385 14276538 14276583 14325678 14325687 14325768 14325786 14325867 14325876 14326578 14326587 14326758 14326785 14326857 14326875 14327568 14327586 14327658 14327685 14327856 14327865 14328567 14328576 14328657 14328675 14328756 14328765 14356278 14356287 14356728 14356782 14356827 14356872 14357268 14357286 14357628 14357682 14357826 14357862 14358267 14358276 14358627 14358672 14358726 14358762 14365278 14365287 14365728 14365782 14365827 14365872 14367258 14367285 14367528 14367582 14368257 14368275 14368527 14368572 14375268 14375286 14375628 14375682 14376258 14376285 14376528 14376582 14526738 14526783 14526837 14526873 14527638 14527683 14527836 14527863 14528637 14528673 14528736 14528763 14536728 14536782 14536827 14536872 14537628 14537682 14537826 14537862 14538627 14538672 14538726 14538762 14625738 14625783 14625837 14625873 14627835 14627853 14628735 14628753 14635728 14635782 14635827 14635872 14637825 14637852 14638725 14638752 14725836 14725863 14726835 14726853 14735826 14735862 14736825 14736852 15236478 15236487 15236748 15236784 15236847 15236874 15237468 15237486 15237648 15237684 15237846 15237864 15238467 15238476 15238647 15238674 15238746 15238764 15246378 15246387 15246738 15246783 15246837 15246873 15247368 15247386 15247638 15247683 15247836 15247863 15248367 15248376 15248637 15248673 15248736 15248763 15267348 15267384 15267438 15267483 15268347 15268374 15268437 15268473 15276348 15276384 15276438 15276483 15326478 15326487 15326748 15326784 15326847 15326874 15327468 15327486 15327648 15327684 15327846 15327864 15328467 15328476 15328647 15328674 15328746 15328764 15346278 15346287 15346728 15346782 15346827 15346872 15347268 15347286 15347628 15347682 15347826 15347862 15348267 15348276 15348627 15348672 15348726 15348762 15367248 15367284 15367428 15367482 15368247 15368274 15368427 15368472 15376248 15376284 15376428 15376482 15426738 15426783 15426837 15426873 15427638 15427683 15427836 15427863 15428637 15428673 15428736 15428763 15436728 15436782 15436827 15436872 15437628 15437682 15437826 15437862 15438627 15438672 15438726 15438762 15627834 15627843 15628734 15628743 15637824 15637842 15638724 15638742 15726834 15726843 15736824 15736842 16237458 16237485 16237548 16237584 16237845 16237854 16238457 16238475 16238547 16238574 16238745 16238754 16247358 16247385 16247538 16247583 16247835 16247853 16248357 16248375 16248537 16248573 16248735 16248753 16257348 16257384 16257438 16257483 16258347 16258374 16258437 16258473 16327458 16327485 16327548 16327584 16327845 16327854 16328457 16328475 16328547 16328574 16328745 16328754 16347258 16347285 16347528 16347582 16347825 16347852 16348257 16348275 16348527 16348572 16348725 16348752 16357248 16357284 16357428 16357482 16358247 16358274 16358427 16358472 16427538 16427583 16427835 16427853 16428537 16428573 16428735 16428753 16437528 16437582 16437825 16437852 16438527 16438572 16438725 16438752 16527834 16527843 16528734 16528743 16537824 16537842 16538724 16538742 17238456 17238465 17238546 17238564 17238645 17238654 17248356 17248365 17248536 17248563 17248635 17248653 17258346 17258364 17258436 17258463 17328456 17328465 17328546 17328564 17328645 17328654 17348256 17348265 17348526 17348562 17348625 17348652 17358246 17358264 17358426 17358462 17428536 17428563 17428635 17428653 17438526 17438562 17438625 17438652 17528634 17528643 17538624 17538642