题目:
s
u
m
sum
sum
=
1
1
+
1
2
+
⋅
⋅
⋅
+
1
M
+
2
1
+
2
2
+
⋅
⋅
⋅
+
2
M
+
⋅
⋅
⋅
+
N
1
+
N
2
+
⋅
⋅
⋅
+
N
M
=1^1+1^2+···+1^M+2^1+2^2+···+2^M+···+N^1+N^2+···+N^M
=11+12+⋅⋅⋅+1M+21+22+⋅⋅⋅+2M+⋅⋅⋅+N1+N2+⋅⋅⋅+NM
=
M
+
2
M
+
1
−
2
2
−
1
+
3
M
+
1
−
3
3
−
1
+
⋅
⋅
⋅
+
N
M
+
1
−
N
N
−
1
=M+\frac{2^{M+1}-2}{2-1}+\frac{3^{M+1}-3}{3-1}+···+\frac{N^{M+1}-N}{N-1}
=M+2−12M+1−2+3−13M+1−3+⋅⋅⋅+N−1NM+1−N
对于以一为底数的数的和,可以直接加上M;
从2到N枚举每个数记为i,则要算出
i
M
+
1
−
i
i
−
1
\frac{i^{M+1}-i}{i-1}
i−1iM+1−i的值,累加。(在计算过程以及结果中别忘了 mod 10^9+7。)
i
M
+
1
i^{M+1}
iM+1可以用快速幂算出;
至于除以
i
−
1
i-1
i−1,可以利用逆元,即:
a
/
b
m
o
d
p
=
a
∗
b
p
−
2
m
o
d
p
a/b\mod p=a*b^{p-2}\mod p
a/bmodp=a∗bp−2modp
记
i
M
+
1
i^{M+1}
iM+1为t,则要算出
t
∗
(
i
−
1
)
1
0
9
+
7
−
2
t*(i-1)^{10^9+7-2}
t∗(i−1)109+7−2,用快速幂即可。
代码实现:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,mod=1000000007;
ll ans=0;
int power(ll a,ll b,ll p){
ll ans=1%p;
for(;b;b>>=1){
if(b&1)ans=ans*a%p;
a=a*a%p;
}
return ans;
}
int main(){
cin>>n>>m;
ans+=m;
for(int i=2;i<=n;i++){
ll t=power(i,m+1,mod);
t=(t-i)%mod;
ll x=power(i-1,mod-2,mod);
t=t*x%mod;
ans=(ans+t)%mod;
}
cout<<ans;
return 0;
}