还真的就卡常啊, T1000组, 我以为有什么nb的预处理方法呢。
每个长度的贡献期望是一样的, dp出来然后计算就好了。
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int power(int a, int b) { int ans = 1; while(b) { if(b & 1) ans = 1LL * ans * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ans; } int n, m; int inv[N]; int dp[N][N]; int main() { inv[1] = 1; for(int i = 2; i < N; i++) { inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; } int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); dp[0][0] = 1; for(int i = 1; i <= n; i++) { for(int j = (i & 1); j <= m; j += 2) { dp[i][j] = 0; if(j > 0) { add(dp[i][j], 1LL * dp[i - 1][j - 1] * (m - j + 1) % mod); } if(j < m) { add(dp[i][j], 1LL * dp[i - 1][j + 1] * (j + 1) % mod); } } } int ans = 0; for(int i = 1; i <= n; i++) { int tmp = power(m, n - i); int p = dp[i][i & 1]; int c = n - i + 1; add(ans, 1LL * p * c % mod * tmp % mod); } printf("%d ", ans); } return 0; } /**/