这种题做起来贼爽…
套路考虑一个子序列的贡献,有个问题就是每种
a
串只计算一次,如何避免重复。
很简单,对于一个串
考虑一个子序列,下标分别为
k1,k2,...,kt
。
1
到
也就是说有
空串直接单独算,然后我们就可以列出答案的式子了:
观察一下,肯定就是 xjb 变形凑二项式的形式了:
=∑j=1n∑i=1j(j−1i−1)(m−1)j−imn−j+i=∑j=0n−1mn−j∑i=0j(ji)(m−1)j−imi=∑j=0n−1mn−j(m−1+m)j
就好了。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MOD=1e9+7;
int n,m;
LL ans;
LL Pow(LL a,int b){
LL res=1;
for(;b;b>>=1,a=a*a%MOD) if(b&1) res=(res*a)%MOD;
return res;
}
int main(){
freopen("cf660E.in","r",stdin);
freopen("cf660E.out","w",stdout);
scanf("%d%d",&n,&m);
ans=Pow(m,n);
for(int i=0;i<=n-1;i++) (ans+=Pow(m,n-i)*Pow(m-1+m,i)%MOD)%=MOD;
printf("%lld\n",ans);
return 0;
}