例题
一个模型
一件事情成功的概率为p,则期望的第一次成功的次数是多少?
答:1/p
第一次成功的概率为$p$
第二次成功的概率为$(1-p)\times p$
第三次成功的概率为 $(1-p)^2 \times p$
…
期望次数 $E(x) = p + 2\times (1-p)\times p$ + $3 \times (1-p)^2 \times p + \ldots = \sum_{k\ge 1}{k(1-p)^{k-1}}p$
设$f(x) = \sum_{k\ge 1}{x^k}=\frac x{1-x}(\lvert x \rvert <1)$
求导得,$f’(x) =\sum_{k\ge 1}{kx^{k-1}}=\frac{(1-x)-x\times (-1)}{(1-x)^2}=\frac 1 {(1-x)^2}$
$\Rightarrow f’(x-1)=\sum_{k\ge 1}{k(x - 1)^{k-1}}=\frac 1 {x^2}$
$\Rightarrow E(x) = f’(1-p) \times p = \frac p {p^2}=\frac 1 p$
这里终点不唯一,但起点唯一,所以从后往前推,最后的状态固定。
令$dp[i][j]$为当前是第$i$轮,已经$j$轮未获奖,到$n$轮抽奖结束获得奖品数量的期望。
状态转移:
最后的答案即$dp[0][0] \times n$(因为$dp[0][0]$表示一个人的期望,有n个人所以乘上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 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| #include<bits/stdc++.h> #define endl '\n' #define int long long using namespace std; using ll = long long;
bool multi = 1;
template<class T> constexpr T power(T a, long long b) { T res = 1; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; }
constexpr long long mul(long long a, long long b, long long p) { long long res = a * b - (long long)(1.L * a * b / p) * p; res %= p; if (res < 0) { res += p; } return res; } template<long long P> struct MLong { long long x; constexpr MLong() : x{} {} constexpr MLong(long long x) : x{norm(x % getMod())} {}
static long long Mod; constexpr static long long getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(long long Mod_) { Mod = Mod_; } constexpr long long norm(long long x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr long long val() const { return x; } explicit constexpr operator long long() const { return x; } constexpr MLong operator-() const { MLong res; res.x = norm(getMod() - x); return res; } constexpr MLong inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MLong &operator*=(MLong rhs) & { x = mul(x, rhs.x, getMod()); return *this; } constexpr MLong &operator+=(MLong rhs) & { x = norm(x + rhs.x); return *this; } constexpr MLong &operator-=(MLong rhs) & { x = norm(x - rhs.x); return *this; } constexpr MLong &operator/=(MLong rhs) & { return *this *= rhs.inv(); } friend constexpr MLong operator*(MLong lhs, MLong rhs) { MLong res = lhs; res *= rhs; return res; } friend constexpr MLong operator+(MLong lhs, MLong rhs) { MLong res = lhs; res += rhs; return res; } friend constexpr MLong operator-(MLong lhs, MLong rhs) { MLong res = lhs; res -= rhs; return res; } friend constexpr MLong operator/(MLong lhs, MLong rhs) { MLong res = lhs; res /= rhs; return res; } friend constexpr std::istream &operator>>(std::istream &is, MLong &a) { long long v; is >> v; a = MLong(v); return is; } friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) { return os << a.val(); } friend constexpr bool operator==(MLong lhs, MLong rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MLong lhs, MLong rhs) { return lhs.val() != rhs.val(); } };
template<> long long MLong<0LL>::Mod = (long long)(1E18) + 9;
template<int P> struct MInt { int x; constexpr MInt() : x{} {} constexpr MInt(long long x) : x{norm(x % getMod())} {} static int Mod; constexpr static int getMod() { if (P > 0) { return P; } 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 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) { long long 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 MInt<0>::Mod = 998244353;
template<int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353; using Z = MInt<P>;
int qpow(int a,int b,int p){ long long res=1%p; while(b){ if(b&1) res=res*a%p; a=(long long)a*a%p; b>>=1; } return res; } const int N = 2010; int n, m, k, d; Z dp[N][N]; void solve() { cin >> n >> m >> k >> d; for(int i = 0; i <= d; i++) { dp[m][i] = 0; } Z p1 = k / (Z)n, p2 = 1 - p1; for(int i = m - 1; i >= 0; i--) { for(int j = d - 1; j >= 0; j--) { if(j == d - 1) dp[i][j] = dp[i + 1][0] + 1; else dp[i][j] = (dp[i + 1][0] + 1) * p1 + dp[i + 1][j + 1] * p2; } } cout << dp[0][0] * n << '\n'; }
signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; if(multi) cin >> T; while(T--) { solve(); }
return 0; }
|