#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif
typedef long long ll;
typedef long double ld;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
vector<int> read_vec(int n) {
vector<int> v(n);
for (int& t : v) cin >> t;
return v;
}
template<typename T>
inline void upd_max(T& a, T b) {
if (a < b) {
a = b;
}
}
template<typename T>
inline void upd_min(T& a, T b) {
if (a > b) {
a = b;
}
}
template<int mod>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(ll x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (mod > 0) {
return mod;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt power(MInt a, ll b) {
MInt res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
ll v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template <int mod>
string to_string(const MInt<mod>& number) {
return to_string(number.val());
}
template<int V, int mod>
constexpr MInt<mod> CInv = MInt<mod>(V).inv();
template<>
int MInt<0>::Mod = 0;
constexpr int mod = (int)1e9 + 7;
using Mint = MInt<mod>;
int n, k, m;
const int maxN = 1e6 + 228;
Mint p[maxN];
Mint e[maxN];
Mint prefp[maxN][2];
Mint prefe[maxN][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
cin >> n >> k >> m;
p[0] = 1;
Mint se = 0;
Mint sp = 1;
Mint invk = Mint(1) / k;
FOR(i, 1, m + 1) {
if (i - k - 1 >= 0) {
sp -= p[i - k - 1];
se -= e[i - k - 1];
}
p[i] = sp * invk;
e[i] = se * invk + sp * invk;
sp += p[i];
se += e[i];
}
FOR(i, 0, m + 1) {
prefp[i + 1][0] = prefp[i][0] + p[i];
prefp[i + 1][1] = prefp[i][1] + p[i] * i;
prefe[i + 1][0] = prefe[i][0] + e[i];
prefe[i + 1][1] = prefe[i][1] + e[i] * i;
}
Mint tot = 0;
auto compute = [&](Mint x, Mint y, int z) {
//x^0 * y^z + ... + x^z * y^0
assert(0 <= z);
if (x == y) return (z + 1) * power(Mint(x), z);
return (power(x, z + 1) - power(y, z + 1)) / (x - y);
};
FOR(last, 0, m) {
if (last + k < m) continue;
//all have point >= last, some guy has exactly last -> >= m
Mint prob_not_x = 0;
Mint exp_not_x = 0;
int L = max(m - 1 - k, 0);
int R = last - 1;
if (L <= R) {
prob_not_x += (m - 1 - last) * (prefp[R + 1][0] - prefp[L][0]) * invk;
exp_not_x += (m - 1 - last) * (prefe[R + 1][0] - prefe[L][0]) * invk;
}
L = max(last - k + 1, 0);
R = max(m - 1 - k, 0) - 1;
if (L <= R) {
prob_not_x += (k - last) * (prefp[R + 1][0] - prefp[L][0]) * invk;
exp_not_x += (k - last) * (prefe[R + 1][0] - prefe[L][0]) * invk;
prob_not_x += (prefp[R + 1][1] - prefp[L][1]) * invk;
exp_not_x += (prefe[R + 1][1] - prefe[L][1]) * invk;
}
/*
FOR(s, 0, last) {
int a = last + 1;
int b = min(m - 1, s + k);
if (a > b) continue;
//I jump from s to [a .. b]
prob_not_x += p[s] * (Mint(b) - a + 1) / k;
exp_not_x += (e[s] + p[s]) * (Mint(b) - a + 1) / k;
}*/
exp_not_x += prob_not_x;
Mint good_p = (Mint(last + k) - m + 1) * invk;
Mint bad_p = 1 - good_p;
tot += n * (e[last] * bad_p + exp_not_x) * (power(p[last] + prob_not_x, n - 1) -
power(p[last] * bad_p + prob_not_x, n - 1));
tot += n * e[last] * good_p * (power(p[last] + prob_not_x, n - 1));
if (n >= 2) {
tot += bad_p * p[last] * (compute(bad_p * p[last] + prob_not_x, p[last] + prob_not_x, n - 1) -
compute(bad_p * p[last] + prob_not_x, bad_p * p[last] + prob_not_x, n - 1));
}
}
//last move
cout << tot + 1 << '\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 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif typedef long long ll; typedef long double ld; #define pb push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) vector<int> read_vec(int n) { vector<int> v(n); for (int& t : v) cin >> t; return v; } template<typename T> inline void upd_max(T& a, T b) { if (a < b) { a = b; } } template<typename T> inline void upd_min(T& a, T b) { if (a > b) { a = b; } } template<int mod> struct MInt { int x; constexpr MInt() : x{} {} constexpr MInt(ll x) : x{norm(x % getMod())} {} static int Mod; constexpr static int getMod() { if (mod > 0) { return mod; } else { return Mod; } } constexpr static void setMod(int Mod_) { Mod = Mod_; } constexpr int norm(int x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr int val() const { return x; } explicit constexpr operator int() const { return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MInt &operator*=(MInt rhs) & { x = 1LL * x * rhs.x % getMod(); return *this; } constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; } constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; } constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); } friend constexpr MInt power(MInt a, ll b) { MInt res = 1; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; } friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friend constexpr std::istream &operator>>(std::istream &is, MInt &a) { ll v; is >> v; a = MInt(v); return is; } friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); } friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } }; template <int mod> string to_string(const MInt<mod>& number) { return to_string(number.val()); } template<int V, int mod> constexpr MInt<mod> CInv = MInt<mod>(V).inv(); template<> int MInt<0>::Mod = 0; constexpr int mod = (int)1e9 + 7; using Mint = MInt<mod>; int n, k, m; const int maxN = 1e6 + 228; Mint p[maxN]; Mint e[maxN]; Mint prefp[maxN][2]; Mint prefe[maxN][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> k >> m; p[0] = 1; Mint se = 0; Mint sp = 1; Mint invk = Mint(1) / k; FOR(i, 1, m + 1) { if (i - k - 1 >= 0) { sp -= p[i - k - 1]; se -= e[i - k - 1]; } p[i] = sp * invk; e[i] = se * invk + sp * invk; sp += p[i]; se += e[i]; } FOR(i, 0, m + 1) { prefp[i + 1][0] = prefp[i][0] + p[i]; prefp[i + 1][1] = prefp[i][1] + p[i] * i; prefe[i + 1][0] = prefe[i][0] + e[i]; prefe[i + 1][1] = prefe[i][1] + e[i] * i; } Mint tot = 0; auto compute = [&](Mint x, Mint y, int z) { //x^0 * y^z + ... + x^z * y^0 assert(0 <= z); if (x == y) return (z + 1) * power(Mint(x), z); return (power(x, z + 1) - power(y, z + 1)) / (x - y); }; FOR(last, 0, m) { if (last + k < m) continue; //all have point >= last, some guy has exactly last -> >= m Mint prob_not_x = 0; Mint exp_not_x = 0; int L = max(m - 1 - k, 0); int R = last - 1; if (L <= R) { prob_not_x += (m - 1 - last) * (prefp[R + 1][0] - prefp[L][0]) * invk; exp_not_x += (m - 1 - last) * (prefe[R + 1][0] - prefe[L][0]) * invk; } L = max(last - k + 1, 0); R = max(m - 1 - k, 0) - 1; if (L <= R) { prob_not_x += (k - last) * (prefp[R + 1][0] - prefp[L][0]) * invk; exp_not_x += (k - last) * (prefe[R + 1][0] - prefe[L][0]) * invk; prob_not_x += (prefp[R + 1][1] - prefp[L][1]) * invk; exp_not_x += (prefe[R + 1][1] - prefe[L][1]) * invk; } /* FOR(s, 0, last) { int a = last + 1; int b = min(m - 1, s + k); if (a > b) continue; //I jump from s to [a .. b] prob_not_x += p[s] * (Mint(b) - a + 1) / k; exp_not_x += (e[s] + p[s]) * (Mint(b) - a + 1) / k; }*/ exp_not_x += prob_not_x; Mint good_p = (Mint(last + k) - m + 1) * invk; Mint bad_p = 1 - good_p; tot += n * (e[last] * bad_p + exp_not_x) * (power(p[last] + prob_not_x, n - 1) - power(p[last] * bad_p + prob_not_x, n - 1)); tot += n * e[last] * good_p * (power(p[last] + prob_not_x, n - 1)); if (n >= 2) { tot += bad_p * p[last] * (compute(bad_p * p[last] + prob_not_x, p[last] + prob_not_x, n - 1) - compute(bad_p * p[last] + prob_not_x, bad_p * p[last] + prob_not_x, n - 1)); } } //last move cout << tot + 1 << '\n'; return 0; } |
English