这两份代码是第二类斯特林数的两种求法,请帮我看看这两种求法有什么问题,对拍有答案不同的情况:
#include <iostream>
using namespace std;
const int N = 2e3, p = 1e9 + 7;
int dp[N][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
for (int i = 1; i <= N; i ++)
dp[i][1] = 1;
for (int i = 1; i <= N; i ++)
for (int j = 2; j <= N; j ++)
dp[i][j] = (1ll * dp[i - 1][j - 1] + 1ll * dp[i - 1][j] * j % p) % p;
int x, y;
cin >> x >> y;
cout << dp[x][y];
return 0;
}
#include <iostream>
using namespace std;
const int N = 2e6 + 5, p = 1e9 + 7;
int n, m, fac[N], inv[N];
int qpow (int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % p;
a = 1ll * a * a % p;
b >>= 1;
}
return res;
}
int C (int x, int y) {
int z = 1ll * fac[x] * inv[y] % p;
return 1ll * z * inv[x - y] % p;
}
int main () {
int i;
fac[0] = 1;
cin >> n >> m;
for (i = 1; i <= n + m; i ++)
fac[i] = 1ll * fac[i - 1] * i % p;
inv[n + m] = qpow(fac[n + m], p - 2);
for (i = n + m - 1; i >= 0; i --)
inv[i] = 1ll * inv[i + 1] * (i + 1) % p;
int ans = 0;
for (int k = 0; k < m; k ++) {
int cnt = (1ll * C(m, k) * qpow(m - k, n)) % p;
ans += (k & 1 ? -1 : 1) * cnt;
ans %= p;
}
cout << 1ll * ans * (1ll * inv[fac[m]] % p) % p;
return 0;
}