#include <bits/stdc++.h>
using namespace std;
long long mod=1000000007;
long long spm(long long a,long long b)
{
long long k;
if(b==0)
{
return 1;
}
if(b==1)
{
return a;
}
if(b%2==0)
{
k=spm(a,b/2);
return (k*k)%mod;
}
k=spm(a,b/2);
return ((k*k)%mod*a)%mod;
}
long long t[3003];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
long long m,w;
cin>>n>>m;
t[1]=0;
t[2]=m;
t[3]=spm(m,2);
for(int i=4;i<=n;++i)
{
w=0;
//cout<<"w: \n";
for(int j=i-1;j>=0;--j)
{
//cout<<w<<' '<<t[j-1]<<' '<<spm(m,i-j)<<'\n';
w+=(t[j-1]*spm(m,i-j))%mod;
w%=mod;
}
w+=spm(m,i-1);
w-=(spm(m,i-4)*m)%mod;
w%=mod;
t[i]=w;
//cout<<'\n';
}
/*for(int i=0;i<=n;++i)
{
cout<<t[i]<<' ';
}
cout<<'\n';*/
cout<<t[n];
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <bits/stdc++.h> using namespace std; long long mod=1000000007; long long spm(long long a,long long b) { long long k; if(b==0) { return 1; } if(b==1) { return a; } if(b%2==0) { k=spm(a,b/2); return (k*k)%mod; } k=spm(a,b/2); return ((k*k)%mod*a)%mod; } long long t[3003]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; long long m,w; cin>>n>>m; t[1]=0; t[2]=m; t[3]=spm(m,2); for(int i=4;i<=n;++i) { w=0; //cout<<"w: \n"; for(int j=i-1;j>=0;--j) { //cout<<w<<' '<<t[j-1]<<' '<<spm(m,i-j)<<'\n'; w+=(t[j-1]*spm(m,i-j))%mod; w%=mod; } w+=spm(m,i-1); w-=(spm(m,i-4)*m)%mod; w%=mod; t[i]=w; //cout<<'\n'; } /*for(int i=0;i<=n;++i) { cout<<t[i]<<' '; } cout<<'\n';*/ cout<<t[n]; return 0; } |
English