
#include "comb/perm-lex2.h"  // with factorial numbers
#include "perm/permq.h"  // major_index()

#include "comb/comb-print.h"

#include "fxtio.h"
#include "fxttypes.h"
#include "jjassert.h"

#include "nextarg.h"


//% OEIS sequence A207018:
//% Number of permutations with major index equal to inversion number.

int
main(int argc, char **argv)
{
    ulong n = 5;
    NXARG(n, "Permutations of n elements (n>=2)");
    jjassert( n>=2 );

    perm_lex2 P(n);

    ulong ct = 0;
    do
    {
        const ulong ni = P.num_inversions();
        const ulong mi = major_index(P.x_, n);
        if ( ni != mi )  continue;

        ++ct;
#if 1
        cout << setw(4) << ct << ":";
        P.print("    ");
        cout << setw(4) << mi;
//        cout << setw(4) << ni;
        print_vec("    ", P.d_, n-1);
        cout << endl;
#endif
    }
    while ( P.next() );

    cout << " ct=" << ct;
    cout << endl;

    return 0;
}
// -------------------------

/*

 1, 2, 4, 10, 28, 116, 576, 3634, 26620, 223440, 2100964, 21888712, 250062982


*/


/// Local Variables:
/// MyRelDir: "demo/seq"
/// makefile-dir: "../../"
/// make-target: "1demo DSRC=demo/seq/A207018-demo.cc"
/// make-target2: "1demo DSRC=demo/seq/A207018-demo.cc DEMOFLAGS=-DTIMING"
/// End:
