No.1196 A lazy student
タグ : / 解いたユーザー数 27
作問者 :
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
Problem Statement
capra is very indecisive and cannot decide anything.
Even when he attends a meeting, he never makes it clear whether it is YES or NO, and only talks about things that are beside the point.
Trying to take advantage of him, autumn gave capra a string $S$ of length $N$ and instructed him to act according to it.
This string $S$ consists only of the 7 kinds of strings ["YES", "NO", "and", "or", "random", "(", ")"],
and capra evaluates the string $S$ in the following way and says the final result.
Below, $x$ and $y$ denote expressions that evaluate to YES or NO.
1. The expression `$x\ or\ y$` is $\mathrm{YES}$ if at least one of them is $\mathrm{YES}$, and $\mathrm{NO}$ otherwise.
2. The expression `$x\ and\ y$` is $\mathrm{YES}$ if both of them are $\mathrm{YES}$, and $\mathrm{NO}$ otherwise.
3. The expression `$random(x\ y)$` becomes $\mathrm{YES}$ with probability $P$ if both of them are $\mathrm{YES}$, and becomes $\mathrm{YES}$ with probability $Q$ otherwise.
If it does not become $\mathrm{YES}$, it becomes $\mathrm{NO}$.
4. The contents of parentheses are computed first.
5. $\mathrm{and}$ takes precedence over $\mathrm{or}$.
6. Operations of the same precedence are performed from the left.
7. Since following instructions exactly would be boring, each time capra evaluates an $\mathrm{or}$ or an $\mathrm{and}$, with probability $R$ he changes its result, turning $\mathrm{YES}$ into $\mathrm{NO}$ and $\mathrm{NO}$ into $\mathrm{YES}$.
8. All probabilities are independent and uniform.
Thistle, who has secretly obtained the instructions, wants to know how likely it is that capra says YES.
For Thistle's sake, please write a program that returns the probability as a percentage.
It has been confirmed that this problem can be solved with plenty of margin in PyPy3.
A case is considered correct if the difference between the answer (the true value) and the submitted value is at most 1. (Added 08/22 19:31)
Input
$N$ $P$ $Q$ $R$ $S$
$N$ is an integer satisfying
$2\leq N\leq 10^6$.
$P,Q,R$ are real numbers satisfying
$0\leq P,Q,R\leq 1$.
They are given with at most 3 digits after the decimal point.
$S$ is a string of length $N$. It consists only of ["YES", "NO", "and", "or", "random", "(", ")"], and it is guaranteed to be grammatically correct.
Output
Compute the probability as a percentage, truncate the digits after the decimal point, and output it on one line.
Print a newline at the end of the line.
Sample
Sample 1
Input
3 0.8 0.2 0.5 YES
Output
100
It is always $\mathrm{YES}$, so he answers YES with a probability of 100%.
Sample 2
Input
15 0.8 0.2 0.5 YESand(YESorNO)
Output
50
capra changes the result of the $\mathrm{or}$ operation with probability 50%, so the result of ($\mathrm{YES}\ \mathrm{or}\ \mathrm{NO}$) is $\mathrm{YES}$ with probability 50% and $\mathrm{NO}$ with probability 50%.
The next $\mathrm{and}$ operation also has its result changed with probability 50%, so the answer is 50%.
Sample 3
Input
13 1.0 0.8 0.4 random(YESNO)
Output
80
For the $\mathrm{random}$ operation, the values are $\mathrm{YES}$ and $\mathrm{NO}$, so he says YES with probability 80%. That is, the answer is 80%.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。