description
Solution1
用倍增搞,
设
Sk,q=∑ki=1iq∗mi
从
Sk
转移到
Sk+1
显然,
从
Sk
转移到
S2k
:
S2k,q=Sk,q+mk∑i=1k(i+k)q∗mi
S2k,q=Sk,q+mk∑i=1kmi∗∑j=0qCjq∗ij∗kq−j
调换主体:
S2k,q=Sk,q+mk∑j=0qCjq∗kq−j∗∑i=1kij∗mi
S2k,q=Sk,q+mk∑j=0qCjq∗kq−j∗Sk,j
复杂度: O(log(n)∗m2)
Solution2
设
Sk=∑ni=1ik∗mi
,
(m−1)Sk=mSk−Sk
(m−1)Sk=∑i=1nik∗mi+1−∑i=1nik∗mi
(m−1)Sk=∑i=2n+1(i−1)k∗mi+1−∑i=1nik∗mi
把 (i−1)k 拆开,发现一堆可以抵消,整理一下:
(m−1)Sk=nkmn+1−m+∑j=1kCjk∗(−1)j∗∑i=2nmi∗ik−j
或者:(但要注意k=0的情况)
(m−1)Sk=nkmn+1+∑j=1kCjk∗(−1)j∗∑i=1nmi∗ik−j
(m−1)Sk=nkmn+1+∑j=1kCjk∗(−1)j∗Sk−j
愉快的用 O(m2) 搞,
Code
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;
}