#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;
const i64 MOD = 1'000'000'007;
void add_mod(i64 &a, i64 b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
void sub_mod(i64 &a, i64 b) {
a -= b;
if (a < 0) {
a += MOD;
}
}
i64 mul_mod(i64 a, i64 b) {
return (a * b) % MOD;
}
i64 pow_mod(i64 a, i64 n) {
i64 r = 1;
while (n) {
if (n % 2 == 1) {
r = mul_mod(a, r);
}
n /= 2;
a = mul_mod(a, a);
}
return r;
}
i64 inv_mod(i64 a) {
return pow_mod(a, MOD - 2);
}
struct ProbEV {
i64 prob;
i64 ev;
ProbEV() : prob{0}, ev{0} {}
ProbEV(i64 prob_, i64 ev_) : prob{prob_}, ev{ev_} {}
ProbEV &operator*=(const ProbEV &other) {
ev = (prob * other.ev + other.prob * ev) % MOD;
prob = mul_mod(prob, other.prob);
return *this;
}
ProbEV operator*(const ProbEV &other) const {
ProbEV ret = *this;
ret *= other;
return ret;
}
ProbEV &operator*=(int p) {
ev = mul_mod(ev, p);
prob = mul_mod(prob, p);
return *this;
}
ProbEV operator*(int p) const {
ProbEV ret = *this;
ret *= p;
return ret;
}
ProbEV &operator+=(const ProbEV &other) {
add_mod(prob, other.prob);
add_mod(ev, other.ev);
return *this;
}
ProbEV operator+(const ProbEV &other) const {
ProbEV ret = *this;
ret += other;
return ret;
}
ProbEV &operator-=(const ProbEV &other) {
sub_mod(prob, other.prob);
sub_mod(ev, other.ev);
return *this;
}
ProbEV operator-(const ProbEV &other) const {
ProbEV ret = *this;
ret -= other;
return ret;
}
friend ostream &operator<<(ostream &os, const ProbEV &val) {
os << "(p=" << val.prob << ", ev=" << val.ev << ")";
return os;
}
};
// \sum_{i=0}^n A^i B^{n-i}. Assume that One is identity wrt multiplication
template <typename T>
T GeomABSum(T A, T B, T one, i64 n) {
// More general: given also C, D, compute
// (\sum_{i=0}^n A^i B^{n-i}) * C + D * B^n
T C = one, D = T{};
while (n) {
if (n % 2 == 1) {
C *= (A + B);
D *= B;
A *= A;
B *= B;
n /= 2;
} else {
D = (D + C) * B;
C *= A;
--n;
}
}
return C + D;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(14);
cerr << fixed << setprecision(6);
int N, K, M;
cin >> N >> K >> M;
const ProbEV one_step{1, 1};
const ProbEV prob_divk{inv_mod(K), 0};
const ProbEV step_divk = prob_divk * one_step;
vector<ProbEV> jump_to(M + 1);
ProbEV jump_to_cur_intv;
jump_to[0] = ProbEV{1, 0};
for (int s = 1; s <= M; ++s) {
jump_to_cur_intv += jump_to[s - 1];
jump_to[s] = jump_to_cur_intv * step_divk;
if (s >= K) {
jump_to_cur_intv -= jump_to[s - K];
}
}
vector<ProbEV> jump_no_finish(M + 1);
jump_no_finish[0] = ProbEV{1, 0};
for (int i = 0; i < M; ++i) {
const int last = min(M - 1, i + K);
const ProbEV coef = jump_to[i] * step_divk;
jump_no_finish[last] += coef;
jump_no_finish[i] -= coef * (last - i + 1);
if (i) {
jump_no_finish[i - 1] += coef * (last - i);
}
}
for (int iter = 0; iter < 2; ++iter) {
for (int i = M; i > 0; --i) {
jump_no_finish[i - 1] += jump_no_finish[i];
}
}
ProbEV ans;
for (int s = max(0, M - K); s < M; ++s) {
const int nopts = s + K - M + 1;
ProbEV coef = jump_to[s] * prob_divk * nopts * one_step;
coef *= GeomABSum(jump_no_finish[s], jump_no_finish[s + 1], ProbEV{1, 0}, N - 1);
ans += coef;
}
debug(ans);
assert(ans.prob == 1);
cout << ans.ev << "\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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; #ifdef LOCAL template<typename A, typename B> auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif using i64 = long long; using ll = long long; using pii = pair<int, int>; using pll = pair<i64, i64>; using vi = vector<int>; using vll = vector<i64>; const i64 MOD = 1'000'000'007; void add_mod(i64 &a, i64 b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_mod(i64 &a, i64 b) { a -= b; if (a < 0) { a += MOD; } } i64 mul_mod(i64 a, i64 b) { return (a * b) % MOD; } i64 pow_mod(i64 a, i64 n) { i64 r = 1; while (n) { if (n % 2 == 1) { r = mul_mod(a, r); } n /= 2; a = mul_mod(a, a); } return r; } i64 inv_mod(i64 a) { return pow_mod(a, MOD - 2); } struct ProbEV { i64 prob; i64 ev; ProbEV() : prob{0}, ev{0} {} ProbEV(i64 prob_, i64 ev_) : prob{prob_}, ev{ev_} {} ProbEV &operator*=(const ProbEV &other) { ev = (prob * other.ev + other.prob * ev) % MOD; prob = mul_mod(prob, other.prob); return *this; } ProbEV operator*(const ProbEV &other) const { ProbEV ret = *this; ret *= other; return ret; } ProbEV &operator*=(int p) { ev = mul_mod(ev, p); prob = mul_mod(prob, p); return *this; } ProbEV operator*(int p) const { ProbEV ret = *this; ret *= p; return ret; } ProbEV &operator+=(const ProbEV &other) { add_mod(prob, other.prob); add_mod(ev, other.ev); return *this; } ProbEV operator+(const ProbEV &other) const { ProbEV ret = *this; ret += other; return ret; } ProbEV &operator-=(const ProbEV &other) { sub_mod(prob, other.prob); sub_mod(ev, other.ev); return *this; } ProbEV operator-(const ProbEV &other) const { ProbEV ret = *this; ret -= other; return ret; } friend ostream &operator<<(ostream &os, const ProbEV &val) { os << "(p=" << val.prob << ", ev=" << val.ev << ")"; return os; } }; // \sum_{i=0}^n A^i B^{n-i}. Assume that One is identity wrt multiplication template <typename T> T GeomABSum(T A, T B, T one, i64 n) { // More general: given also C, D, compute // (\sum_{i=0}^n A^i B^{n-i}) * C + D * B^n T C = one, D = T{}; while (n) { if (n % 2 == 1) { C *= (A + B); D *= B; A *= A; B *= B; n /= 2; } else { D = (D + C) * B; C *= A; --n; } } return C + D; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(14); cerr << fixed << setprecision(6); int N, K, M; cin >> N >> K >> M; const ProbEV one_step{1, 1}; const ProbEV prob_divk{inv_mod(K), 0}; const ProbEV step_divk = prob_divk * one_step; vector<ProbEV> jump_to(M + 1); ProbEV jump_to_cur_intv; jump_to[0] = ProbEV{1, 0}; for (int s = 1; s <= M; ++s) { jump_to_cur_intv += jump_to[s - 1]; jump_to[s] = jump_to_cur_intv * step_divk; if (s >= K) { jump_to_cur_intv -= jump_to[s - K]; } } vector<ProbEV> jump_no_finish(M + 1); jump_no_finish[0] = ProbEV{1, 0}; for (int i = 0; i < M; ++i) { const int last = min(M - 1, i + K); const ProbEV coef = jump_to[i] * step_divk; jump_no_finish[last] += coef; jump_no_finish[i] -= coef * (last - i + 1); if (i) { jump_no_finish[i - 1] += coef * (last - i); } } for (int iter = 0; iter < 2; ++iter) { for (int i = M; i > 0; --i) { jump_no_finish[i - 1] += jump_no_finish[i]; } } ProbEV ans; for (int s = max(0, M - K); s < M; ++s) { const int nopts = s + K - M + 1; ProbEV coef = jump_to[s] * prob_divk * nopts * one_step; coef *= GeomABSum(jump_no_finish[s], jump_no_finish[s + 1], ProbEV{1, 0}, N - 1); ans += coef; } debug(ans); assert(ans.prob == 1); cout << ans.ev << "\n"; } |
English