Test instructions
For the arrangement of N number, the first m has the number of methods of k in its own position.
Ideas:
Set D[n] is the number of rows of n elements.
So we have d[1] = 0 d[2] = 1;
D[n] = (d[n-1] + d[n-2]) * (i-1)
Considering the problem itself, we first select the number of K in the previous m number is not moving. That is, C (m,k). For the number of M-k not selected in the first M is definitely involved in the wrong row, and the number of n-m the number of rows in the wrong row, so we enumerate a number of n-m in the back to select I (0 <= i <= n-m) number has been involved in the wrong row. In total, there are n-k-i involved in the wrong row.
In summary, the ANS formula is c[m][k] * Sigama (c[n-m][i] * d[n-k-i])% MoD;
Preprocess the number of combinations and the number of rows and then mess up.
Note that there is a pit point. D[0] To assign a value of 1
Reference code:
/ * #pragma warning (disable:4786) #pragma comment (linker, "/stack:0x800000") */#include <cassert>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <sstream>#include <iomanip>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <algorithm>#include <iterator>#include <utility>using namespace STD;Template<classT > t _abs (t N) {return(N <0? -N:N);}Template<classT > t _max (t A, T b) {return(! (A < b)? A:B);}Template<classT > t _min (t A, T b) {return(A < b? a:b);}Template<classT > t sq (t x) {returnx * x;}Template<classT > t gcd (t A, T b) {return(b! =0? Gcd<t> (b, a%b): a);}Template<classT > t LCM (t A, T b) {return(A/gcd<t> (A, b) * b);}Template<classT >BOOLInside (t A, T B, t C) {returnA<=b && B<=c;}#define MIN (A, B) ((a) < (b)? (a): (b))#define MAX (A, B) ((a) > (b)? (a): (b))#define F (i, n) for (int (i) =0; (i) < (n); + + (i))#define REP (i, S, T) for (int (i) = (s);(i) <= (t); + + (i))#define UREP (i, S, T) for (int (i) = (s);(i) >= (t);--(i))#define REPOK (i, S, T, O) for (int (i) = (s);(i) <= (t) && (o); + + (i))#define MEM0 (addr) memset ((addr), 0, sizeof ((addr)))#define MP (x, y) make_pair (x, y)#define REV (S, e) reverse (S, e )#define SET (P) memset (pair,-1, sizeof (p))#define CLR (P) memset (p, 0, sizeof (p))#define MEM (P, v) memset (P, V, sizeof (p))#define CPY (d, s) memcpy (d, S, sizeof (s))#define READ (f) freopen (F, "R", stdin)#define WRITE (f) freopen (F, "w", stdout)#define SZ (c) (int) c.size (#define PB (x) push_back (x)#define FF First#define SS Second#define LL Long Long#define LD long double#define PII pair< int, int >#define PSI pair< string, int >#define LS l,mid,rt<<1#define RS Mid+1,r,rt<<1|1#define DEBUG (x) cout << #x << "=" << x << EndlConst DoublePI =ACOs(-1.0);Const intINF =1<< -;Const intMAXN = +;Const intMoD =1000000007;intM,n,k;ll d[maxn+5];ll c[maxn+5][maxn+5];voidInit () {c[0][0] =1; Rep (I,1, MAXN) {c[i][0] = C[i][i] =1; Rep (J,1, I-1) C[i][j] = (c[i-1][j-1] + c[i-1][J])% MoD; } d[1] =0, d[0] = d[2] =1; Rep (I,3, MAXN) D[i] = (I-1) * (d[i-1] + d[i-2])% MoD;} ll solve (intNintMintK) {ll ans =0; Rep (I,0, n-m) ans = (ans + c[n-m][i] * d[n-k-i])% MoD;returnAns * c[m][k]% mod; }intMain () {//read ("In.txt"); intT,kase =1;scanf("%d", &t); Init (); while(t--) {scanf("%d%d%d", &n,&m,&k);printf("Case%d:%llu\n", Kase++,solve (n,m,k)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Lightoj 1095-arrange The Numbers (number of rows)