#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
constexpr int mod = 1000000007;
int n, k, m;
vector<vector<int>> states;
// source: https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector
class vector_hasher {
public:
std::size_t operator()(std::vector<int> const& vec) const {
std::size_t seed = vec.size();
for(auto& i : vec) {
seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
long long modPow(long long a, long long b)
{
if (b == 0) return 1;
long long v = modPow(a, b / 2);
v = (v * v) % mod;
if (b & 1) return (v * a) % mod;
else return v;
}
void dfs(vector<int> current)
{
if (current.size() == n)
{
states.push_back(current);
return;
}
for (int i : current)
{
if (i >= m) return;
}
int start = 0;
if (!current.empty()) start = current.back();
for (int i = start; i < k + m; ++i)
{
current.push_back(i);
dfs(current);
current.pop_back();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> m;
dfs({});
sort(states.begin(), states.end());
cc_hash_table<vector<int>, long long, vector_hasher> dp1, dp2;
long long answer = 0;
dp2[states.front()] = 1;
long long kInv = modPow(k, mod - 2);
for (auto state : states)
{
if (state.back() >= m)
{
answer = (answer + dp1[state]) % mod;
continue;
}
for (int i = 1; i <= k; ++i)
{
int v = state[0];
vector<int> nextState = state;
nextState.erase(nextState.begin());
for (int j = 0; j <= nextState.size(); ++j)
{
if (j == nextState.size() || nextState[j] > v + i)
{
nextState.insert(nextState.begin() + j, v + i);
break;
}
}
long long v1 = (dp1[state] + dp2[state]) % mod;
v1 = (v1 * kInv) % mod;
long long v2 = (dp2[state] * kInv) % mod;
dp1[nextState] = (dp1[nextState] + v1) % mod;
dp2[nextState] = (dp2[nextState] + v2) % mod;
}
}
cout << answer << '\n';
}
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; constexpr int mod = 1000000007; int n, k, m; vector<vector<int>> states; // source: https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector class vector_hasher { public: std::size_t operator()(std::vector<int> const& vec) const { std::size_t seed = vec.size(); for(auto& i : vec) { seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2); } return seed; } }; long long modPow(long long a, long long b) { if (b == 0) return 1; long long v = modPow(a, b / 2); v = (v * v) % mod; if (b & 1) return (v * a) % mod; else return v; } void dfs(vector<int> current) { if (current.size() == n) { states.push_back(current); return; } for (int i : current) { if (i >= m) return; } int start = 0; if (!current.empty()) start = current.back(); for (int i = start; i < k + m; ++i) { current.push_back(i); dfs(current); current.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k >> m; dfs({}); sort(states.begin(), states.end()); cc_hash_table<vector<int>, long long, vector_hasher> dp1, dp2; long long answer = 0; dp2[states.front()] = 1; long long kInv = modPow(k, mod - 2); for (auto state : states) { if (state.back() >= m) { answer = (answer + dp1[state]) % mod; continue; } for (int i = 1; i <= k; ++i) { int v = state[0]; vector<int> nextState = state; nextState.erase(nextState.begin()); for (int j = 0; j <= nextState.size(); ++j) { if (j == nextState.size() || nextState[j] > v + i) { nextState.insert(nextState.begin() + j, v + i); break; } } long long v1 = (dp1[state] + dp2[state]) % mod; v1 = (v1 * kInv) % mod; long long v2 = (dp2[state] * kInv) % mod; dp1[nextState] = (dp1[nextState] + v1) % mod; dp2[nextState] = (dp2[nextState] + v2) % mod; } } cout << answer << '\n'; } |
English