#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, {1, 4}) << endl; cout << sieve(s, {3, 8}) << endl; cout << sieve(s, {2, 7}) << endl; cout << sieve(s, {4, 5}) << endl; cout << sieve(s, {6, 7}) << endl; cout << sieve(s, {1, 2}) << endl; show(s); return 0; }
Standard input is empty
40320 20160 13440 6720 3360 1680 1120 784 784 total: 12345678 12345687 12345786 12346578 12346587 12346785 12347568 12347586 12347685 12348567 12348576 12348675 12356478 12356487 12356784 12357468 12357486 12357684 12358467 12358476 12358674 12367458 12367485 12367584 12368457 12368475 12368574 12378456 12378465 12378564 12435678 12435687 12435786 12436578 12436587 12436785 12437568 12437586 12437685 12438567 12438576 12438675 12456378 12456387 12457368 12457386 12458367 12458376 12467358 12467385 12468357 12468375 12478356 12478365 12534678 12534687 12534786 12536478 12536487 12537468 12537486 12538467 12538476 12546378 12546387 12547368 12547386 12548367 12548376 12567348 12568347 12578346 12634578 12634587 12635478 12635487 12637458 12638457 12645378 12645387 12647358 12648357 12657348 12658347 12734568 12735468 12736458 12745368 12746358 12756348 13245678 13245687 13245786 13246578 13246587 13246785 13247568 13247586 13247685 13248567 13248576 13248675 13256478 13256487 13256784 13257468 13257486 13257684 13258467 13258476 13258674 13267458 13267485 13267584 13268457 13268475 13268574 13278456 13278465 13278564 13425678 13425687 13425786 13426578 13426587 13426785 13427568 13427586 13427685 13428567 13428576 13428675 13456278 13456287 13457268 13457286 13458267 13458276 13467258 13467285 13468257 13468275 13478256 13478265 13524678 13524687 13524786 13526478 13526487 13527468 13527486 13528467 13528476 13546278 13546287 13547268 13547286 13548267 13548276 13567248 13568247 13578246 13624578 13624587 13625478 13625487 13627458 13628457 13645278 13645287 13647258 13648257 13657248 13658247 13724568 13725468 13726458 13745268 13746258 13756248 14235678 14235687 14235786 14236578 14236587 14236785 14237568 14237586 14237685 14238567 14238576 14238675 14256378 14256387 14256783 14257368 14257386 14257683 14258367 14258376 14258673 14267358 14267385 14267583 14268357 14268375 14268573 14278356 14278365 14278563 14325678 14325687 14325786 14326578 14326587 14326785 14327568 14327586 14327685 14328567 14328576 14328675 14356278 14356287 14357268 14357286 14358267 14358276 14367258 14367285 14368257 14368275 14378256 14378265 14523678 14523687 14523786 14526378 14526387 14527368 14527386 14528367 14528376 14536278 14536287 14537268 14537286 14538267 14538276 14567238 14568237 14578236 14623578 14623587 14625378 14625387 14627358 14628357 14635278 14635287 14637258 14638257 14657238 14658237 14723568 14725368 14726358 14735268 14736258 14756238 15234678 15234687 15234786 15236478 15236487 15236784 15237468 15237486 15237684 15238467 15238476 15238674 15246378 15246387 15246783 15247368 15247386 15247683 15248367 15248376 15248673 15267348 15267384 15267483 15268347 15268374 15268473 15278346 15278364 15278463 15324678 15324687 15324786 15326478 15326487 15326784 15327468 15327486 15327684 15328467 15328476 15328674 15346278 15346287 15347268 15347286 15348267 15348276 15367248 15367284 15368247 15368274 15378246 15378264 15423678 15423687 15423786 15426378 15426387 15427368 15427386 15428367 15428376 15436278 15436287 15437268 15437286 15438267 15438276 15467238 15468237 15478236 15623478 15623487 15624378 15624387 15634278 15634287 15723468 15724368 15734268 16234578 16234587 16234785 16235478 16235487 16235784 16237485 16237584 16238475 16238574 16245378 16245387 16245783 16247385 16247583 16248375 16248573 16257384 16257483 16258374 16258473 16278345 16278354 16278453 16324578 16324587 16324785 16325478 16325487 16325784 16327485 16327584 16328475 16328574 16345278 16345287 16347285 16348275 16357284 16358274 16378245 16378254 16423578 16423587 16423785 16425378 16425387 16427385 16428375 16435278 16435287 16437285 16438275 16478235 16523478 16523487 16524378 16524387 16534278 16534287 17234586 17234685 17235486 17235684 17236485 17236584 17245386 17245683 17246385 17246583 17256384 17256483 17324586 17324685 17325486 17325684 17326485 17326584 17345286 17346285 17356284 17423586 17423685 17425386 17426385 17435286 17436285 17523486 17524386 17534286 23145678 23145687 23145786 23146578 23146587 23146785 23147568 23147586 23147685 23148567 23148576 23148675 23156478 23156487 23156784 23157468 23157486 23157684 23158467 23158476 23158674 23167458 23167485 23167584 23168457 23168475 23168574 23178456 23178465 23178564 23456178 23456187 23457168 23457186 23458167 23458176 23467158 23467185 23468157 23468175 23478156 23478165 23546178 23546187 23547168 23547186 23548167 23548176 23567148 23568147 23578146 23645178 23645187 23647158 23648157 23657148 23658147 23745168 23746158 23756148 24135678 24135687 24135786 24136578 24136587 24136785 24137568 24137586 24137685 24138567 24138576 24138675 24156378 24156387 24156783 24157368 24157386 24157683 24158367 24158376 24158673 24167358 24167385 24167583 24168357 24168375 24168573 24178356 24178365 24178563 24356178 24356187 24357168 24357186 24358167 24358176 24367158 24367185 24368157 24368175 24378156 24378165 24536178 24536187 24537168 24537186 24538167 24538176 24567138 24568137 24578136 24635178 24635187 24637158 24638157 24657138 24658137 24735168 24736158 24756138 25134678 25134687 25134786 25136478 25136487 25136784 25137468 25137486 25137684 25138467 25138476 25138674 25146378 25146387 25146783 25147368 25147386 25147683 25148367 25148376 25148673 25167348 25167384 25167483 25168347 25168374 25168473 25178346 25178364 25178463 25346178 25346187 25347168 25347186 25348167 25348176 25367148 25367184 25368147 25368174 25378146 25378164 25436178 25436187 25437168 25437186 25438167 25438176 25467138 25468137 25478136 25634178 25634187 25734168 26134578 26134587 26134785 26135478 26135487 26135784 26137485 26137584 26138475 26138574 26145378 26145387 26145783 26147385 26147583 26148375 26148573 26157384 26157483 26158374 26158473 26178345 26178354 26178453 26345178 26345187 26347185 26348175 26357184 26358174 26378145 26378154 26435178 26435187 26437185 26438175 26478135 26534178 26534187 27134586 27134685 27135486 27135684 27136485 27136584 27145386 27145683 27146385 27146583 27156384 27156483 27345186 27346185 27356184 27435186 27436185 27534186 34156278 34156287 34156782 34157268 34157286 34157682 34158267 34158276 34158672 34167258 34167285 34167582 34168257 34168275 34168572 34178256 34178265 34178562 34256178 34256187 34257168 34257186 34258167 34258176 34267158 34267185 34268157 34268175 34278156 34278165 35146278 35146287 35146782 35147268 35147286 35147682 35148267 35148276 35148672 35167248 35167284 35167482 35168247 35168274 35168472 35178246 35178264 35178462 35246178 35246187 35247168 35247186 35248167 35248176 35267148 35267184 35268147 35268174 35278146 35278164 36145278 36145287 36145782 36147285 36147582 36148275 36148572 36157284 36157482 36158274 36158472 36178245 36178254 36178452 36245178 36245187 36247185 36248175 36257184 36258174 36278145 36278154 37145286 37145682 37146285 37146582 37156284 37156482 37245186 37246185 37256184 45167283 45167382 45168273 45168372 45178263 45178362 45267183 45268173 45278163 46157283 46157382 46158273 46158372 46178253 46178352 46257183 46258173 46278153 47156283 47156382 47256183