Index: lib/numeric/modArith.cpp ================================================================== --- lib/numeric/modArith.cpp +++ lib/numeric/modArith.cpp @@ -51,5 +51,21 @@ { if( b > e ) return 0; if( k.val <= 1 ) return k*(e-b+1); return (POW(k, e+1) - POW(k,b)) / (k-1); } + +// https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind +// Number of ways to split |n| labelled objects to exactly |k| unlabbled sets. +// * If we drop "exactly", the answer was k^n +// * If we split to "labeled" sets, the answer will be S(n,k)*k! +// * If unlabeled/unlabeld bar-and-ball-arranging argument. +vector< vector<mint> > SP_; +mint S(int n, int k) { + while (SP_.size() <= n) { + int nn = SP_.size(); + SP_.push_back(vector<mint>(nn + 1, 1)); + for (int kk = 2; kk<nn; ++kk) + SP_[nn][kk] = SP_[nn - 1][kk - 1] + kk*SP_[nn - 1][kk]; + } + return k<=0 || n<k ? 0 : SP_[n][k]; +}