#include <bits/stdc++.h>
using std::cout;
using std::cin;
using std::ios_base;
using std::string;
using std::sort;
using std::max;
using std::min;
using std::vector;
using std::priority_queue;
using std::stack;
using std::endl;
using std::fixed;
#define be_faster_please ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
ll mod = 1000000007;
ll tab [4000];
ll fast_pow(ll a, ll b)
{
if (b == 0)
return 1;
if (b == 1)
return a;
if (b % 2 == 0)
{
ll temp = fast_pow (a, b / 2);
return (temp * temp) % mod;
}
else
{
ll temp = fast_pow (a, b / 2);
return (((temp * temp) % mod) * a) % mod;
}
}
ll wyznacz (int x, ll m)
{
if (x == 1)
return 0;
if (x == 2)
return m;
if (x == 3)
return (m * m) % mod;
ll ans = 0;
ans += fast_pow(m - 1, x - 2);
ans += (fast_pow(m - 1, x - 3) * m) % mod;
ans %= mod;
// ans += ((x - 3) * fast_pow(m - 1, x - 4)) % mod;
//ans %= mod;
ans = (ans * m) % mod;
for(int i = 2; i + 2 <= x; i++)
{
ll temp = 0;
int n = x - i;
if(n == 2)
temp = m;
else
{
temp += fast_pow(m - 1, n - 2);
// temp += (fast_pow(m - 1, n - 3) * m) % mod;
temp %= mod;
temp = (temp * m) % mod;
}
temp *= tab[i];
temp %= mod;
ans += temp;
ans %= mod;
// cout << i << ": " << temp << "\n";
}
return ans;
}
int main()
{
be_faster_please;
ll n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
tab[i] = wyznacz(i, m);
// cout << tab[i] << "\n";
}
cout << tab[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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <bits/stdc++.h> using std::cout; using std::cin; using std::ios_base; using std::string; using std::sort; using std::max; using std::min; using std::vector; using std::priority_queue; using std::stack; using std::endl; using std::fixed; #define be_faster_please ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long ll mod = 1000000007; ll tab [4000]; ll fast_pow(ll a, ll b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { ll temp = fast_pow (a, b / 2); return (temp * temp) % mod; } else { ll temp = fast_pow (a, b / 2); return (((temp * temp) % mod) * a) % mod; } } ll wyznacz (int x, ll m) { if (x == 1) return 0; if (x == 2) return m; if (x == 3) return (m * m) % mod; ll ans = 0; ans += fast_pow(m - 1, x - 2); ans += (fast_pow(m - 1, x - 3) * m) % mod; ans %= mod; // ans += ((x - 3) * fast_pow(m - 1, x - 4)) % mod; //ans %= mod; ans = (ans * m) % mod; for(int i = 2; i + 2 <= x; i++) { ll temp = 0; int n = x - i; if(n == 2) temp = m; else { temp += fast_pow(m - 1, n - 2); // temp += (fast_pow(m - 1, n - 3) * m) % mod; temp %= mod; temp = (temp * m) % mod; } temp *= tab[i]; temp %= mod; ans += temp; ans %= mod; // cout << i << ": " << temp << "\n"; } return ans; } int main() { be_faster_please; ll n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { tab[i] = wyznacz(i, m); // cout << tab[i] << "\n"; } cout << tab[n]; return 0; } |
English