用倍增搞,
设 Sk,q=∑ki=1iq∗mi
从 Sk 转移到 Sk+1 显然,
从 Sk 转移到 S2k :
复杂度: O(log(n)∗m2)
设 Sk=∑ni=1ik∗mi ,
Solution2:
#include<cstdio>
#include<cstdlib>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long LL;
const int N=1500,mo=1000000007;
LL n,m;
LL C[N][N];
LL S[N];
LL ksm(LL q,LL w)
{
LL ans=1;q%=mo;
while(w)
{
if(w%2)ans=(ans*q)%mo;
(q*=q)%=mo;w=w/2;
}
return ans;
}
LL ss(LL q)
{
LL ans=ksm(n,q)*ksm(m,n+1)%mo;
fo(i,1,q)(ans+=C[q][i]*((i%2)?-1:1)*S[q-i])%=mo;
return (ans%mo+mo)%mo;
}
int main()
{
scanf("%lld%lld",&n,&m);
if(m==1){printf("%lld\n",(1+n%mo)*n%mo/2%mo);return 0;}
fo(i,0,m+1)
{
C[i][0]=1;
fo(j,1,i)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mo;
}
LL mn=ksm(m-1,mo-2);
S[0]=((ksm(m,n+1)-m)%mo+mo)%mo*mn%mo;
fo(i,1,m)S[i]=(ss(i)*mn)%mo;
printf("%lld\n",S[m]);
return 0;
}