【題目鏈接】
https://www.lydsy.com/JudgeOnline/problem.php?id=3157
【題解】
一道數學題,考慮使用擾動法。記
那麼有 :
就是所求的答案。接下來就可以遞推了。
時間複雜度
【代碼】
/* - - - - - - - - - - - - - - -
User : VanishD
problem : [bzoj3157]
Points : maths
- - - - - - - - - - - - - - - */
# include <bits/stdc++.h>
# define ll long long
# define inf 0x3f3f3f3f
# define M 2000
using namespace std;
int read(){
int tmp = 0, fh = 1; char ch = getchar();
while (ch < '0' || ch > '9'){ if (ch == '-') fh = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9'){ tmp = tmp * 10 + ch - '0'; ch = getchar(); }
return tmp * fh;
}
const int P = 1e9 + 7;
int n, m, f[M + 1], c[M + 1][M + 1];
int power(int x, int y){
int i = x; x = 1;
while (y > 0){
if (y % 2 == 1) x = 1ll * x * i % P;
i = 1ll * i * i % P;
y /= 2;
}
return x;
}
int pow2(int x){
return (x % 2 == 1) ? -1 : 1;
}
int main(){
n = read(), m = read();
if (m == 1){
printf("%lld\n", (1ll + n) * n / 2 % P);
return 0;
}
c[0][0] = 1;
for (int i = 1; i <= M; i++){
c[i][0] = 1;
for (int j = 1; j <= i; j++)
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
}
f[0] = 1ll * (power(m, n + 1) - m) * power(m - 1, P - 2) % P;
for (int i = 1; i <= m; i++){
f[i] = 1ll * power(n, i) * power(m, n + 1) % P;
for (int j = 0; j < i; j++)
f[i] = (f[i] + 1ll * c[i][j] * pow2(i - j) * f[j]) % P;
f[i] = 1ll * f[i] * power(m - 1, P - 2) % P;
}
printf("%d\n", (f[m] + P) % P);
return 0;
}