题目 | Critical Hit
Denso Create Programming Contest 2022 Winter(AtCoder Beginner Contest 280)
E - Critical Hit
https://atcoder.jp/contests/abc280/tasks/abc280_e
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
There is a monster with initial stamina
Takahashi repeatedly attacks the monster while the monster's stamina remains
An attack by Takahashi reduces the monster's stamina by
Find the expected value, modulo
Notes
We can prove that the sought expected value is always a finite rational number. Moreover, under the Constraints of this problem, when the value is represented as
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Find the expected value, modulo
Sample Input 1
3 10
Sample Output 1
229596204
An attack by Takahashi reduces the monster's stamina by
- The monster's initial stamina is
. - After the first attack, the monster's stamina is
with probability and with probability . - After the second attack, the monster's stamina is
with probability , with probability , and with probability . With probability , the stamina becomes or less, and Takahashi stops attacking after two attacks. - If the stamina remains
after two attacks, the monster's stamina always becomes or less by the third attack, so he stops attacking after three attacks.
Therefore, the expected value is
Sample Input 2
5 100
Sample Output 2
3
Takahashi's attack always reduces the monster's stamina by
Sample Input 3
280 59
Sample Output 3
567484387
题解
题面分析
该题为一个非常基础的 DP + 模逆元题,只不过题面的 Notes 小节有点坑人,需要重点关注下(我就因为这被害了)
首先 Notes 中的变量
其次,要我们输出满足
分析后,问题就清晰了,题目就是让我们使用模逆元计算这个期望值罢了。
动态规划
我们约定
易得
接下来的递推方式为:
具体解释,要达成
- 前面达成了
点伤害,最后一步达成了 点伤害,期望为 - 前面达成了
点伤害,最后一步达成了 点伤害,期望为
模逆元
(关于模逆元:https://io.zouht.com/13.html)
原式转化为:
可见只用的找
代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
/* ll fastpow(ll a, ll b)
{
a %= MOD;
ll ans = 1;
while (b)
{
if (b % 2 == 1)
ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
inline ll inv(ll x)
{
return fastpow(x, MOD - 2);
} */
const ll MAXN = 2e5 + 10;
ll dp[MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll N, P;
cin >> N >> P;
// ll inv100 = inv(100);
ll inv100 = 828542813;
dp[1] = 1;
for (int i = 2; i <= N; i++)
{
dp[i] = P * inv100 % MOD * dp[i - 2] % MOD +
(1 - (P * inv100 % MOD) + MOD) % MOD * dp[i - 1] % MOD + 1;
dp[i] %= MOD;
}
cout << dp[N] << endl;
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi