LOJ #2034. 「SDOI2016」排列计数
题目链接:传送门
这种题就应该一眼秒掉才对
有个数是稳定的,也就是说
有个数要做错排
那就是从个数里挑个数做错排,为错排数组
可是数组初始化在全局里LOJ就说我超内存直接CE
为啥为啥
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <complex> #include <algorithm> #include <climits> #include <queue> #include <map> #include <set> #include <vector> #include <iomanip> #define A 1000010 #define B 2010 using namespace std; typedef long long ll; const int mod = 1e9 + 7; ll n, m, f[A], fac[A]; int T; ll fpow(ll a, ll b, ll ans = 1) { while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } ll C(ll n, ll m) { return fac[n] * fpow(fac[m] * fac[n - m] % mod, mod - 2) % mod; } ll lucas(ll n, ll m) { return m ? C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod : 1; } int main(int argc, char const *argv[]) { cin >> T; f[2] = fac[0] = fac[1] = 1; fac[2] = 2; for (int i = 3; i <= 1000000; i++) f[i] = (f[i - 1] + f[i - 2]) % mod * (i - 1) % mod, fac[i] = fac[i - 1] * (ll)i % mod; while (T--) { scanf("%lld%lld", &n, &m); if (n == m) puts("1"); else printf("%lld\n", lucas(n, n - m) * f[n - m] % mod); } }