description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
min_ans = -1
for i in range(1, 10000):
nn = n
nn -= i * p
if nn > 0:
st = bin(nn)[2:]
if st.count("1") <= i and nn >= i:
min_ans = i
break
print(min_ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
if n == 0:
print(0)
elif p >= n:
print(-1)
else:
i = 0
while 1:
n -= p
if n <= 0:
print(-1)
break
i += 1
s = str(bin(n))
k = 0
for j in s:
if j == "1":
k += 1
if k <= i and n >= i:
print(i)
break | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = -1
for i in range(40):
t = n - p * i
if t < 0:
break
cnt = 0
while t:
if t & 1:
cnt += 1
t >>= 1
if n - p * i >= i and cnt <= i:
print(i)
exit()
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, m = map(int, input().split())
i = 1
count = 0
flag = 0
while True:
count = 0
if n - m * i <= 0:
flag = 1
break
str = bin(n - m * i).replace("0b", "")
for j in str:
if j == "1":
count += 1
if i >= count and i <= n - m * i:
break
i += 1
if flag == 1:
print(-1)
else:
print(i) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING STRING FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
Output
Print a single integer — the minimum number of hints that the other players should make.
Examples
Input
2
G3 G3
Output
0
Input
4
G4 R4 R3 B3
Output
2
Input
5
B1 Y1 W1 G1 R1
Output
4
Note
In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors. | A = {"R": 0, "G": 1, "B": 2, "Y": 3, "W": 4}
def bit_m(s):
num = 1 << int(s[1]) - 1
symb = 1 << A[s[0]] + 5
return num | symb
n = int(input())
cards = list(set(input().split()))
res = [(0) for i in range(len(cards))]
mask = [bit_m(cards[i]) for i in range(len(cards))]
def make_res(Q, cards):
for i in range(len(cards)):
if mask[i] & Q:
res[i] |= Q
def check():
for i in range(len(res)):
for j in range(i + 1, len(res)):
if res[i] == res[j]:
return 0
return 1
quest = [(1 << i) for i in range(10)]
cnt = 11
for i in range(0, 1023):
cnt_now = 0
res = [(0) for i in range(len(cards))]
for j in range(10):
if 1 << j & i != 0:
cnt_now += 1
make_res(quest[j], cards)
if check():
cnt = min(cnt, cnt_now)
print(cnt) | ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
Output
Print a single integer — the minimum number of hints that the other players should make.
Examples
Input
2
G3 G3
Output
0
Input
4
G4 R4 R3 B3
Output
2
Input
5
B1 Y1 W1 G1 R1
Output
4
Note
In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors. | def main():
p = [1]
for i in range(1, 11):
p.append(p[i - 1] * 2)
hr = "12345RGBYW"
gt = {
"1": 0,
"2": 1,
"3": 2,
"4": 3,
"5": 4,
"R": 5,
"G": 6,
"B": 7,
"Y": 8,
"W": 9,
}
def check(c, mask):
tmp = gt[c]
if mask // p[tmp] % 2 == 1:
return True
return False
def count(mask):
tmp = 0
while mask > 0:
tmp += mask % 2
mask //= 2
return tmp
n = int(input())
a = list(set(input().split()))
n = len(a)
ans = 10
for mask in range(1023):
f = True
for i in range(n):
for j in range(i + 1, n):
if a[i] == a[j]:
pass
elif a[i][1] != a[j][1] and (
check(a[i][1], mask) or check(a[j][1], mask)
):
pass
elif a[i][0] != a[j][0] and (
check(a[i][0], mask) or check(a[j][0], mask)
):
pass
else:
f = False
break
if not f:
break
if f:
ans = min(ans, count(mask))
print(ans)
main() | FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
Output
Print a single integer — the minimum number of hints that the other players should make.
Examples
Input
2
G3 G3
Output
0
Input
4
G4 R4 R3 B3
Output
2
Input
5
B1 Y1 W1 G1 R1
Output
4
Note
In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors. | input()
p = {((1 << "RGBYW".index(c)) + (1 << int(k) + 4)) for c, k in input().split()}
print(
min(bin(t).count("1") for t in range(1024) if len({(t & q) for q in p}) == len(p))
) | EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR |
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
Output
Print a single integer — the minimum number of hints that the other players should make.
Examples
Input
2
G3 G3
Output
0
Input
4
G4 R4 R3 B3
Output
2
Input
5
B1 Y1 W1 G1 R1
Output
4
Note
In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors. | n = int(input())
data = set(input().split())
temp = "RGBYW"
c = []
res = 10
for i in data:
p1 = temp.find(i[0])
p2 = int(i[1]) - 1
c.append(2**p1 + 2 ** (p2 + 5))
for i in range(1024):
ok = True
for j in range(len(c) - 1):
for k in range(j + 1, len(c)):
if c[j] & i == c[k] & i:
ok = False
if ok:
tt = 0
inow = i
while inow != 0:
tt += inow % 2
inow //= 2
res = min(res, tt)
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import sys
def get_sol(a, b, c, n, reverse):
if reverse[0]:
a = a[1], a[0], a[2]
if reverse[1]:
b = b[1], b[0], b[2]
if reverse[2]:
c = c[1], c[0], c[2]
ans = []
if a[0] == b[0] == c[0] == n:
if a[1] + b[1] + c[1] == n:
for i in range(a[1]):
ans.append(a[2] * n)
for i in range(b[1]):
ans.append(b[2] * n)
for i in range(c[1]):
ans.append(c[2] * n)
return True, ans
if a[0] + c[0] == b[0] + c[0] == n and c[1] == n == a[1] + b[1]:
for i in range(a[1]):
ans.append(a[2] * a[0] + c[2] * c[0])
for i in range(b[1]):
ans.append(b[2] * b[0] + c[2] * c[0])
return True, ans
return False, ans
def printans(ans, n):
print(n)
for line in ans:
print(line)
return
x1, y1, x2, y2, x3, y3 = [int(i) for i in input().split()]
total_area = x1 * y1 + x2 * y2 + x3 * y3
n = 0
while n**2 < total_area:
n += 1
if n**2 != total_area:
print(-1)
else:
first = x1, y1, "A"
second = x2, y2, "B"
third = x3, y3, "C"
pereb = (
(first, second, third),
(first, third, second),
(second, first, third),
(second, third, first),
(third, first, second),
(third, second, first),
)
for rev1 in (False, True):
for rev2 in (False, True):
for rev3 in (False, True):
for per in pereb:
reverse = rev1, rev2, rev3
is_ans, ans = get_sol(per[0], per[1], per[2], n, reverse)
if is_ans:
printans(ans, n)
print(-1) | IMPORT FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR RETURN NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR NUMBER NUMBER FOR VAR NUMBER NUMBER FOR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def print_if_possible(w, h):
xw, xh = max(w), max(h)
nw, nh = min(w), min(h)
x = max(xw, xh)
wm, hm = w.count(xw), h.count(xh)
if hm == 1 and xh == x:
q = h.index(xh)
i, j = (q + 1) % 3, (q + 2) % 3
if xh != sum(w) - w[i] or h[q] != h[i] + h[j] or w[i] != w[j]:
return False
print(x)
for yi in range(h[i]):
print("%s%s" % ("ABC"[i] * w[i], "ABC"[q] * w[q]))
for yi in range(h[j]):
print("%s%s" % ("ABC"[j] * w[j], "ABC"[q] * w[q]))
return True
elif wm == 1 and xw == x:
q = w.index(xw)
i, j = (q + 1) % 3, (q + 2) % 3
if xw != sum(h) - h[i] or w[q] != w[i] + w[j] or h[i] != h[j]:
return False
print(x)
for yi in range(h[i]):
print("%s%s" % ("ABC"[i] * w[i], "ABC"[j] * w[j]))
for yi in range(h[q]):
print("ABC"[q] * w[q])
return True
elif wm == 3 and xw == sum(h):
q = w.index(xw)
i, j = (q + 1) % 3, (q + 2) % 3
print(x)
for yi in range(h[i]):
print("ABC"[i] * w[i])
for yi in range(h[j]):
print("ABC"[j] * w[j])
for yi in range(h[q]):
print("ABC"[q] * w[q])
return True
elif hm == 3 and xh == sum(w):
q = h.index(xh)
i, j = (q + 1) % 3, (q + 2) % 3
print(x)
for yi in range(h[i]):
print("%s%s%s" % ("ABC"[i] * w[i], "ABC"[j] * w[j], "ABC"[q] * w[q]))
return True
else:
return False
def rec(i, w, h):
if i == 3:
if print_if_possible(w, h):
return
return
rec(i + 1, w, h)
w[i], h[i] = h[i], w[i]
rec(i + 1, w, h)
w[i], h[i] = h[i], w[i]
w, h = [0, 0, 0], [0, 0, 0]
w[0], h[0], w[1], h[1], w[2], h[2] = [int(i) for i in input().split()]
rec(0, w, h)
print(-1) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import itertools
x1, y1, x2, y2, x3, y3 = list(map(int, input().split(" ")[:6]))
A, B, C = (x1, y1, "A"), (x2, y2, "B"), (x3, y3, "C")
def rot(X, do_rot):
return (X[1], X[0], X[2]) if do_rot else X
def solve_1(A, B, C):
w = A[0] + B[0] + C[0]
if A[1] == B[1] == C[1] == w:
res = [["" for j in range(w)] for i in range(w)]
for i in range(w):
for j in range(w):
if j < A[0]:
res[i][j] = A[2]
elif j < A[0] + B[0]:
res[i][j] = B[2]
else:
res[i][j] = C[2]
return res
def solve_2(A, B, C):
w = A[0] + B[0]
if w == A[1] + C[1] and C[0] == w and B[1] == A[1]:
res = [["" for j in range(w)] for i in range(w)]
for i in range(w):
for j in range(w):
if i < A[1]:
if j < A[0]:
res[i][j] = A[2]
else:
res[i][j] = B[2]
else:
res[i][j] = C[2]
return res
for I, J, K in itertools.permutations([A, B, C]):
for i in range(2):
for j in range(2):
for k in range(2):
L = rot(I, i)
M = rot(J, j)
N = rot(K, k)
v = solve_1(L, M, N)
if not v:
v = solve_2(L, M, N)
if v:
print(len(v))
for l in v:
print("".join(map(str, l)))
return
print(-1) | IMPORT ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR STRING VAR VAR STRING VAR VAR STRING FUNC_DEF RETURN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | a1, a2, b1, b2, c1, c2 = map(int, input().split())
l = max([a1, a2, b1, b2, c1, c2])
if a1 * a2 + b1 * b2 + c1 * c2 != l**2:
print(-1)
else:
if a1 > a2:
a1, a2 = a2, a1
if b1 > b2:
b1, b2 = b2, b1
if c1 > c2:
c1, c2 = c2, c1
if a2 == b2 and b2 == c2:
print(l)
for i in range(a1):
print("A" * a2)
for i in range(b1):
print("B" * b2)
for i in range(c1):
print("C" * c2)
else:
ls = [[a1, a2, "A"], [b1, b2, "B"], [c1, c2, "C"]]
if b2 == l:
ls[0], ls[1] = ls[1], ls[0]
if c2 == l:
ls[0], ls[2] = ls[2], ls[0]
valid = True
if ls[1][0] == ls[2][0]:
pass
elif ls[1][1] == ls[2][1]:
ls[1][0], ls[1][1] = ls[1][1], ls[1][0]
ls[2][0], ls[2][1] = ls[2][1], ls[2][0]
elif ls[1][0] == ls[2][1]:
ls[2][0], ls[2][1] = ls[2][1], ls[2][0]
elif ls[1][1] == ls[2][0]:
ls[1][0], ls[1][1] = ls[1][1], ls[1][0]
else:
valid = False
if ls[1][0] + ls[0][0] != l or ls[1][1] + ls[2][1] != l:
valid = False
if not valid:
print(-1)
else:
print(l)
for i in range(ls[0][0]):
print(ls[0][2] * l)
for i in range(ls[1][0]):
print(ls[1][2] * ls[1][1] + ls[2][2] * ls[2][1]) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR LIST LIST VAR VAR STRING LIST VAR VAR STRING LIST VAR VAR STRING IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def getprint(c1, c2, c3, maxi, ost, delta):
ans = str(maxi) + "\n" + (c1 * maxi + "\n") * ost
return ans + (c2 * delta + c3 * (maxi - delta) + "\n") * (maxi - ost)
def solve():
a = list(map(int, input().split()))
if max(a) ** 2 != a[0] * a[1] + a[2] * a[3] + a[4] * a[5]:
return -1
maxi = max(a)
if maxi in a[:2] and maxi in a[2:4] and maxi in a[4:]:
return (
str(maxi)
+ "\n"
+ ("A" * maxi + "\n") * min(a[:2])
+ ("B" * maxi + "\n") * min(a[2:4])
+ ("C" * maxi + "\n") * min(a[4:])
)
ind = a.index(maxi)
ost = a[ind ^ 1]
if ind // 2 == 0 and maxi - ost in a[2:4] and maxi - ost in a[4:]:
return getprint("A", "B", "C", maxi, ost, a[2] * a[3] // (maxi - ost))
elif ind // 2 == 1 and maxi - ost in a[:2] and maxi - ost in a[4:]:
return getprint("B", "A", "C", maxi, ost, a[0] * a[1] // (maxi - ost))
elif ind // 2 == 2 and maxi - ost in a[:2] and maxi - ost in a[2:4]:
return getprint("C", "A", "B", maxi, ost, a[0] * a[1] // (maxi - ost))
return -1
print(solve()) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING BIN_OP BIN_OP BIN_OP STRING VAR STRING FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP STRING VAR STRING FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP STRING VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING STRING STRING VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING STRING STRING VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR STRING STRING STRING VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | a, b, c, d, e, f = map(int, input().split())
n, n1 = 1, a * b + c * d + e * f
while n**2 < n1:
n += 1
if n**2 > n1:
print(-1)
exit()
l = sorted(
[
[max(a, b), min(a, b), "A"],
[max(c, d), min(c, d), "B"],
[max(e, f), min(e, f), "C"],
]
)
if l[2][0] != n:
print(-1)
exit()
v = str(n) + "\n" + (l[2][2] * n + "\n") * l[2][1]
if l[0][0] == n and l[1][0] == n:
for i in range(2):
v += (l[i][2] * n + "\n") * l[i][1]
else:
s = n - l[2][1]
if s not in l[0] or s not in l[1]:
print(-1)
exit()
x, y = l[0][1] if s == l[0][0] else l[0][0], l[1][1] if s == l[1][0] else l[1][0]
v += (l[0][2] * x + l[1][2] * y + "\n") * s
print(v) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR STRING VAR EXPR FUNC_CALL VAR VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def out(t, x1, y1, x2, y2, x3, y3, m):
if t == 1:
print(x1)
for i in range(y1):
for j in range(x1):
print("A", end="")
print()
for i in range(y2):
for j in range(x1):
print("B", end="")
print()
for i in range(y3):
for j in range(x1):
print("C", end="")
print()
elif m == 1:
print(x1)
for i in range(y1):
for j in range(x1):
print("A", end="")
print()
for i in range(x1 - y1):
for j in range(x1):
if x2 > j:
print("B", end="")
else:
print("C", end="")
print()
elif m == 2:
print(x2)
for i in range(y2):
for j in range(x2):
print("B", end="")
print()
for i in range(x2 - y2):
for j in range(x2):
if x1 > j:
print("A", end="")
else:
print("C", end="")
print()
else:
print(x3)
for i in range(y3):
for j in range(x3):
print("C", end="")
print()
for i in range(x3 - y3):
for j in range(x3):
if x1 > j:
print("A", end="")
else:
print("B", end="")
print()
def check1(x1, y1, x2, y2, x3, y3):
a, b = max(x1, y1), min(x1, y1)
b += (y2 if x2 == a else x2) + (y3 if x3 == a else x3)
if a == b:
out(1, a, min(x1, y1), a, y2 if x2 == a else x2, a, y3 if x3 == a else x3, 1)
return a == b
def check2(x1, y1, x2, y2, x3, y3):
a, b = max(x1, y1), min(x1, y1)
bt = x2 + x3
if bt == a and y2 == a - b and y3 == a - b:
out(2, a, b, x2, y2, x3, y3, 1)
exit(0)
bt = x2 + y3
if bt == a and y2 == a - b and x3 == a - b:
out(2, a, b, x2, y2, y3, x3, 1)
exit(0)
bt = y2 + x3
if bt == a and x2 == a - b and y3 == a - b:
out(2, a, b, y2, x2, x3, y3, 1)
exit(0)
bt = y2 + y3
if bt == a and x2 == a - b and x3 == a - b:
out(2, a, b, y2, x2, y3, x3, 1)
exit(0)
a, b = max(x2, y2), min(x2, y2)
bt = x1 + x3
if bt == a and y1 == a - b and y3 == a - b:
out(2, x1, y1, a, b, x3, y3, 2)
exit(0)
bt = x1 + y3
if bt == a and y1 == a - b and x3 == a - b:
out(2, x1, y1, a, b, y3, x3, 2)
exit(0)
bt = y1 + x3
if bt == a and x1 == a - b and y3 == a - b:
out(2, y1, x1, a, b, x3, y3, 2)
exit(0)
bt = y1 + y3
if bt == a and x1 == a - b and x3 == a - b:
out(2, y1, x1, a, b, y3, x3, 2)
exit(0)
a, b = max(x3, y3), min(x3, y3)
bt = x2 + x1
if bt == a and y2 == a - b and y1 == a - b:
out(2, x1, y1, x2, y2, a, b, 3)
exit(0)
bt = x2 + y1
if bt == a and y2 == a - b and x1 == a - b:
out(2, y1, x1, x2, y2, a, b, 3)
exit(0)
bt = y2 + x1
if bt == a and x2 == a - b and y1 == a - b:
out(2, x1, y1, y2, x2, a, b, 3)
exit(0)
bt = y2 + y1
if bt == a and x2 == a - b and x1 == a - b:
out(2, y1, x1, y2, x2, a, b, 3)
exit(0)
x1, y1, x2, y2, x3, y3 = [int(x) for x in input().split()]
b = check1(x1, y1, x2, y2, x3, y3)
if b:
exit(0)
b = check2(x1, y1, x2, y2, x3, y3)
if b:
exit(0)
print(-1) | FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | __author__ = "User"
def f(arr, value, deep, vis):
global flag
if flag == True:
return
fl = False
pos = 0, 0
for i in range(n):
for j in range(n):
if arr[i][j] == 0:
pos = i, j
fl = True
break
if fl == True:
break
symbol = value[2]
for y, x in value[:-1]:
tarr = arr[:]
y0, x0 = pos[0], pos[1]
if y0 < n and x0 < n and arr[y0][x0] == 0:
if x0 + x - 1 < n and y0 + y - 1 < n:
tr = True
for i in range(y0, y0 + y):
for j in range(x0, x0 + x):
if arr[i][j] != 0:
tr = False
break
arr[i][j] = symbol
if tr == False:
break
if tr:
if deep == 0:
print(n)
flag = True
for i in range(n):
print(*arr[i], sep="")
return
for k in range(3):
if vis[k] == 0:
vis[k] = -1
f(arr, val[k], deep - 1, vis)
tr = True
for i in range(y0, y0 + y):
for j in range(x0, x0 + x):
if arr[i][j] == symbol:
arr[i][j] = 0
else:
tr = False
break
if tr == False:
break
x1, y1, x2, y2, x3, y3 = map(int, input().split())
val = [[(x1, y1), (y1, x1), "A"], [(x2, y2), (y2, x2), "B"], [(x3, y3), (y3, x3), "C"]]
s = x1 * y1 + x2 * y2 + x3 * y3
flag = True
mx = max(x1, x2, x3, y1, y2, y3)
if round(s ** (1 / 2)) == round(s ** (1 / 2), 8):
n = int(s**0.5)
if n < mx:
flag = False
else:
flag = False
if flag == True:
flag = False
arr = [(n * [0]) for i in range(n)]
for i in range(3):
deep = 3
vis = [0] * 3
vis[i] = -1
f(arr, val[i], deep - 1, vis)
if flag == False:
print(-1)
else:
print(-1) | ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR VAR VAR VAR STRING LIST VAR VAR VAR VAR STRING LIST VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def main():
mode = "filee"
if mode == "file":
p = open("test.txt", "r")
get = lambda: [
int(x) for x in (p.readline() if mode == "file" else input()).split()
]
[a, b, c, d, e, f] = get()
g = [[a, b, "A"], [c, d, "B"], [e, f, "C"]]
n = 0
for i in g:
n = max(n, max(i[:2]))
if a * b + c * d + e * f != n * n:
print("-1")
return
h = []
print(n)
for i in g:
if n in i:
for j in range(min(i[:2])):
print(i[2] * max(i[:2]))
else:
h.append(i)
if len(h) > 0:
for j in range(2):
for k in range(2):
if h[0][j] == h[1][k] and h[0][1 - j] + h[1][1 - k] == n:
l1 = h[0][1 - j]
l2 = h[1][1 - k]
times = h[0][j]
c1 = h[0][2]
c2 = h[1][2]
break
for i in range(times):
print(c1 * l1 + c2 * l2)
if mode == "file":
p.close()
main() | FUNC_DEF ASSIGN VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR VAR STRING LIST VAR VAR STRING LIST VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | a, b, c, d, e, f = list(map(int, input().split()))
m = sorted(
[
(max(a, b), min(a, b), "A"),
(max(c, d), min(c, d), "B"),
(max(e, f), min(e, f), "C"),
]
)
ma = m[-1][0]
ma2 = ma - m[-1][1]
ans = [(["A"] * ma) for i in range(ma)]
if a * b + c * d + e * f != ma * ma:
print(-1)
elif m[0][0] * m[0][1] + m[1][0] * m[1][1] != ma2 * ma:
print(-1)
elif m[0][0] != m[1][0]:
if m[0][0] != m[1][1] and m[0][1] != m[1][1]:
print(-1)
else:
for i in range(m[-1][1]):
for j in range(ma):
ans[i][j] = m[-1][2]
for i in range(m[-1][1], ma):
for j in range(m[0][0] + m[0][1] - ma2):
ans[i][j] = m[0][2]
for i in range(m[-1][1], ma):
for j in range(m[0][0] + m[0][1] - ma2, ma):
ans[i][j] = m[1][2]
print(ma)
for x in ans:
print("".join(x))
else:
for i in range(m[-1][1]):
for j in range(ma):
ans[i][j] = m[-1][2]
if m[0][0] == ma:
for i in range(m[-1][1], m[-1][1] + m[0][1]):
for j in range(ma):
ans[i][j] = m[0][2]
for i in range(m[-1][1] + m[0][1], ma):
for j in range(ma):
ans[i][j] = m[1][2]
else:
for i in range(m[-1][1], ma):
for j in range(m[0][1]):
ans[i][j] = m[0][2]
for i in range(m[-1][1], ma):
for j in range(m[0][1], ma):
ans[i][j] = m[1][2]
print(ma)
for x in ans:
print("".join(x)) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import itertools
a, b, c = [[(0) for _ in range(2)] for _ in range(3)]
a[0], a[1], b[0], b[1], c[0], c[1] = [int(i) for i in input().split()]
if max(a) == max(b) == max(c) == min(a) + min(b) + min(c):
print(max(a))
for _ in range(min(a)):
print("".join(["A" for __ in range(max(a))]))
for _ in range(min(b)):
print("".join(["B" for __ in range(max(a))]))
for _ in range(min(c)):
print("".join(["C" for __ in range(max(a))]))
else:
fl = False
for i in itertools.permutations(a):
for j in itertools.permutations(b):
for k in itertools.permutations(c):
for l in itertools.permutations([("A", i), ("B", j), ("C", k)]):
if (
l[0][1][1] == l[0][1][0] + l[1][1][0]
and l[0][1][1] == l[1][1][1] + l[2][1][1]
and l[1][1][0] == l[2][1][0]
):
if fl:
break
print(l[0][1][1])
for _ in range(l[0][1][0]):
print("".join([l[0][0] for __ in range(l[0][1][1])]))
for _ in range(l[1][1][0]):
print(
"".join(
[l[1][0] for __ in range(l[1][1][1])]
+ [l[2][0] for __ in range(l[2][1][1])]
)
)
fl = True
if not fl:
print(-1) | IMPORT ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR LIST STRING VAR STRING VAR STRING VAR IF VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | x1, y1, x2, y2, x3, y3 = list(map(int, input().split()))
def solve(x1, y1, x2, y2, x3, y3):
n = max(x1, y1, x2, y2, x3, y3)
sum_area = x1 * y1 + x2 * y2 + x3 * y3
if sum_area != n * n:
return -1, None
if x1 < y1:
x1, y1 = y1, x1
if x2 < y2:
x2, y2 = y2, x2
if x3 < y3:
x3, y3 = y3, x3
if x1 == x2 == x3:
bil = [("A" * n) for i in range(y1)]
bil.extend([("B" * n) for i in range(y2)])
bil.extend([("C" * n) for i in range(y3)])
return n, bil
xys = [(x1, y1, "A"), (x2, y2, "B"), (x3, y3, "C")]
xys.sort(reverse=True)
x1, y1, C1 = xys[0]
x2, y2, C2 = xys[1]
x3, y3, C3 = xys[2]
if x2 == x1:
return -1, None
m = n - y1
if (x2 == m or y2 == m) and (x3 == m or y3 == m):
bil = [(C1 * n) for i in range(y1)]
w2 = x2 + y2 - m
w3 = x3 + y3 - m
bil.extend([(C2 * w2 + C3 * w3) for i in range(m)])
return n, bil
return -1, None
n, pattern = solve(x1, y1, x2, y2, x3, y3)
if n == -1:
print(-1)
else:
print(n)
for row in pattern:
print(row) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER NONE IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR LIST VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER NONE ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN NUMBER NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | from itertools import permutations
def stripe(a, b, c):
ans = [str(a[0])]
ans.append("\n".join(a[2] * a[0] for _ in range(a[1])))
ans.append("\n".join(b[2] * b[0] for _ in range(b[1])))
ans.append("\n".join(c[2] * c[0] for _ in range(c[1])))
return "\n".join(ans)
def tt(a):
return a[1], a[0], a[2]
def one_two(a, b, c):
ans = [str(a[0])]
ans.append("\n".join(a[2] * a[0] for _ in range(a[1])))
for _ in range(b[1]):
ans.append(b[2] * b[0] + c[2] * c[0])
return "\n".join(ans)
def solve():
x1, y1, x2, y2, x3, y3 = map(int, input().split())
a = x1, y1, "A"
b = x2, y2, "B"
c = x3, y3, "C"
from itertools import permutations
for a, b, c in permutations((a, b, c)):
for a, b, c in (
(a, b, c),
(tt(a), b, c),
(a, tt(b), c),
(a, b, tt(c)),
(tt(a), tt(b), c),
(tt(a), b, tt(c)),
(a, tt(b), tt(c)),
(tt(a), tt(b), tt(c)),
):
if a[0] == a[1] + b[1] + c[1]:
return stripe(a, b, c)
if a[0] == b[0] + c[0] and b[1] == c[1] and a[0] == a[1] + b[1]:
return one_two(a, b, c)
return -1
print(solve()) | FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | x1, y1, x2, y2, x3, y3 = map(int, input().split())
x1, y1 = max(x1, y1), min(x1, y1)
x2, y2 = max(x2, y2), min(x2, y2)
x3, y3 = max(x3, y3), min(x3, y3)
x1c, y1c, x2c, y2c, x3c, y3c = x1, y1, x2, y2, x3, y3
if x2 == max(x1, x2, x3):
x1, y1, x2, y2 = x2, y2, x1, y1
elif x3 == max(x1, x2, x3):
x1, y1, x3, y3 = x3, y3, x1, y1
if x1 == x1c and y1 == y1c and x2 == x2c and y2 == y2c:
s1 = "A"
s2 = "B"
s3 = "C"
elif x1 == x1c and y1 == y1c and x2 == x3c and y2 == y3c:
s1 = "A"
s2 = "C"
s3 = "B"
elif x1 == x2c and y1 == y2c and x2 == x1c and y2 == y1c:
s1 = "B"
s2 = "A"
s3 = "C"
elif x1 == x2c and y1 == y2c and x2 == x3c and y2 == y3c:
s1 = "B"
s2 = "C"
s3 = "A"
elif x1 == x3c and y1 == y3c and x2 == x1c and y2 == y1c:
s1 = "C"
s2 = "A"
s3 = "B"
elif x1 == x3c and y1 == y3c and x2 == x2c and y2 == y2c:
s1 = "C"
s2 = "B"
s3 = "A"
if x1 == x2 == x3 and y1 + y2 + y3 == x1:
print(x1)
for i in range(x1):
for j in range(x1):
if i < y1:
print(s1, end="")
elif y1 <= i < y1 + y2:
print(s2, end="")
else:
print(s3, end="")
print()
elif x1 == y2 + x3 and x2 + y1 == y3 + y1 == x1:
print(x1)
for i in range(x1):
for j in range(x1):
if i < y1:
print(s1, end="")
elif j < y2:
print(s2, end="")
else:
print(s3, end="")
print()
elif x1 == x2 + y3 and y2 + y1 == x3 + y1 == x1:
print(x1)
for i in range(x1):
for j in range(x1):
if i < y1:
print(s1, end="")
elif j < x2:
print(s2, end="")
else:
print(s3, end="")
print()
elif x1 == x2 + x3 and y2 + y1 == y3 + y1 == x1:
print(x1)
for i in range(x1):
for j in range(x1):
if i < y1:
print(s1, end="")
elif j < x2:
print(s2, end="")
else:
print(s3, end="")
print()
elif x1 == y2 + y3 and x2 + y1 == x3 + y1 == x1:
print(x1)
for i in range(x1):
for j in range(x1):
if i < y1:
print(s1, end="")
elif j < y2:
print(s2, end="")
else:
print(s3, end="")
print()
else:
print(-1) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | __author__ = "Admin"
def f(n):
return max(n[0], n[1])
t = True
x1, y1, x2, y2, x3, y3 = map(int, input().split())
m = [x1, y1, x2, y2, x3, y3]
m1 = [[x1, y1, "A"], [x2, y2, "B"], [x3, y3, "C"]]
m1.sort(key=f)
maxi = max(m1[-1][0], m1[-1][1])
mini = min(m1[-1][0], m1[-1][1])
maxj = max(m1[-2][1], m1[-2][0])
minj = min(m1[-2][1], m1[-2][0])
maxk = max(m1[0][1], m1[0][0])
mink = min(m1[0][1], m1[0][0])
s = m1[-1][2]
s1 = m1[-2][2]
s2 = m1[0][2]
matr = [([0] * maxi) for i in range(maxi)]
for i in range(mini):
for j in range(maxi):
matr[i][j] = s
if maxj == maxi and mini + minj <= maxi:
for i in range(mini, minj + mini):
for j in range(maxj):
matr[i][j] = s1
if maxk == maxi and mini + minj + mink == maxi:
for i in range(minj + mini, mink + minj + mini):
for j in range(maxk):
matr[i][j] = s2
else:
t = False
elif maxj == maxi - mini:
for i in range(mini, mini + maxj):
for j in range(minj):
matr[i][j] = s1
if maxk == maxj and mink == maxi - minj:
for i in range(mini, mini + maxk):
for j in range(minj, minj + mink):
matr[i][j] = s2
else:
t = False
elif minj == maxi - mini:
for i in range(mini, mini + minj):
for j in range(maxj):
matr[i][j] = s1
if mink == minj and maxk == maxi - maxj:
for i in range(mini, mini + mink):
for j in range(maxj, maxj + maxk):
matr[i][j] = s2
elif maxk == minj and mink == maxi - maxj:
for i in range(mini, mini + maxk):
for j in range(maxj, maxj + mink):
matr[i][j] = s2
else:
t = False
else:
t = False
if t == True:
print(maxi)
for i in range(maxi):
print(*matr[i], sep="")
else:
print(-1) | ASSIGN VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR STRING LIST VAR VAR STRING LIST VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | a, b, c, d, e, f = map(int, input().split())
if a < b:
a, b = b, a
if c < d:
c, d = d, c
if e < f:
e, f = f, e
sides = [[a, b, "A"], [c, d, "B"], [e, f, "C"]]
sides.sort(reverse=True)
c1, c2, c3 = sides[0][2], sides[1][2], sides[2][2]
area = a * b + c * d + e * f
if int(area**0.5) ** 2 != area:
print(-1)
else:
l = int(area**0.5)
if l not in sides[0]:
print(-1)
elif l in sides[1] and l in sides[2]:
print(l)
for i in range(3):
sides[i].remove(l)
for i in range(3):
for _ in range(sides[i][0]):
print([c1, c2, c3][i] * l)
else:
r = l - sides[0][1]
if r in sides[1] and r in sides[2]:
print(l)
for i in range(1, 3):
sides[i].remove(r)
for _ in range(sides[0][1]):
print(c1 * l)
for _ in range(r):
print(c2 * sides[1][0] + c3 * sides[2][0])
else:
print(-1) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR STRING LIST VAR VAR STRING LIST VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import time
def flag(a, b, c, d, e, f, X, Y, Z):
if b == d and d == f and a + c + e == b:
m = [[(0) for i in range(b)] for i in range(b)]
for i in range(a):
for j in range(b):
m[i][j] = X
for i in range(a, a + c):
for j in range(b):
m[i][j] = Y
for i in range(a + c, a + c + e):
for j in range(b):
m[i][j] = Z
return b, m
return False
def frame(a, b, c, d, e, f, X, Y, Z):
if a == c + e and a == b + d and d == f:
m = [[(0) for i in range(a)] for i in range(a)]
for i in range(a):
for j in range(b):
m[i][j] = X
for i in range(c):
for j in range(b, a):
m[i][j] = Y
for i in range(c, a):
for j in range(b, a):
m[i][j] = Z
return a, m
return False
def test0(a, b, c, d, e, f, X, Y, Z):
ans = flag(a, b, c, d, e, f, X, Y, Z)
if ans == False:
ans = frame(a, b, c, d, e, f, X, Y, Z)
return ans
def test(a, b, c, d, e, f, X, Y, Z):
ans = test0(a, b, c, d, e, f, X, Y, Z)
if ans == False:
ans = test0(a, b, c, d, f, e, X, Y, Z)
if ans == False:
ans = test0(a, b, d, c, e, f, X, Y, Z)
if ans == False:
ans = test0(b, a, c, d, e, f, X, Y, Z)
if ans == False:
ans = test0(a, b, d, c, f, e, X, Y, Z)
if ans == False:
ans = test0(b, a, c, d, f, e, X, Y, Z)
if ans == False:
ans = test0(b, a, d, c, e, f, X, Y, Z)
if ans == False:
ans = test0(b, a, d, c, f, e, X, Y, Z)
return ans
a, b, c, d, e, f = (int(i) for i in input().split())
start = time.time()
ans = test(a, b, c, d, e, f, "A", "B", "C")
if ans == False:
ans = test(c, d, a, b, e, f, "B", "A", "C")
if ans == False:
ans = test(e, f, a, b, c, d, "C", "A", "B")
if ans == False:
print(-1)
else:
print(ans[0])
for i in range(ans[0]):
for j in range(ans[0]):
print(ans[1][i][j], end="")
print()
finish = time.time() | IMPORT FUNC_DEF IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING STRING STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | from itertools import permutations
def isqrt(x):
l, r = 0, 10**100
while l < r - 1:
mid = (l + r) // 2
if mid**2 > x:
r = mid
else:
l = mid
return l
x1, y1, x2, y2, x3, y3 = map(int, input().split())
p = [(x1, y1, "A"), (x2, y2, "B"), (x3, y3, "C")]
s = x1 * y1 + x2 * y2 + x3 * y3
x = isqrt(s)
if x * x != s:
print(-1)
exit(0)
for mask in range(1 << 3):
q = []
for i in range(3):
if mask >> i & 1:
q.append((p[i][0], p[i][1], p[i][2]))
else:
q.append((p[i][1], p[i][0], p[i][2]))
if q[0][0] == q[1][0] == q[2][0] == x:
print(x)
for i in range(q[0][1]):
print("A" * x)
for i in range(q[1][1]):
print("B" * x)
for i in range(q[2][1]):
print("C" * x)
exit(0)
for r in permutations(q):
t = list(r)
if (
t[0][0] == x
and t[0][1] + t[1][1] == x
and t[0][1] + t[2][1] == x
and t[1][0] + t[2][0] == x
):
print(x)
for i in range(t[0][1]):
print(t[0][2] * x)
for i in range(x - t[0][1]):
print(t[1][2] * t[1][0] + t[2][2] * t[2][0])
exit(0)
print(-1) | FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def chnge(last, cap, ini=(0, 0)):
for i in range(ini[1], last[1]):
fin[i][ini[0] : last[0]] = [cap] * (last[0] - ini[0])
x1, y1, x2, y2, x3, y3 = map(int, input().split())
a = max(x1, y1), [x1, y1], "A"
b = max(x2, y2), [x2, y2], "B"
c = max(x3, y3), [x3, y3], "C"
m = max(a[0], b[0], c[0])
fin = [["*" for i in range(m)] for j in range(m)]
if x1 * y1 + x2 * y2 + x3 * y3 != m**2:
print(-1)
else:
l = sorted([a] + [b] + [c], reverse=True)
l[0][1].sort(reverse=True)
chnge(l[0][1], l[0][2])
ini = [0, l[0][1][1]]
last = l[1][1]
if (
m in [ini[0] + last[0], ini[1] + last[1]]
and ini[0] + last[0] + ini[1] + last[1] <= 2 * m
):
last = [ini[0] + last[0], ini[1] + last[1]]
else:
last = [ini[0] + last[1], ini[1] + last[0]]
chnge(last, l[1][2], ini)
chr = l[2][2]
print(m)
for i in fin:
print("".join(i).replace("*", chr)) | FUNC_DEF NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER BIN_OP LIST VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR LIST VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR STRING VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | x1, y1, x2, y2, x3, y3 = map(int, input().split())
rect1 = [x1, y1]
rect2 = [x2, y2]
rect3 = [x3, y3]
def func():
rect11 = [x1, y1]
rect22 = [x2, y2]
rect33 = [x3, y3]
rect1 = [x1, y1]
rect2 = [x2, y2]
rect3 = [x3, y3]
recta = [x1, y1]
rectb = [x2, y2]
rectc = [x3, y3]
for i in rect11:
for ii in rect22:
for iii in rect33:
if i == ii:
rect1.remove(i)
rect2.remove(ii)
if rect1[0] + rect2[0] == iii:
rect3.remove(iii)
if i + rect3[0] == iii:
print(iii)
for j in range(iii):
if j < rect1[0]:
print("C" * rect3[0] + "A" * i)
else:
print("C" * rect3[0] + "B" * ii)
exit()
rect1 = recta.copy()
rect2 = rectb.copy()
rect3 = rectc.copy()
if i == iii:
rect1.remove(i)
rect3.remove(iii)
if rect1[0] + rect3[0] == ii:
rect2.remove(ii)
if i + rect2[0] == ii:
print(ii)
for j in range(ii):
if j < rect1[0]:
print("B" * rect2[0] + "A" * i)
else:
print("B" * rect2[0] + "C" * iii)
exit()
rect1 = recta.copy()
rect2 = rectb.copy()
rect3 = rectc.copy()
if ii == iii:
rect2.remove(ii)
rect3.remove(iii)
if rect2[0] + rect3[0] == i:
rect1.remove(i)
if i == rect1[0] + ii:
print(i)
for j in range(i):
if j < rect2[0]:
print("A" * rect1[0] + "B" * ii)
else:
print("A" * rect1[0] + "C" * iii)
exit()
rect1 = recta.copy()
rect2 = rectb.copy()
rect3 = rectc.copy()
return print(-1)
for i in rect1:
for ii in rect2:
for iii in rect3:
recta = [x1, y1]
rectb = [x2, y2]
rectc = [x3, y3]
if i == ii == iii:
rect1.remove(i)
rect2.remove(i)
rect3.remove(i)
if rect1[0] + rect2[0] + rect3[0] == i:
print(i)
for j in range(i):
print("A" * rect1[0] + "B" * rect2[0] + "C" * rect3[0])
exit()
rect1 = recta
rect2 = rectb
rect3 = rectc
func() | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import sys
x1, y1, x2, y2, x3, y3 = map(int, input().split())
a = x1, y1
b = x2, y2
c = x3, y3
for i in range(2):
for j in range(2):
for k in range(2):
if a[i] == b[j] == c[k] and a[1 - i] + b[1 - j] + c[1 - k] == a[i]:
print(a[i])
print(("A" * a[i] + "\n") * a[1 - i], end="")
print(("B" * b[j] + "\n") * b[1 - j], end="")
print(("C" * c[k] + "\n") * c[1 - k], end="")
sys.exit(0)
r = max(max(a), max(b), max(c))
xs = [a, b, c]
for i, t in enumerate(xs):
if r == max(t):
index = i
break
o = xs[index][:]
s = o[1] if o[0] == r else o[0]
del xs[index]
for i in range(2):
for j in range(2):
if xs[0][i] == xs[1][j] == r - s and xs[0][1 - i] + xs[1][1 - j] == r:
print(r)
symb = chr(ord("A") + index)
print((symb * r + "\n") * s, end="")
syms = ["A", "B", "C"]
syms.remove(symb)
print(
(syms[0] * xs[0][1 - i] + syms[1] * xs[1][1 - j] + "\n") * (r - s),
end="",
)
sys.exit(0)
print(-1) | IMPORT ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR STRING ASSIGN VAR LIST STRING STRING STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR STRING BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | from sys import *
inp = lambda: stdin.readline()
def main():
x1, y1, x2, y2, x3, y3 = map(int, inp().split())
m = max(x1, x2, x3, y1, y2, y3)
l = [(x1, y1), (x2, y2), (x3, y3)]
ans = [["C" for i in range(m)] for i in range(m)]
bools = True
if all([(m in x) for x in l]):
if min(x1, y1) + min(x2, y2) + min(x3, y3) != m:
bools = False
else:
for i in range(0, min(x1, y1)):
ans[i] = ["A" for i in range(m)]
for i in range(min(x1, y1), min(x1, y1) + min(x2, y2)):
ans[i] = ["B" for i in range(m)]
elif x2 * y2 + x3 * y3 + x1 * y1 != m * m:
bools = False
elif m in l[0]:
if not all([(m - min(x1, y1) in i) for i in [l[1], l[2]]]):
bools = False
else:
for i in range(0, min(x1, y1)):
ans[i] = ["A" for i in range(m)]
if min(x2, y2) + min(x1, y1) == m:
for i in range(min(x1, y1), m):
ans[i] = ["B" for i in range(max(x2, y2))] + [
"C" for i in range(m - max(x2, y2))
]
else:
for i in range(min(x1, y1), m):
ans[i] = ["B" for i in range(min(x2, y2))] + [
"C" for i in range(m - min(x2, y2))
]
elif m in l[1]:
if not all([(m - min(x2, y2) in i) for i in [l[0], l[2]]]):
bools = False
else:
x1, x2 = x2, x1
y1, y2 = y2, y1
for i in range(0, min(x1, y1)):
ans[i] = ["B" for i in range(m)]
if min(x2, y2) + min(x1, y1) == m:
for i in range(min(x1, y1), m):
ans[i] = ["A" for i in range(max(x2, y2))] + [
"C" for i in range(m - max(x2, y2))
]
else:
for i in range(min(x1, y1), m):
ans[i] = ["A" for i in range(min(x2, y2))] + [
"C" for i in range(m - min(x2, y2))
]
elif m in l[2]:
if not all([(m - min(x3, y3) in i) for i in [l[1], l[0]]]):
bools = False
else:
x1, x3 = x3, x1
y1, y3 = y3, y2
for i in range(0, min(x1, y1)):
ans[i] = ["C" for i in range(m)]
if min(x2, y2) + min(x1, y1) == m:
for i in range(min(x1, y1), m):
ans[i] = ["B" for i in range(max(x2, y2))] + [
"A" for i in range(m - max(x2, y2))
]
else:
for i in range(min(x1, y1), m):
ans[i] = ["B" for i in range(min(x2, y2))] + [
"A" for i in range(m - min(x2, y2))
]
if bools:
print(m)
for i in ans:
print("".join(map(str, i)))
else:
print(-1)
main() | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | ii = lambda: int(input())
kk = lambda: map(int, input().split())
ll = lambda: list(kk())
a, b, c, d, e, f = kk()
al = [a, b, c, d, e, f]
s = sum(al)
area = a * b + c * d + e * f
side = int(area**0.5)
if side**2 != area or side not in al:
print(-1)
exit()
if al.count(side) == 3:
if s == 4 * side:
rest = [a for a in al if a != side]
print(side)
for _ in range(side):
print("".join(["A" * rest[0], "B" * rest[1], "C" * rest[2]]))
elif al.count(side) > 1:
print(-1)
else:
x = al.index(side)
y = x ^ 1
res = al[y]
a, b = min(x, y), max(x, y)
s1 = "ABC"[a // 2]
s23 = [s for s in "ABC" if s != s1]
rest = al[:a] + al[b + 1 :]
res = side - res
a, b = [rest[0], rest[1]], [rest[2], rest[3]]
if not (res in a and res in b):
print(-1)
exit()
o1, o2 = a[a.index(res) ^ 1], b[b.index(res) ^ 1]
print(side)
for _ in range(al[y]):
print(s1 * side)
for _ in range(res):
print("".join([s23[0] * o1, s23[1] * o2])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST VAR NUMBER VAR NUMBER LIST VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | x1, y1, x2, y2, x3, y3 = [int(s) for s in input().split()]
def print_logos(top, bottom, h1, h2):
top_line = ""
if "a" in top:
top_line += (x1 if top["a"] else y1) * "A"
if "b" in top:
top_line += (x2 if top["b"] else y2) * "B"
if "c" in top:
top_line += (x3 if top["c"] else y3) * "C"
print(len(top_line))
for i in range(h1):
print(top_line)
bottom_line = ""
if "a" in bottom:
bottom_line += (x1 if bottom["a"] else y1) * "A"
if "b" in bottom:
bottom_line += (x2 if bottom["b"] else y2) * "B"
if "c" in bottom:
bottom_line += (x3 if bottom["c"] else y3) * "C"
for i in range(h2):
print(bottom_line)
done = False
for a in [True, False]:
if done:
break
for b in [True, False]:
if done:
break
for c in [True, False]:
width = (x1 if a else y1) + (x2 if b else y2) + (x3 if c else y3)
if (
width == (y1 if a else x1)
and width == (y2 if b else x2)
and width == (y3 if c else x3)
):
print_logos({"a": a, "b": b, "c": c}, {}, y1 if a else x1, 0)
done = True
break
width = (x1 if a else y1) + (x2 if b else y2)
if (
width == (x3 if c else y3)
and (y1 if a else x1) == (y2 if b else x2)
and (y1 if a else x1) + (y3 if c else x3) == width
):
print_logos(
{"a": a, "b": b}, {"c": c}, y1 if a else x1, y3 if c else x3
)
done = True
break
width = (x1 if a else y1) + (x3 if c else y3)
if (
width == (x2 if b else y2)
and (y1 if a else x1) == (y3 if c else x3)
and (y1 if a else x1) + (y2 if b else x2) == width
):
print_logos(
{"a": a, "c": c}, {"b": b}, y1 if a else x1, y2 if b else x2
)
done = True
break
width = (x3 if c else y3) + (x2 if b else y2)
if (
width == (x1 if a else y1)
and (y3 if c else x3) == (y2 if b else x2)
and (y1 if a else x1) + (y3 if c else x3) == width
):
print_logos(
{"b": b, "c": c}, {"a": a}, y2 if b else x2, y1 if a else x1
)
done = True
break
if not done:
print(-1) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING IF STRING VAR VAR BIN_OP VAR STRING VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER IF VAR FOR VAR LIST NUMBER NUMBER IF VAR FOR VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING STRING VAR VAR VAR DICT VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR DICT STRING VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR DICT STRING VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR DICT STRING VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | inp = input().split()
a = [int(inp[0]), int(inp[1])]
b = [int(inp[2]), int(inp[3])]
c = [int(inp[4]), int(inp[5])]
a.sort()
a.reverse()
a.append("A")
b.sort()
b.reverse()
b.append("B")
c.sort()
c.reverse()
c.append("C")
first = [a, b, c]
first.sort()
first.reverse()
second = [a, b, c]
second.sort()
second.reverse()
third = [a, b, c]
def swap(a):
temp = a[0]
a[0] = a[1]
a[1] = temp
def check_first(x):
fla = x[0][0] == x[1][0] + x[2][0]
flb = x[1][1] == x[2][1] == x[0][0] - x[0][1]
return fla and flb
def check_second(x):
if x[0][0] == x[1][0] and x[0][1] + x[1][1] == x[2][0] == x[0][0]:
return True
else:
return False
flag = False
ind = 0
res = -1
s = ""
while not flag:
ind = 1
if check_first(first):
break
swap(first[1])
if check_first(first):
break
swap(first[2])
if check_first(first):
break
swap(first[1])
if check_first(first):
break
ind = 2
if check_second(second):
break
swap(second[2])
if check_second(second):
break
if (
third[0][0] == third[1][0] == third[2][0]
and third[0][0] == third[0][1] + third[1][1] + third[2][1]
):
ind = 3
break
flag = True
if flag:
print(-1)
elif ind == 1:
print(first[0][0])
for i in range(first[0][1]):
print(first[0][2] * first[0][0])
for i in range(first[1][1]):
print(first[1][2] * first[1][0] + first[2][2] * first[2][0])
elif ind == 2:
print(second[2][1])
for i in range(second[0][1]):
print(second[0][2] * second[0][0] + second[2][2] * second[2][0])
for i in range(second[1][1]):
print(second[1][2] * second[1][0] + second[2][2] * second[2][0])
elif ind == 3:
print(third[0][0])
for i in range(third[0][1]):
print(third[0][2] * third[0][0])
for i in range(third[1][1]):
print(third[1][2] * third[0][0])
for i in range(third[2][1]):
print(third[2][2] * third[0][0]) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | v = list(map(int, input().split()))
def funk(a):
if a[0] == a[2] + a[4] and a[1] + a[3] == a[0] and a[3] == a[5]:
print(a[0])
for i in range(a[1]):
print("A" * a[0])
for i in range(a[3]):
print("B" * a[2] + "C" * a[4])
elif a[0] == a[2] == a[4] and a[1] + a[3] + a[5] == a[0]:
print(a[0])
for i in range(a[1]):
print("A" * a[0])
for i in range(a[3]):
print("B" * a[2])
for i in range(a[5]):
print("C" * a[4])
elif a[0] + a[2] + a[4] == a[1] and a[1] == a[3] == a[5]:
print(a[1])
for i in range(a[1]):
print("A" * a[0] + "B" * a[2] + "C" * a[4])
elif a[0] + a[2] == a[4] and a[1] == a[3] and a[1] + a[5] == a[4]:
print(a[4])
for i in range(a[1]):
print("A" * a[0] + "B" * a[2])
for i in range(a[5]):
print("C" * a[4])
elif a[0] + a[2] == a[1] and a[2] == a[4] and a[3] + a[5] == a[1]:
print(a[1])
for i in range(a[3]):
print("A" * a[0] + "B" * a[2])
for i in range(a[5]):
print("A" * a[0] + "C" * a[4])
elif a[0] + a[2] == a[3] and a[0] == a[4] and a[1] + a[5] == a[3]:
print(a[3])
for i in range(a[1]):
print("A" * a[0] + "B" * a[2])
for i in range(a[5]):
print("C" * a[4] + "B" * a[2])
else:
return 0
return 1
s = 0
for i in range(2):
v[0], v[1] = v[1], v[0]
for i1 in range(2):
v[2], v[3] = v[3], v[2]
for i2 in range(2):
v[4], v[5] = v[5], v[4]
if s == 0:
s = funk(v)
if s:
break
if s == 0:
print("-1") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP STRING VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | import itertools
import sys
def can(h1, w1, h2, w2, h3, w3, c1, c2, c3, tot):
if tot % h1 == 0 and tot // h1 == h1:
if h1 == h2 == h3:
print(h1)
for r in range(h1):
temp = ""
for c in range(w1 + w2 + w3):
if c < w1:
temp += c1
elif c < w1 + w2:
temp += c2
else:
temp += c3
print(temp)
return True
if h2 + h3 == h1 and w2 == w3:
print(h1)
for r in range(h1):
temp = ""
for c in range(w1 + w2):
if c < w1:
temp += c1
elif r < h2:
temp += c2
else:
temp += c3
print(temp)
return True
return False
def solve(perm):
x1, y1 = perm[0][0][0], perm[0][0][1]
x2, y2 = perm[1][0][0], perm[1][0][1]
x3, y3 = perm[2][0][0], perm[2][0][1]
c1 = perm[0][1]
c2 = perm[1][1]
c3 = perm[2][1]
tot = x1 * y1 + x2 * y2 + x3 * y3
for sw1 in range(2):
for sw2 in range(2):
for sw3 in range(2):
h1, w1, h2, w2, h3, w3 = x1, y1, x2, y2, x3, y3
if sw1 == 1:
h1, w1 = w1, h1
if sw2 == 1:
h2, w2 = w2, h2
if sw3 == 1:
h3, w3 = w3, h3
if can(h1, w1, h2, w2, h3, w3, c1, c2, c3, tot):
return True
def supersolve():
x1, y1, x2, y2, x3, y3 = rv()
a = [([x1, y1], "A"), ([x2, y2], "B"), ([x3, y3], "C")]
for perm in itertools.permutations(a):
if solve(perm):
return
print(-1)
def prt(l):
return print(" ".join(map(str, l)))
def rs():
return map(str, input().split())
def rv():
return map(int, input().split())
def rl(n):
return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544:
sys.stdin = open("test.txt")
supersolve() | IMPORT IMPORT FUNC_DEF IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR VAR STRING LIST VAR VAR STRING LIST VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | w1, h1, w2, h2, w3, h3 = map(int, input().split())
r1, r2, r3 = sorted((w1, h1)), sorted((w2, h2)), sorted((w3, h3))
for i in range(3):
ra, ca = (r1, r2, r3)[i], "ABC"[i]
rb, cb = (r1, r2, r3)[(i + 1) % 3], "ABC"[(i + 1) % 3]
rc, cc = (r1, r2, r3)[(i + 2) % 3], "ABC"[(i + 2) % 3]
if ra[0] == ra[1]:
continue
w, h = ra[1] - ra[0], ra[1]
rs = []
wtot, whit, ws = 0, 0, []
htot, hhit, hs = 0, 0, []
for r in (rb, rc):
rs.append(r)
if r[0] != r[1]:
rs.append((r[1], r[0]))
for r in rs:
if r[0] == w:
wtot += r[1]
whit += 1
ws.append(r[1])
if r[0] == h:
htot += r[1]
hhit += 1
hs.append(r[1])
if whit == 2 and wtot == h:
n = ra[1]
xb = rb[0] if rb[1] == w else rb[1]
xc = rc[0] if rc[1] == w else rc[1]
print(n)
print((ca * n + "\n") * ra[0], end="")
print((cb * xb + cc * xc + "\n") * w, end="")
break
if hhit == 2 and htot == w:
n = ra[1]
xb = rb[0] if rb[1] == h else rb[1]
xc = rc[0] if rc[1] == h else rc[1]
print(n)
print((ca * n + "\n") * ra[0], end="")
print((cb * n + "\n") * xb, end="")
print((cc * n + "\n") * xc, end="")
break
else:
print(-1) | ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR STRING VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER LIST ASSIGN VAR VAR VAR NUMBER NUMBER LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR STRING IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR NUMBER |
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
-----Input-----
The first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, y_1, x_2, y_2, x_3, y_3 ≤ 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.
-----Output-----
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
-----Examples-----
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC | def possible(arr):
a, b, c, d, e, f = arr
if a == c == e and b + d + f == a:
one = "A" * b + "B" * d + "C" * f
print(a)
for i in range(a):
print(one)
return True
if b == d == f and a + c + e == d:
print(b)
for i in range(a):
print("A" * b)
for i in range(c):
print("B" * b)
for i in range(e):
print("C" * b)
return True
ns = [(a, b, "A"), (c, d, "B"), (e, f, "C")]
fs = [(b, a, "A"), (d, c, "B"), (f, e, "C")]
ns.sort(reverse=True)
x, y, z = ns
a, b, t1 = x
c, d, t2 = y
e, f, t3 = z
if c + e == a and d == f and d + b == a:
print(a)
mat = [["." for i in range(a)] for j in range(a)]
for i in range(a):
for j in range(b):
mat[i][j] = t1
for i in range(c):
for j in range(b, a):
mat[i][j] = t2
for i in range(c, a):
for j in range(b, a):
mat[i][j] = t3
for i in range(a):
print("".join(mat[i]))
return True
fs.sort(reverse=True)
x, y, z = fs
b, a, t1 = x
d, c, t2 = y
f, e, t3 = z
if d + f == b and c == e and c + a == b:
print(b)
mat = [["." for i in range(b)] for j in range(b)]
for i in range(a):
for j in range(b):
mat[i][j] = t1
for i in range(a, b):
for j in range(d):
mat[i][j] = t2
for i in range(a, b):
for j in range(d, b):
mat[i][j] = t3
for i in range(b):
print("".join(mat[i]))
return True
return False
arr = [int(x) for x in input().split()]
cnt = 0
ok = False
for i in range(8):
send = [x for x in arr]
if i & 1:
send[0], send[1] = send[1], send[0]
if i & 2:
send[2], send[3] = send[3], send[2]
if i & 4:
send[4], send[5] = send[5], send[4]
if possible(send):
ok = True
break
if not ok:
print(-1) | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN NUMBER ASSIGN VAR LIST VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR LIST VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | import sys
I = sys.stdin.readline
def rails(x):
z, ret = 1, set()
while x:
if x & 1:
ret.add(z)
z += 1
x //= 2
return ret
n = int(I())
a = map(int, I().split())
a = [i for i in a if i > 0]
n = len(a)
b, e = [[] for i in range(n)], {}
for i, x in enumerate(a):
rs = rails(x)
for r in rs:
if not r in e:
e[r] = []
e[r].append(i)
if len(e[r]) > 2:
print(3)
sys.exit(0)
def bfs(i, j, k):
dist = 1
v = [0] * n
v[i] = 1
v[j] = 1
q, nq = [], []
q.append(j)
while len(q):
while len(q):
x = q.pop()
for y in b[x]:
if v[y] == 0:
v[y] = 1
nq.append(y)
if y == k:
return dist
q, nq = nq, q
dist += 1
return n
def find(i):
ret = n
for j, jj in enumerate(b[i]):
for k, kk in enumerate(b[i][j + 1 :]):
ret = min(ret, bfs(i, jj, kk))
return 2 + ret
for v in e.values():
if len(v) > 1:
b[v[0]].append(v[1])
b[v[1]].append(v[0])
ans = min(find(i) for i in range(n))
print(ans if ans <= n else -1) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR WHILE VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | def do():
n = int(input())
nums = [int(c) for c in input().split(" ")]
valid = set()
for i in range(64):
count = []
for j in range(n):
if 1 << i & nums[j]:
count.append(nums[j])
if len(count) == 3:
return 3
if len(count) == 2:
valid.add(count[0])
valid.add(count[1])
nv = len(valid)
valid = list(valid)
dis = [([float("inf")] * nv) for _ in range(nv)]
for i in range(nv):
for j in range(nv):
if i == j:
dis[i][j] = 0
elif valid[i] & valid[j]:
dis[i][j] = 1
mp = [[_ for _ in dis[i]] for i in range(len(dis))]
res = nv + 1
for k in range(nv):
for i in range(k):
for j in range(k):
if i != j:
res = min(res, dis[i][j] + mp[j][k] + mp[k][i])
for i in range(nv):
if k != i:
for j in range(nv):
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j])
return res if res <= nv else -1
print(do()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | import sys
n = int(input())
dat = [int(i) for i in input().split()]
data = [i for i in dat if i != 0]
n = len(data)
dic = {}
for d in data:
if d in dic:
dic[d] += 1
else:
dic[d] = 1
keys = list(dic.keys())
if len(keys) != len(data):
if len(data) > 200:
print(3)
sys.exit()
else:
data2 = []
for i in range(len(keys)):
k = keys[i]
if k != 0:
if dic[k] == 2:
for j in range(len(keys)):
if k & keys[j] and i != j:
print(3)
sys.exit()
elif dic[k] > 2:
print(3)
sys.exit()
else:
data2.append(k)
data = data2
n = len(data)
inf = 1 << 10
if n > 100:
print(3)
sys.exit()
def bfs(a):
layer = {a}
parent = {a: -1}
cnt = 0
while len(layer) > 0:
cnt += 1
new = set()
ret = -1
for l in layer:
for v in data:
if l & v and v != l and parent[l] != v:
if v in layer:
return 2 * cnt - 1
elif v in new:
ret = 2 * cnt
parent[v] = l
new.add(v)
layer = new
if ret != -1:
return ret
return inf
ans = inf
for d in data:
a2 = bfs(d)
if a2 == inf:
continue
elif a2 == 3:
print(3)
sys.exit()
elif a2 < ans:
ans = a2
if ans == inf:
print(-1)
else:
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | _ = int(input())
v = list(filter(lambda x: x > 0, list(map(int, input().split()))))
n = len(v)
if n > 128:
print(3)
exit(0)
ans = 10000000000.0
dis = [[(10000000000.0) for _ in range(n)] for __ in range(n)]
dp = [[(10000000000.0) for _ in range(n)] for __ in range(n)]
for i in range(n):
for j in range(n):
if i != j and v[i] & v[j]:
dp[i][j] = dis[i][j] = 1
for k in range(n):
for i in range(n):
for j in range(n):
if i != k and j != k and i != j:
ans = min(ans, dp[i][j] + dis[i][k] + dis[k][j])
for i in range(n):
for j in range(n):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
print([ans, -1][ans == 10000000000.0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | def solve(n, a):
B = 60
bits = [[] for i in range(B)]
for i in range(n):
for j in range(B):
if a[i] & 1 << j:
bits[j].append(i)
adj = [[] for i in range(n)]
for shared in bits:
if len(shared) >= 3:
return 3
elif len(shared) == 2:
u, v = shared
adj[u].append(v)
adj[v].append(u)
vis = [(False) for u in range(n)]
dist = [(-1) for u in range(n)]
def shortest_cycle(n, adj, s):
frontier = [(s, -1)]
dist[s] = 0
ptr = 0
while ptr < len(frontier):
u, p = frontier[ptr]
if not vis[u]:
vis[u] = True
for v in adj[u]:
if vis[v] and v != p:
ans = dist[u] + dist[v] + 1
for u, p in frontier:
vis[u] = False
dist[u] = -1
return ans
if dist[v] == -1:
dist[v] = dist[u] + 1
frontier.append((v, u))
ptr += 1
return 10**18
ans = min([shortest_cycle(n, adj, i) for i in range(n)])
if ans == 10**18:
ans = -1
return ans
print(solve(int(input()), list(map(int, input().split())))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | class node:
def __init__(self, v, edges):
self.value = v
self.edges = edges
n = int(input())
a = [int(i) for i in input().split(" ")]
def fun(a):
nodes = {}
for i in range(60):
k = 0
value = 1 << i
i_edges = set()
for j in a:
if value & j > 0:
k += 1
if k > 2:
del i_edges
del nodes
return 3
i_edges.add(j)
for j in i_edges:
if j not in nodes:
if i_edges.difference({j}) != 0:
nodes[j] = node(j, i_edges.difference({j}))
else:
nodes[j].edges = nodes[j].edges.union(i_edges.difference({j}))
return nodes
def find_short_path(v1, v2):
length = 0
v2 = {v2}
setic = set()
set_was = set()
while v1 not in setic:
del setic
setic = set()
for i in v2:
setic = setic.union(nodes[i].edges.difference(set_was))
set_was = set_was.union(setic)
if len(setic) == 0:
del v2
return 0
length += 1
del v2
v2 = setic.copy()
del set_was
del setic
return length
nodes = fun(a)
if type(nodes) == int:
print(3)
else:
mass = []
while len(nodes.keys()) != 0:
i = list(nodes.keys())[0]
if len(nodes[i].edges) != 0:
first_v = i
second_v = nodes[first_v].edges.pop()
nodes[second_v].edges.remove(first_v)
length = 0
if len(nodes[first_v].edges) == 0:
nodes.pop(first_v)
elif len(nodes[second_v].edges) == 0:
nodes.pop(second_v)
else:
length = find_short_path(first_v, second_v)
if length:
mass.append(length + 1)
else:
nodes.pop(i)
if len(mass) != 0:
print(min(mass))
else:
print(-1) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | def bfs(g, src, dest):
visited = {}
dist = {}
for i in g:
visited[i] = False
dist[i] = 1e18
dist[src] = 0
visited[src] = True
q = []
q.append(src)
while q:
src = q.pop(0)
for i in g[src]:
if visited[i] == False:
visited[i] = True
dist[i] = min(dist[i], dist[src] + 1)
q.append(i)
return dist[dest]
n = int(input())
a = list(map(int, input().split()))
c = 0
a = sorted(a)
while n != 0 and a[0] == 0:
a.pop(0)
n -= 1
for i in range(64):
c = 0
for j in range(n):
if a[j] >> i & 1:
c += 1
if c == 3:
break
if c == 3:
break
if c == 3:
print(3)
else:
g = {}
for i in range(64):
buff = [-1, -1]
for j in range(n):
if a[j] >> i & 1:
if buff[0] == -1:
buff[0] = j
elif buff[1] == -1:
buff[1] = j
if buff[0] not in g:
g[buff[0]] = []
g[buff[0]].append(buff[1])
if buff[1] not in g:
g[buff[1]] = []
g[buff[1]].append(buff[0])
tg = {}
dist = []
for i in g:
for j in g[i]:
tg = g
tg[i].remove(j)
tg[j].remove(i)
dist.append(bfs(tg, i, j))
while len(dist) != 0 and min(dist) < 2:
dist.remove(min(dist))
if len(dist) != 0 and min(dist) < 1e18:
print(min(dist) + 1)
else:
print(-1) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
L = max(A).bit_length()
ans = -1
graph = [[] for _ in range(N)]
V = []
for l in range(L):
P = []
for i in range(N):
if 1 << l & A[i]:
P.append(i)
if len(P) > 2:
ans = 3
break
elif len(P) == 2:
p0, p1 = P
graph[p0].append(p1)
graph[p1].append(p0)
V.append(P)
def bfs(s, g):
a = -1
q = [s]
checked = [False] * N
checked[s] = True
d = 0
while q:
qq = []
d += 1
for p in q:
for np in graph[p]:
if np == g:
if d == 1:
continue
else:
return d
if not checked[np]:
qq.append(np)
checked[np] = True
q = qq
return -1
if ans == 3:
print(3)
else:
ans = 10**9
for s, g in V:
cycle = bfs(s, g)
if cycle == -1:
continue
ans = min(cycle + 1, ans)
if ans == 10**9:
print(-1)
else:
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | import sys
n = int(input())
a = []
for x in input().split():
if int(x) != int(0):
a.append(int(x))
if len(a) > 500:
print(3)
return
n = len(a)
g = []
for i in range(n):
g.append([])
for i in range(n):
for j in range(n):
g[i].append(1000000000)
for i in range(n):
for j in range(n):
if i != j and a[i] & a[j] != 0:
g[i][j] = 1
ans = int(10000000000)
dis = []
for i in range(n):
dis.append([])
for i in range(n):
for j in range(n):
dis[i].append(g[i][j])
for k in range(n):
for i in range(k):
for j in range(i + 1, k):
ans = min(ans, dis[i][j] + g[i][k] + g[k][j])
for i in range(n):
for j in range(n):
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j])
if ans > 1000:
print(-1)
else:
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
s = [list() for i in range(0, 61)]
v = []
for _v in a:
if _v:
v.append(_v)
a = v
n = len(v)
if n > 128:
print(3)
exit()
for i in range(0, len(a)):
for j in range(0, 61):
if a[i] & 1 << j != 0:
s[j].append(a[i])
if len(s[j]) > 2:
print(3)
exit()
ans = 10000000000.0
dis = [[(10000000000.0) for _ in range(n)] for __ in range(n)]
dp = [[(10000000000.0) for _ in range(n)] for __ in range(n)]
for i in range(n):
for j in range(n):
if i != j and a[i] & a[j]:
dp[i][j] = dis[i][j] = 1
for k in range(n):
for i in range(k):
for j in range(i + 1, k):
ans = min(ans, dp[i][j] + dis[i][k] + dis[k][j])
for i in range(n):
for j in range(n):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
print([ans, -1][ans == 10000000000.0]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | def int2bin(n):
bit_cnt = ""
while n:
bit_cnt = str(n & 1) + bit_cnt
n >>= 1
return bit_cnt.zfill(70)
n = int(input())
if n <= 2:
print(-1)
exit(0)
A = list(map(int, input().split()))
B = []
for i in A:
B.append(int2bin(i))
graph = {i: set() for i in range(n + 55)}
starts = set()
ans = 10**10
for bit in range(69, -1, -1):
od = []
for i in range(n):
if B[i][bit] == "1":
od.append(i)
if len(od) >= 3:
print(3)
exit(0)
if len(od) == 2:
a, b = od
graph[a].add(b)
graph[b].add(a)
starts.add(a)
starts.add(b)
for v in starts:
used = [0] * (n + 55)
used[v] = 1
s1 = {v}
s2 = set()
c = 0
par = {}
while s1:
c += 1
for u in s1:
for j in graph[u]:
if used[j] == 0:
par[j] = u
s2.add(j)
used[j] = 1
elif c > 1 and j != v and par[u] != j:
if j in s1:
ans = min(ans, 2 * c - 1)
else:
ans = min(ans, 2 * c)
s1 = s2
s2 = set()
if ans == 10**10:
print(-1)
else:
print(ans) | FUNC_DEF ASSIGN VAR STRING WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example. | from sys import setrecursionlimit as SRL
from sys import stdin
SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
n = int(rd())
a = list(rrd())
b = []
for i in a:
if i:
b.append(i)
a = b[:]
n = len(a)
cnt = [0] * 100
edge = [[] for _i in range(n + 100)]
con = [[] for _j in range(100)]
for i, v in enumerate(a):
for j in range(70):
if v & 1 << j:
cnt[j] += 1
con[j].append(i)
for i in range(70):
if cnt[i] >= 3:
print(3)
exit(0)
for i in range(100):
for j in con[i]:
for k in con[i]:
if k not in edge[j]:
edge[j].append(k)
if j not in edge[k]:
edge[k].append(j)
ans = 1000000
def bfs(s):
ct = [1000000] * (n + 10)
q = [[s, 0, 0]]
ct[s] = 0
while len(q):
now = q[0]
q = q[1:]
for i in edge[now[0]]:
if i != now[0] and i != now[2] and i != s:
if ct[i] == 1000000:
ct[i] = now[1] + 1
q.append([i, ct[i], now[0]])
else:
global ans
ans = min(ans, ct[i] + now[1] + 1)
for i in range(n):
bfs(i)
if ans == 1000000:
print(-1)
else:
print(ans) | EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | def OP(i, j, op):
if op == "AND":
return i & j
if op == "OR":
return i | j
if op == "XOR":
return i ^ j
return 0
def totbit(i, test):
ans = 0
for j in range(0, len(ops)):
a = ops[j][0]
b = ops[j][1]
op = ops[j][2]
if a == "?":
x = test
elif a in M:
x = int(M[a][i])
else:
x = OL[OD[a]]
if b == "?":
y = test
elif b in M:
y = int(M[b][i])
else:
y = OL[OD[b]]
ans += OP(x, y, op)
OL[j] = OP(x, y, op)
return ans
ops = []
[n, m] = list(map(int, input().split()))
M = dict()
OL = []
OD = dict()
for i in range(0, n):
inp = input().split()
a = inp[0]
if len(inp) == 3:
b = inp[2]
M[a] = b
else:
a = inp[2]
b = inp[4]
op = inp[3]
OD[inp[0]] = len(OL)
OL.append(0)
ops.append([a, b, op])
mi = ""
ma = ""
for i in range(0, m):
b0 = totbit(i, 0)
b1 = totbit(i, 1)
if b0 >= b1:
ma += "0"
else:
ma += "1"
if b0 <= b1:
mi += "0"
else:
mi += "1"
print(mi)
print(ma) | FUNC_DEF IF VAR STRING RETURN BIN_OP VAR VAR IF VAR STRING RETURN BIN_OP VAR VAR IF VAR STRING RETURN BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | def sumBin(a, b, varMap):
lhs = varMap[a]
rhs = varMap[b]
return bin(int(lhs, 2) + int(rhs, 2))[2:]
def andBin(a, b, varMap):
lhs = varMap[a]
rhs = varMap[b]
return bin(int(lhs, 2) & int(rhs, 2))[2:]
def orBin(a, b, varMap):
lhs = varMap[a]
rhs = varMap[b]
return bin(int(lhs, 2) | int(rhs, 2))[2:]
def xorBin(a, b, varMap):
lhs = varMap[a]
rhs = varMap[b]
return bin(int(lhs, 2) ^ int(rhs, 2))[2:]
mapOper = {"AND": andBin, "OR": orBin, "XOR": xorBin}
n, m = list(map(int, input().split()))
minMap = {"?": "0", "": "0"}
maxMap = {"?": "1" * m, "": "0"}
minSum = "0"
maxSum = "0"
for _ in range(n):
name, _, expr = input().split(" ", 2)
if len(expr.split(" ")) == 1:
minMap[name] = expr
maxMap[name] = expr
else:
lhs, oper, rhs = expr.split()
minMap[name] = mapOper[oper](lhs, rhs, minMap).zfill(m)
maxMap[name] = mapOper[oper](lhs, rhs, maxMap).zfill(m)
minSum = sumBin("", name, minMap)
maxSum = sumBin("", name, maxMap)
def countOnes(i, varMap):
ones = 0
for name, num in list(varMap.items()):
if name != "?" and name != "":
ones += num[i] == "1"
return ones
minRes = ""
maxRes = ""
for i in range(m):
zeroOnes = countOnes(i, minMap)
oneOnes = countOnes(i, maxMap)
if zeroOnes > oneOnes:
maxRes += "0"
minRes += "1"
elif zeroOnes < oneOnes:
maxRes += "1"
minRes += "0"
else:
maxRes += "0"
minRes += "0"
print(minRes)
print(maxRes) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING BIN_OP STRING VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING VAR STRING VAR VAR VAR STRING RETURN VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | n, m = map(int, input().split())
vars = {}
def mxor(a, b):
if a == b:
return "0"
elif a == "0" and b == "1" or a == "1" and b == "0":
return "1"
elif a == "0" and b == "x" or a == "x" and b == "0":
return "x"
elif a == "0" and b == "!" or a == "!" and b == "0":
return "!"
elif a == "1" and b == "x" or a == "x" and b == "1":
return "!"
elif a == "1" and b == "!" or a == "!" and b == "1":
return "x"
elif a == "x" and b == "!" or a == "!" and b == "x":
return "1"
def mand(a, b):
if a == b:
return a
elif a == "0" or b == "0":
return "0"
elif a == "1" and b == "x" or a == "x" and b == "1":
return "x"
elif a == "1" and b == "!" or a == "!" and b == "1":
return "!"
elif a == "x" and b == "!" or a == "!" and b == "x":
return "0"
def mor(a, b):
if a == b:
return a
if a == "1" or b == "1":
return "1"
elif a == "0":
return b
elif b == "0":
return a
elif a == "x" and b == "!" or a == "!" and b == "x":
return "1"
def calc(a, op, b):
global m
global vars
a = ["x"] * m if a == "?" else vars[a]
b = ["x"] * m if b == "?" else vars[b]
if op == "XOR":
op = mxor
elif op == "AND":
op = mand
else:
op = mor
return "".join([op(x, y) for x, y in zip(a, b)])
for _ in range(n):
i = input().split()
if len(i) == 3:
vars[i[0]] = i[2]
else:
vars[i[0]] = calc(*i[2:])
r = [0] * m
for i in range(m):
for v in vars.values():
if v[i] == "x":
r[i] += 1
elif v[i] == "!":
r[i] -= 1
mmin = ["0"] * m
mmax = ["0"] * m
for i in range(m):
if r[i] < 0:
mmin[i] = "1"
for i in range(m):
if r[i] > 0:
mmax[i] = "1"
print("".join(mmin))
print("".join(mmax)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING RETURN STRING IF VAR STRING RETURN VAR IF VAR STRING RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING FUNC_DEF ASSIGN VAR VAR STRING BIN_OP LIST STRING VAR VAR VAR ASSIGN VAR VAR STRING BIN_OP LIST STRING VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | f = {"OR": lambda x, y: x | y, "AND": lambda x, y: x & y, "XOR": lambda x, y: x ^ y}
n, m = map(int, input().split())
p, u, v = [], [], []
l = {"?": n}
for i in range(n):
q, s = input().split(" := ")
if " " in s:
x, t, y = s.split()
p += [(l[x], f[t], l[y])]
u += [i]
else:
p += [int(s, 2)]
v += [i]
l[q] = i
s = [0] * (n + 1)
def g(k, l):
s[n] = k
for i in v:
s[i] = p[i] & l > 0
for i in u:
x, f, y = p[i]
s[i] = f(s[x], s[y])
return sum(s) - k
a = b = ""
for j in range(m):
l = 1 << m - j - 1
x, y = g(0, l), g(1, l)
a += "01"[y < x]
b += "01"[y > x]
print(a, b) | ASSIGN VAR DICT STRING STRING STRING BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR DICT STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF STRING VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR LIST FUNC_CALL VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR STRING VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | n, m = map(int, input().split())
v = [("?", "")]
temp = [(0, 1)]
d = {}
d["?"] = 0
mn, mx = "", ""
for i in range(n):
name, val = input().split(" := ")
v.append((name, val.split()))
temp.append((-1, -1))
d[name] = i + 1
def eval(expr, bit1, bit2):
if expr == "OR":
return bit1 | bit2
elif expr == "AND":
return bit1 and bit2
elif expr == "XOR":
return bit1 ^ bit2
else:
raise AttributeError()
for i in range(m):
for name, expr in v[1:]:
j = d[name]
if len(expr) == 1:
temp[j] = int(expr[0][i]), int(expr[0][i])
else:
bit1, bit2 = temp[d[expr[0]]], temp[d[expr[2]]]
temp[j] = eval(expr[1], bit1[0], bit2[0]), eval(expr[1], bit1[1], bit2[1])
z, o = sum(temp[_][0] for _ in range(1, n + 1)), sum(
temp[_][1] for _ in range(1, n + 1)
)
mn += "1" if o < z else "0"
mx += "1" if o > z else "0"
print(mn)
print(mx) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR STRING RETURN BIN_OP VAR VAR IF VAR STRING RETURN VAR VAR IF VAR STRING RETURN BIN_OP VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR STRING STRING VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
-----Input-----
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
-----Output-----
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
-----Note-----
In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | import sys
def calc(b0, b1, q):
if q == 0:
return b0 ^ b1
if q == 1:
return b0 | b1
if q == 2:
return b0 & b1
n, m = map(int, sys.stdin.readline().split())
arr1 = {}
opt = ["XOR", "OR", "AND"]
arr2 = []
for j in range(n):
a, b = list(map(str, sys.stdin.readline().split(" := ")))
b = b.split()
if len(b) == 1:
s = b[0]
arr1[a] = s
else:
c = b[0]
d = b[2]
q = opt.index(b[1])
arr2.append((a, c, d, q))
mins = ""
maxs = ""
d0 = {"?": 0}
d1 = {"?": 1}
for i in range(m):
for a, b in arr1.items():
d0[a] = int(b[i])
d1[a] = int(b[i])
s0 = 0
s1 = 0
for a, c, d, q in arr2:
b00 = d0[c]
b01 = d0[d]
b10 = d1[c]
b11 = d1[d]
c0 = calc(b00, b01, q)
c1 = calc(b10, b11, q)
s0 += 1 if c0 else 0
s1 += 1 if c1 else 0
d0[a] = c0
d1[a] = c1
if s1 < s0:
mins += "1"
else:
mins += "0"
if s1 > s0:
maxs += "1"
else:
maxs += "0"
sys.stdout.write("{0}\n{1}".format(mins, maxs)) | IMPORT FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING NUMBER ASSIGN VAR DICT STRING NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
l = 1e-06
def solve(c, m, p, v, k=1, prob=1.0):
s1 = 0
s2 = 0
if c > l:
prev_prob = c * prob
if c <= v:
n_c = 0.0
n_p = p + c
n_m = 0.0
if m > l:
n_m = m + c / 2
n_p = p + c / 2
s1 = solve(n_c, n_m, n_p, v, k + 1, prev_prob)
else:
n_c = c - v
n_p = p + v
n_m = 0.0
if m > l:
n_m = m + v / 2
n_p = p + v / 2
s1 = solve(n_c, n_m, n_p, v, k + 1, prev_prob)
if m > l:
prev_prob = m * prob
if m <= v:
n_m = 0.0
n_p = p + m
n_c = 0.0
if c > l:
n_c = c + m / 2
n_p = p + m / 2
s2 = solve(n_c, n_m, n_p, v, k + 1, prev_prob)
else:
n_m = m - v
n_p = p + v
n_c = 0.0
if c > l:
n_c = c + v / 2
n_p = p + v / 2
s2 = solve(n_c, n_m, n_p, v, k + 1, prev_prob)
return k * p * prob + s1 + s2
for _ in range(t):
c, m, p, v = map(float, input().split())
print(solve(c, m, p, v)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
input = sys.stdin.readline
scale = 40000
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
c, m, p, v = c * scale, m * scale, p * scale, v * scale
def dfs(c, m, p):
ans = 1
if c:
x = min(v, c)
if not m:
ans += c / scale * dfs(c - x, 0, p + x)
else:
ans += c / scale * dfs(c - x, m + x / 2, p + x / 2)
if m:
x = min(v, m)
if not c:
ans += m / scale * dfs(0, m - x, p + x)
else:
ans += m / scale * dfs(c + x / 2, m - x, p + x / 2)
return ans
print(dfs(c, m, p)) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def rec(races, idx, choices, v):
if idx == 2:
return races
ch = choices.copy()
reduce = min(ch[idx], v)
ch[idx] -= reduce
cn, ci = 0, 0
for i in range(3):
if i != idx and ch[i] > 0:
cn += 1
ci = i
ch[i] += reduce / 2
if ch[i] < 1e-05:
ch[i] = 0
if cn == 0:
return races
elif cn == 1:
ch[ci] += reduce / 2
res = 0
for i in range(3):
if ch[i] > 0:
res += ch[i] * rec(races + 1, i, ch, v)
return res
def solve(c, m, p, v):
choices = [c, m, p, v]
res = 0
for i in range(3):
res += choices[i] * rec(1, i, choices, v)
return res
T = int(input())
for case in range(T):
C, M, P, V = map(float, input().split())
res = solve(C, M, P, V)
print(res) | FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | SCALE = 1000000.0
def find_odds(
c: float, m: float, p: float, v: float, cumulative: float = SCALE, depth=1
) -> float:
total = cumulative / SCALE * p / SCALE * depth
if c > 0:
new_c = max(0.0, c - v)
delta = c - new_c
if m > 0:
new_p = p + 0.5 * delta
new_m = m + 0.5 * delta
else:
new_p = p + delta
new_m = 0
total += find_odds(new_c, new_m, new_p, v, cumulative * c / SCALE, depth + 1)
if m > 0:
new_m = max(0.0, m - v)
delta = m - new_m
if c > 0:
new_p = p + 0.5 * delta
new_c = c + 0.5 * delta
else:
new_p = p + delta
new_c = 0
total += find_odds(new_c, new_m, new_p, v, cumulative * m / SCALE, depth + 1)
return total
for _ in range(int(input())):
c, m, p, v = [float(i) for i in input().split(" ")]
print(find_odds(c * SCALE, m * SCALE, p * SCALE, v * SCALE)) | ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
input = sys.stdin.readline
def calc(c, m, p, v):
ANS = p / 10**16
if c != 0:
M = min(c, v)
if m == 0:
ANS += c / 10**16 * (1 + calc(c - M, 0, p + M, v))
else:
ANS += c / 10**16 * (1 + calc(c - M, m + M // 2, p + M // 2, v))
if m != 0:
M = min(m, v)
if c == 0:
ANS += m / 10**16 * (1 + calc(0, m - M, p + M, v))
else:
ANS += m / 10**16 * (1 + calc(c + M // 2, m - M, p + M // 2, v))
return ANS
t = int(input())
for tests in range(t):
c, m, p, v = input().split()
c += "0" * (10 - len(c))
m += "0" * (10 - len(m))
p += "0" * (10 - len(p))
v += "0" * (10 - len(v))
c = int(c[2:]) * 10**8
m = int(m[2:]) * 10**8
p = int(p[2:]) * 10**8
v = int(v[2:]) * 10**8
print(calc(c, m, p, v)) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def main():
t = int(input())
for i in range(t):
c, m, p, v = list(map(float, input().split()))
res = 0
cur = [[c, m, p, 1]]
i = 0
while cur != []:
nxt = []
for node in cur:
c, m, p, q = node
if c != 0:
if c > v + 1e-06:
if m != 0:
nxt.append([c - v, m + v / 2, p + v / 2, q * c])
else:
nxt.append([c - v, 0, p + v, q * c])
elif m != 0:
nxt.append([0, m + c / 2, p + c / 2, q * c])
else:
nxt.append([0, 0, 1, q * c])
if m != 0:
if m > v + 1e-06:
if c != 0:
nxt.append([c + v / 2, m - v, p + v / 2, q * m])
else:
nxt.append([0, m - v, p + v, q * m])
elif c != 0:
nxt.append([c + m / 2, 0, p + m / 2, q * m])
else:
nxt.append([0, 0, 1, q * m])
res += p * q * (i + 1)
cur = nxt
i += 1
print(res)
while True:
try:
main()
except:
break | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def dfs(c, m, p, v, steps, prob):
ans = 0
if c != -1:
if m != -1:
if c <= v:
ans += dfs(-1, m + c / 2, p + c / 2, v, steps + 1, prob * c / r)
else:
ans += dfs(c - v, m + v / 2, p + v / 2, v, steps + 1, prob * c / r)
elif c <= v:
ans += dfs(-1, -1, p + c, v, steps + 1, prob * c / r)
else:
ans += dfs(c - v, -1, p + v, v, steps + 1, prob * c / r)
if m != -1:
if c != -1:
if m <= v:
ans += dfs(c + m / 2, -1, p + m / 2, v, steps + 1, prob * m / r)
else:
ans += dfs(c + v / 2, m - v, p + v / 2, v, steps + 1, prob * m / r)
elif m <= v:
ans += dfs(-1, -1, p + m, v, steps + 1, prob * m / r)
else:
ans += dfs(-1, m - v, p + v, v, steps + 1, prob * m / r)
if p:
ans += steps * prob * p / r
return ans
r = 10**9
T = int(input())
for case in range(T):
c, m, p, v = list(map(float, input().split()))
c *= r
m *= r
p *= r
v *= r
answer = dfs(c, m, p, v, 1, 1)
print(answer) | FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
sys.setrecursionlimit(15000)
epsilon = 1e-05
def game(c, m, p, chosen, length=0):
length += 1
if chosen == "p":
return p * length
if chosen == "m":
if m - v < epsilon:
if c > epsilon:
return m * game(c + m / 2, 0, p + m / 2, "c", length) + m * game(
c + m / 2, 0, p + m / 2, "p", length
)
else:
return m * game(c, 0, p + m, "p", length)
elif c > epsilon:
return (
m * game(c + v / 2, m - v, p + v / 2, "c", length)
+ m * game(c + v / 2, m - v, p + v / 2, "m", length)
+ m * game(c + v / 2, m - v, p + v / 2, "p", length)
)
else:
return m * game(c, m - v, p + v, "m", length) + m * game(
c, m - v, p + v, "p", length
)
if chosen == "c":
if c - v < epsilon:
if m > epsilon:
return c * game(0, m + c / 2, p + c / 2, "m", length) + c * game(
0, m + c / 2, p + c / 2, "p", length
)
else:
return c * game(0, m, p + c, "p", length)
elif m > epsilon:
return (
c * game(c - v, m + v / 2, p + v / 2, "c", length)
+ c * game(c - v, m + v / 2, p + v / 2, "m", length)
+ c * game(c - v, m + v / 2, p + v / 2, "p", length)
)
else:
return c * game(c - v, m, p + v, "c", length) + c * game(
c - v, m, p + v, "p", length
)
t = int(input())
for _ in range(t):
c, m, p, v = map(float, input().split())
print(game(c, m, p, "c") + game(c, m, p, "m") + game(c, m, p, "p")) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER VAR NUMBER IF VAR STRING RETURN BIN_OP VAR VAR IF VAR STRING IF BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR STRING VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER STRING VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR IF VAR STRING IF BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR RETURN BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR STRING VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER STRING VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR STRING |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
from itertools import zip_longest
EPS = 1e-08
def read_floats():
return [float(i) for i in sys.stdin.readline().strip().split()]
def read_int():
return int(sys.stdin.readline().strip())
def probs(c, m, p, v):
if c > m:
return probs(m, c, p, v)
if c < EPS:
if v >= m:
return [p, 1 - p]
else:
return [p] + [((1 - p) * a) for a in probs(0, m - v, p + v, v)]
if c < EPS:
pc = []
elif v > c:
pc = probs(0, m + c / 2, p + c / 2, v)
else:
pc = probs(c - v, m + v / 2, p + v / 2, v)
if m < EPS:
pm = []
elif v > m:
pm = probs(c + m / 2, 0, p + m / 2, v)
else:
pm = probs(c + v / 2, m - v, p + v / 2, v)
return [p] + [(m * a + c * b) for a, b in zip_longest(pm, pc, fillvalue=0)]
t = read_int()
for i in range(t):
c, m, p, v = read_floats()
pb = probs(c, m, p, v)
print(sum(a * b for a, b in zip(range(1, 20), pb))) | IMPORT ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR RETURN LIST VAR BIN_OP NUMBER VAR RETURN BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP LIST VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | answers = []
def solve(c, m, p, v):
queue = [[1, 1, [c, m, p]]]
ans = 0
while queue:
prevProb, count, array = queue.pop()
length = len(array)
ans += prevProb * count * array[-1]
if length == 1:
continue
for i in range(length - 1):
prob = array[i]
toBeAdded = 0
if prob <= v:
toBeAdded = prob
else:
toBeAdded = v
eachItemShare = toBeAdded / (length - 1)
if prob <= v:
if length == 2:
queue.append(
[
prevProb * prob,
count + 1,
[float("%.6f" % (array[-1] + eachItemShare))],
]
)
elif i == 0:
queue.append(
[
prevProb * prob,
count + 1,
[
float("%.6f" % (array[-2] + eachItemShare)),
float("%.6f" % (array[-1] + eachItemShare)),
],
]
)
else:
queue.append(
[
prevProb * prob,
count + 1,
[
float("%.6f" % (array[0] + eachItemShare)),
float("%.6f" % (array[-1] + eachItemShare)),
],
]
)
elif length == 2:
queue.append(
[
prevProb * prob,
count + 1,
[
float("%.6f" % (array[0] - toBeAdded)),
float("%.6f" % (array[-1] + eachItemShare)),
],
]
)
elif i == 0:
queue.append(
[
prevProb * prob,
count + 1,
[
float("%.6f" % (array[0] - toBeAdded)),
float("%.6f" % (array[1] + eachItemShare)),
float("%.6f" % (array[-1] + eachItemShare)),
],
]
)
else:
queue.append(
[
prevProb * prob,
count + 1,
[
float("%.6f" % (array[0] + eachItemShare)),
float("%.6f" % (array[1] - toBeAdded)),
float("%.6f" % (array[-1] + eachItemShare)),
],
]
)
answers.append(ans)
T = int(input())
while T:
c, m, p, v = [float(x) for x in input().split()]
solve(c, m, p, v)
T -= 1
for ans in answers:
print(ans) | ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def dfs(c, m, p, v, op):
if c == 0 and m == 0:
return op * 1.0
sum = 0.0
sum = sum + p / 1000000.0 * op * 1.0
if c != 0:
if c < v:
if m == 0:
sum = sum + c / 1000000.0 * dfs(0, 0, p + c, v, op + 1)
else:
sum = sum + c / 1000000.0 * dfs(0, m + c / 2, p + c / 2, v, op + 1)
elif m == 0:
sum = sum + c / 1000000.0 * dfs(c - v, 0, p + v, v, op + 1)
else:
sum = sum + c / 1000000.0 * dfs(c - v, m + v / 2, p + v / 2, v, op + 1)
if m != 0:
if m < v:
if c == 0:
sum = sum + m / 1000000.0 * dfs(0, 0, p + m, v, op + 1)
else:
sum = sum + m / 1000000.0 * dfs(c + m / 2, 0, p + m / 2, v, op + 1)
elif c == 0:
sum = sum + m / 1000000.0 * dfs(0, m - v, p + v, v, op + 1)
else:
sum = sum + m / 1000000.0 * dfs(c + v / 2, m - v, p + v / 2, v, op + 1)
return sum
t = int(input())
while t:
c, m, p, v = map(float, input().split())
c = c * 1000000.0
m = m * 1000000.0
p = p * 1000000.0
v = v * 1000000.0
print(dfs(c, m, p, v, 1))
t = t - 1 | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | a = 0
def dp(c, m, p, v, h, ans):
global a
a += h * p * ans
if c > 0 + 1e-09:
if m < 1e-09:
y = dp(c - min(c, v), m, p + min(c, v), v, h + 1, ans * c)
else:
y = dp(
c - min(c, v), m + min(c, v) / 2, p + min(c, v) / 2, v, h + 1, ans * c
)
else:
x2 = 1
if m > 0 + 1e-09:
if c < 1e-09:
z = dp(c, m - min(m, v), p + min(m, v), v, h + 1, ans * m)
else:
z = dp(
c + min(m, v) / 2, m - min(m, v), p + min(m, v) / 2, v, h + 1, ans * m
)
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
dp(c, m, p, v, 1.0, 1.0)
print("%.12f" % round(a, 12))
a = 0 | ASSIGN VAR NUMBER FUNC_DEF VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | ans = []
eps = 1e-06
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
cur = p
pr = [(1, c, m, p)]
st = 1
while len(pr) > 0:
st += 1
pr1 = []
for i in range(len(pr)):
cur_pr, c, m, p = pr[i]
if c > eps:
if c > v:
if m > eps:
c1, m1, p1 = c - v, m + v / 2, p + v / 2
else:
c1, m1, p1 = c - v, 0, p + v
elif m > eps:
c1, m1, p1 = 0, m + c / 2, p + c / 2
else:
c1, m1, p1 = 0, 0, p + c
cur_pr1 = cur_pr * c
cur += st * cur_pr1 * p1
if abs(p1 - 1) > eps:
pr1.append((cur_pr1, c1, m1, p1))
if m > eps:
if m > v:
if c > eps:
c1, m1, p1 = c + v / 2, m - v, p + v / 2
else:
c1, m1, p1 = 0, m - v, p + v
elif c > eps:
c1, m1, p1 = c + m / 2, 0, p + m / 2
else:
c1, m1, p1 = 0, 0, p + m
cur_pr1 = cur_pr * m
cur += st * cur_pr1 * p1
if abs(p1 - 1) > eps:
pr1.append((cur_pr1, c1, m1, p1))
pr = pr1[:]
ans.append(cur)
print("\n".join(map(str, ans))) | ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
eps = 1e-05
def expect(c, m, p, v, itr):
if abs(c) < eps and abs(m) < eps:
return itr * p
if abs(c) < eps:
mv = max(m - v, 0)
return itr * p + m * expect(0, mv, p + m - mv, v, itr + 1)
if abs(m) < eps:
cv = max(c - v, 0)
return itr * p + c * expect(cv, 0, p + c - cv, v, itr + 1)
cv = max(c - v, 0)
mv = max(m - v, 0)
dc = (c - cv) / 2
dm = (m - mv) / 2
return (
itr * p
+ c * expect(cv, m + dc, p + dc, v, itr + 1)
+ m * expect(c + dm, mv, p + dm, v, itr + 1)
)
t = int(input())
for _ in range(t):
q = list(map(float, input().split()))
print(expect(q[0], q[1], q[2], q[3], 1)) | IMPORT ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def calc(a, v):
n = len(a)
ans = 0
for i in range(n):
if a[i] > v + 1e-10:
b = a.copy()
b[i] -= v
if n == 2:
b[1 - i] += v / n
ans += (calc(b, v) + 1) * a[i]
elif n == 2:
ans += (calc([a[1 - i] + a[i] / 2], v) + 1) * a[i]
else:
ans += (1 + 1) * a[i]
ans += 1 * (1 - sum(a))
return ans
def solve_case():
a = list(map(float, input().split()))
print(f"{calc(a[:2], a[-1]):.12f}")
n = int(input())
for i in range(n):
solve_case() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR LIST BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
def expect(c, m, p, v):
l = 1e-06
res = 0
res += p
if m > v and c > l:
res += (1 + expect(c + v / 2, m - v, p + v / 2, v)) * m
if c > v and m > l:
res += (1 + expect(c - v, m + v / 2, p + v / 2, v)) * c
if m <= v and m > l and c > l:
res += (1 + expect(c + m / 2, 0, p + m / 2, v)) * m
if c <= v and m > l and c > l:
res += (1 + expect(0, m + c / 2, p + c / 2, v)) * c
if c <= l and m > l:
if m > v:
res += (1 + expect(0, m - v, p + v, v)) * m
else:
res += (1 + expect(0, 0, p + m, v)) * m
if m <= l and c > l:
if c > v:
res += (1 + expect(c - v, 0, p + v, v)) * c
else:
res += (1 + expect(0, 0, p + c, v)) * c
return res
for _ in range(t):
c, m, p, v = [float(x) for x in input().split()]
print(expect(c, m, p, v)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
def calc(a, b, c, v, s):
if a == 0 and b == 0:
return c * s
eps = 10**-8
res = 0
if a > eps:
l = min(a, v)
if b > eps:
res += a * calc(a - l, b + l / 2, c + l / 2, v, s + 1)
else:
res += a * calc(a - l, 0, c + l, v, s + 1)
if b > eps:
l = min(b, v)
if a > eps:
res += b * calc(a + l / 2, b - l, c + l / 2, v, s + 1)
else:
res += b * calc(0, b - l, c + l, v, s + 1)
return s * c + res
t = int(input())
for _ in range(t):
c, m, p, v = map(float, input().split())
print(calc(c, m, p, v, 0) + 1) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | from sys import setrecursionlimit
blanck = []
def rec(a, b, c, v, p, card):
if a == 0 and b == 0 or c >= 1:
blanck.append(p * card)
return
l = [a, b, c]
for i in range(3):
if i != 2 and l[i] > 1e-08:
if i == 0:
ta = l[i]
dd = min(l[i], v)
if b > 1e-08:
rec(ta - dd, b + dd / 2, c + dd / 2, v, p * l[i], card + 1)
else:
rec(ta - dd, 0, c + dd, v, p * l[i], card + 1)
if i == 1:
tb = l[i]
dd = min(l[i], v)
if a > 1e-08:
rec(a + dd / 2, tb - dd, c + dd / 2, v, p * l[i], card + 1)
else:
rec(0, tb - dd, c + dd, v, p * l[i], card + 1)
if i == 2:
blanck.append(p * c * card)
for t in range(int(input())):
blanck = []
a, b, c, d = map(float, input().strip().split())
rec(a, b, c, d, 1, 1)
ans = 0.0
for i in blanck:
if i != float("inf"):
ans += i
print(ans) | ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
def is_zero(x):
return abs(x) < 1e-09
def EV(x, y, z, v):
if is_zero(x) and is_zero(y):
return 1
elif is_zero(x):
step = min(y, v)
return 1 + y * EV(x, y - step, z + step, v)
elif is_zero(y):
step = min(x, v)
return 1 + x * EV(x - step, y, z + step, v)
else:
step1 = min(x, v)
step2 = min(y, v)
return (
1
+ x * EV(x - step1, y + step1 / 2, z + step1 / 2, v)
+ y * EV(x + step2 / 2, y - step2, z + step2 / 2, v)
)
for test in range(t):
c, m, p, v = [float(x) for x in input().split()]
print(EV(c, m, p, v)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | zero = 1e-06
def f(c, m, p, v, i, prob):
if 1 - p <= zero:
return 0
a1 = 0
c1 = c
m1 = m
p1 = p
prob1 = prob
if c > zero:
o = min(c, v)
if m > zero:
m, p = m + o / 2, p + o / 2
else:
p = p + o
prob = prob * c
a1 = i * prob * p + f(c - o, m, p, v, i + 1, prob)
a2 = 0
if m > zero:
t = min(m1, v)
if c1 > zero:
c1, p1 = c1 + t / 2, p1 + t / 2
else:
p1 = p1 + t
prob1 = prob1 * m1
a2 = i * prob1 * p1 + f(c1, m1 - t, p1, v, i + 1, prob1)
return a1 + a2
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
print(f(c, m, p, v, 2, 1) + p) | ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def func(c, m, p, v, n):
ans = p * (n + 1)
if c > x:
a = min(c, v)
if m > x:
ans += c * func(c - a, m + a / 2, p + a / 2, v, n + 1)
else:
ans += c * func(c - a, m, p + a, v, n + 1)
if m > x:
a = min(m, v)
if c > x:
ans += m * func(c + a / 2, m - a, p + a / 2, v, n + 1)
else:
ans += m * func(c, m - a, p + a, v, n + 1)
return ans
t = int(input())
x = 1e-08
for _ in range(t):
c, m, p, v = map(float, input().split(" "))
ans = func(c, m, p, v, 0)
print("{:.12f}".format(float(ans))) | FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
for _ in range(t):
c, m, p, v = map(float, input().split())
def dp(c, m, p):
ans = 1
if c > 1e-06:
ch = min(c, v)
nc = c - ch
nm = m + ch / 2
np = p + ch / 2
if m < 1e-06:
nm = m
np += ch / 2
ans += c * dp(nc, nm, np)
if m > 1e-06:
ch = min(m, v)
nm = m - ch
nc = c + ch / 2
np = p + ch / 2
if c < 1e-06:
nc = c
np += ch / 2
ans += m * dp(nc, nm, np)
return ans
print(dp(c, m, p)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def f(a, c):
if a <= 1e-06:
return 0
else:
return c / 2
def dfs(c, m, p, v, step, sum):
ans = p * step * sum
if c >= 1e-06:
if c < v:
ans = ans + dfs(c - c, m + f(m, c), p + c - f(m, c), v, step + 1, c * sum)
else:
ans = ans + dfs(c - v, m + f(m, v), p + v - f(m, v), v, step + 1, c * sum)
if m >= 1e-06:
if m < v:
ans = ans + dfs(c + f(c, m), m - m, p + m - f(c, m), v, step + 1, m * sum)
else:
ans = ans + dfs(c + f(c, v), m - v, p + v - f(c, v), v, step + 1, m * sum)
return ans / 1000000.0
t = int(input())
while t != 0:
t = t - 1
l = list(map(float, input().split()))
ans = dfs(
l[0] * 1000000.0, l[1] * 1000000.0, l[2] * 1000000.0, l[3] * 1000000.0, 1, 1
)
print(ans) | FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def dfs(c, m, p, n, k):
global v, res
res += p * n * k
if c > 1e-06 and m > 1e-06:
if c >= v:
dfs(c - v, m + v / 2, p + v / 2, n * c, k + 1)
else:
dfs(0, m + c / 2, p + c / 2, n * c, k + 1)
if m >= v:
dfs(c + v / 2, m - v, p + v / 2, n * m, k + 1)
else:
dfs(c + m / 2, 0, p + m / 2, n * m, k + 1)
elif c > 1e-06:
if c >= v:
dfs(c - v, m, p + v, n * c, k + 1)
else:
dfs(0, 0, p + c, n * c, k + 1)
elif m > 1e-06:
if m >= v:
dfs(c, m - v, p + v, n * m, k + 1)
else:
dfs(0, 0, p + m, n * m, k + 1)
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
res = 0
dfs(c, m, p, 1, 1)
print(res) | FUNC_DEF VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def help(c, m, p, v, count, h):
global ans
if c < 1e-06:
c = 0
if m < 1e-06:
m = 0
if c == 0 and m == 0:
ans = ans + p * count * h
return
ans = ans + h * p * count
x, y, z = c, m, p
if c != 0:
if m != 0:
if c <= v:
m = m + c / 2
p = p + c / 2
else:
m = m + v / 2
p = p + v / 2
elif c <= v:
p = p + c
else:
p = p + v
c = max(0.0, c - v)
help(c, m, p, v, count + 1, h * x)
c, m, p = x, y, z
if m != 0:
if c != 0:
if m <= v:
c = c + m / 2
p = p + m / 2
else:
c = c + v / 2
p = p + v / 2
elif m <= v:
p = p + m
else:
p = p + v
m = max(0.0, m - v)
help(c, m, p, v, count + 1, h * y)
for _ in range(int(input())):
c, m, p, v = map(float, input().split())
ans = 0
help(c, m, p, v, 1, 1.0)
print("%.25f" % ans) | FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def expected(c, m, p, v):
E = 1.0
if c > 1e-07:
if m <= 1e-07:
E += c * expected(c - min(c, v), m, p + min(c, v), v)
else:
E += c * expected(
c - min(c, v), m + min(c, v) / 2.0, p + min(c, v) / 2.0, v
)
if m > 1e-07:
if c <= 1e-07:
E += m * expected(c, m - min(m, v), p + min(m, v), v)
else:
E += m * expected(
c + min(m, v) / 2.0, m - min(m, v), p + min(m, v) / 2.0, v
)
return E
t = int(input())
for _ in range(t):
c, m, p, v = [float(x) for x in input().split()]
print(expected(c, m, p, v)) | FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | delta = 1e-12
def recur(a, b, c, d, cur=1):
assert abs(a + b + c - 1) < delta * 0.1, f"{a} {b} {c}"
a, b = sorted([a, b])
if a > delta:
A = (
recur(
max(a - d, 0), b + min(a / 2, d / 2), c + min(a / 2, d / 2), d, cur + 1
)
* a
)
else:
A = 0
if b > delta:
if a > delta:
B = (
recur(
a + min(b / 2, d / 2),
max(b - d, 0),
c + min(b / 2, d / 2),
d,
cur + 1,
)
* b
)
else:
B = recur(a, max(b - d, 0), c + min(b, d), d, cur + 1) * b
else:
B = 0
return A + B + c * cur
for _ in range(int(input())):
a, b, c, d = map(float, input().split())
print(recur(a, b, c, d)) | ASSIGN VAR NUMBER FUNC_DEF NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR STRING VAR STRING VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | prob = 0
v = 0
def rec(c, m, p, val, num):
global prob
global v
prob += val * p * num
if p < 0.999994:
if c > 1e-06:
if c > v:
if m > 1e-06:
rec(c - v, m + v / 2, p + v / 2, val * c, num + 1)
else:
rec(c - v, 0, p + v, val * c, num + 1)
elif m > 1e-06:
rec(0, m + c / 2, p + c / 2, val * c, num + 1)
else:
rec(0, 0, p + c, val * c, num + 1)
if m > 1e-06:
if m > v:
if c > 1e-06:
rec(c + v / 2, m - v, p + v / 2, val * m, num + 1)
else:
rec(0, m - v, p + v, val * m, num + 1)
elif c > 1e-06:
rec(c + m / 2, 0, p + m / 2, val * m, num + 1)
else:
rec(0, 0, p + m, val * m, num + 1)
for t in range(int(input())):
prob = 0
c, m, p, v = map(float, input().split())
rec(c, m, p, 1, 1)
print(prob) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def solve():
c, m, p, v = map(float, input().split())
def rec1(a, p, cur, r):
cur += 1
ans = cur * p * r
if a > 1e-08:
if a >= v:
ans += rec1(a - v, p + v, cur, r * a)
else:
ans += rec1(0, p + a, cur, r * a)
return ans
def rec(c, m, p, cur, r):
cur += 1
ans = cur * p * r
if c > 1e-08:
if c > v + 1e-09:
ans += rec(c - v, m + v / 2, p + v / 2, cur, r * c)
else:
ans += rec1(m + c / 2, p + c / 2, cur, r * c)
if m > 1e-08:
if m > v + 1e-09:
ans += rec(c + v / 2, m - v, p + v / 2, cur, r * m)
else:
ans += rec1(c + m / 2, p + m / 2, cur, r * m)
return ans
print(f"{rec(c, m, p, 0, 1):.{9}f}")
k = int(input())
for i in range(k):
solve() | FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER STRING NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
for _ in range(t):
def f(c, m, p, v):
if c <= v:
if m > 0:
return -1, m + c / 2, p + c / 2, v
else:
return -1, -1, 10000, v
elif m > 0:
return c - v, m + v / 2, p + v / 2, v
else:
return c - v, -1, p + v, v
def g(c, m, p, v):
if m <= v:
if c > 0:
return c + m / 2, -1, p + m / 2, v
else:
return -1, -1, 10000, v
elif c > 0:
return c + v / 2, m - v, p + v / 2, v
else:
return -1, m - v, p + v, v
def exp(c, m, p, v):
if p == 10000:
return 1
s = p
if c > 0:
s += c * (1 + exp(*f(c, m, p, v)))
if m > 0:
s += m * (1 + exp(*g(c, m, p, v)))
return s / 10000
c, m, p, v = map(float, input().split())
c *= 10000
m *= 10000
p *= 10000
v *= 10000
print(exp(c, m, p, v)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR IF VAR NUMBER RETURN NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER NUMBER NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER NUMBER NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | r = 1000000000.0
def check(c, m, p, v, l):
ans = p / r * l
if c > 0:
if c > v:
if m > 0:
ans += c / r * check(c - v, m + v / 2, p + v / 2, v, l + 1)
else:
ans += c / r * check(c - v, 0, p + v, v, l + 1)
elif m > 0:
ans += c / r * check(0, m + c / 2, p + c / 2, v, l + 1)
else:
ans += c / r * check(0, 0, p + c, v, l + 1)
if m > 0:
if m > v:
if c > 0:
ans += m / r * check(c + v / 2, m - v, p + v / 2, v, l + 1)
else:
ans += m / r * check(0, m - v, p + v, v, l + 1)
elif c > 0:
ans += m / r * check(c + m / 2, 0, p + m / 2, v, l + 1)
else:
ans += m / r * check(0, 0, p + m, v, l + 1)
return ans
test = int(input())
for i in range(test):
lst = [float(i) for i in input().split()][:4]
c, m, p, v = lst[0], lst[1], lst[2], lst[3]
c = c * r
m = m * r
p = p * r
v = v * r
ans = 0.0
ans = check(c, m, p, v, 1)
print(ans) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
input = sys.stdin.readline
shift = 10**4
ans = 0
def solve(c, m, p, v, races, prob):
global ans
ans += (races + 1) * prob * p / shift
if c > v:
if m == 0:
solve(c - v, 0, p + v, v, races + 1, prob * c / shift)
else:
solve(c - v, m + v / 2, p + v / 2, v, races + 1, prob * c / shift)
elif c > 0:
if m == 0:
solve(0, 0, shift, v, races + 1, prob * c / shift)
else:
solve(0, m + c / 2, p + c / 2, v, races + 1, prob * c / shift)
if m > v:
if c == 0:
solve(c, m - v, p + v, v, races + 1, prob * m / shift)
else:
solve(c + v / 2, m - v, p + v / 2, v, races + 1, prob * m / shift)
elif m > 0:
if c == 0:
solve(0, 0, shift, v, races + 1, prob * m / shift)
else:
solve(c + m / 2, 0, p + m / 2, v, races + 1, prob * m / shift)
for _ in range(int(input())):
a = list(map(lambda x: str(x[2:]), input().split()))
for i in range(4):
a[i] = int(a[i]) * 10 ** (4 - len(a[i]))
c, m, p, v = a
ans = 0
solve(c, m, p, v, 0, 1)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
sys.setrecursionlimit(10**5)
result = 0
eps = 1e-09
def pink(c, m, p, v, prob, step):
global result, eps
if c > eps:
if c - v > eps:
if m > eps:
pink(c - v, m + v / 2, p + v / 2, v, prob * c, step + 1)
else:
pink(c - v, m, p + v, v, prob * c, step + 1)
elif m > eps:
pink(0, m + c / 2, p + c / 2, v, prob * c, step + 1)
else:
pink(0, m, p + c, v, prob * c, step + 1)
if m > eps:
if m - v > eps:
if c > eps:
pink(c + v / 2, m - v, p + v / 2, v, prob * m, step + 1)
else:
pink(c, m - v, p + v, v, prob * m, step + 1)
elif c > eps:
pink(c + m / 2, 0, p + m / 2, v, prob * m, step + 1)
else:
pink(c, 0, p + m, v, prob * m, step + 1)
if p > eps:
result += prob * p * step
return
num = int(sys.stdin.readline().strip())
for k in range(num):
c, m, p, v = map(float, sys.stdin.readline().split())
pink(c, m, p, v, 1, 1)
print(result)
result = 0 | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | scale = 10**6
def calculate_expectation(c, m, p, v):
expectation = 1
if c != 0:
if c <= v:
if m != 0:
expectation += (
c / scale * calculate_expectation(0, m + c / 2, p + c / 2, v)
)
else:
expectation += c / scale
elif m != 0:
expectation += (
c / scale * calculate_expectation(c - v, m + v / 2, p + v / 2, v)
)
else:
expectation += c / scale * calculate_expectation(c - v, 0, p + v, v)
if m != 0:
if m <= v:
if c != 0:
expectation += (
m / scale * calculate_expectation(c + m / 2, 0, p + m / 2, v)
)
else:
expectation += m / scale
elif c != 0:
expectation += (
m / scale * calculate_expectation(c + v / 2, m - v, p + v / 2, v)
)
else:
expectation += m / scale * calculate_expectation(0, m - v, p + v, v)
return expectation
t = int(input())
results = []
for i in range(t):
c, m, p, v = map(float, input().split())
c = c * scale
m = m * scale
p = p * scale
v = v * scale
results.append(calculate_expectation(c, m, p, v))
for i in range(t):
print(results[i]) | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
sys.stderr = sys.stdout
K = 1000000
def solve(c, m, p, v):
log("c", c, "m", m, "p", p, "v", v)
c = int(round(c * K))
m = int(round(m * K))
p = int(round(p * K))
v = int(round(v * K))
log("c", c, "m", m, "p", p, "v", v)
v2 = v // 2
S = [(c, m, p, 1.0)]
t = 0.0
while S:
T = []
for s in S:
if len(s) == 4:
c, m, p, f = s
t += f
if c > v:
T.append((c - v, m + v2, p + v2, f * (c / K)))
else:
c2 = c // 2
T.append((m + c2, p + c2, f * (c / K)))
if m > v:
T.append((c + v2, m - v, p + v2, f * (m / K)))
else:
m2 = m // 2
T.append((c + m2, p + m2, f * (m / K)))
elif len(s) == 3:
m, p, f = s
t += f
m_ = m - v
if m > v:
T.append((m - v, p + v, f * (m / K)))
else:
T.append((10000000, f * (m / K)))
elif len(s) == 2:
p, f = s
t += f
else:
raise AssertionError(s)
S = T
return t
def case():
c, m, p, v = map(float, input().split())
t = solve(c, m, p, v)
return f"{t:0.12f}"
def main():
t = readint()
print("\n".join(case() for _ in range(t)))
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintt():
return tuple(readinti())
def readintl():
return list(readinti())
def readinttl(k):
return [readintt() for _ in range(k)]
def readintll(k):
return [readintl() for _ in range(k)]
def lstr(l):
return " ".join(map(str, l))
def llstr(ll):
return "\n".join(map(lstr, ll))
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.__stderr__)
main() | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
THR = 1e-09
def calc(c, m, p, v, n):
ans = p * n
if c < THR and m < THR:
return ans
if c >= 0:
if c > v:
if m < THR:
ans += c * calc(c - v, -1, p + v, v, n + 1)
else:
ans += c * calc(c - v, m + v / 2, p + v / 2, v, n + 1)
elif m < THR:
ans += c * calc(-1, -1, 1, v, n + 1)
else:
ans += c * calc(-1, m + c / 2, p + c / 2, v, n + 1)
if m >= 0:
if m > v:
if c < THR:
ans += m * calc(-1, m - v, p + v, v, n + 1)
else:
ans += m * calc(c + v / 2, m - v, p + v / 2, v, n + 1)
elif c < THR:
ans += m * calc(-1, -1, 1, v, n + 1)
else:
ans += m * calc(c + m / 2, -1, p + m / 2, v, n + 1)
return ans
for i in range(t):
c, m, p, v = [float(j) for j in input().split()]
print(calc(c, m, p, v, 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def process(c, m, p, v):
d = {"": [1, (c, m, p), (True, True, True)]}
I = 1
answer = 0
while len(d) > 0:
d2 = {}
for x in d:
prob, t, R = d[x]
c2, m2, p2 = t
c_r, m_r, p_r = R
for i in range(3):
if i == 0 and R[0]:
step = "C"
if c2 > v and c2 - v > 1e-05:
if R[1]:
t2 = [c2 - v, m2 + v / 2, p2 + v / 2]
d2[x + step] = [prob * c2, t2, R]
else:
t2 = [c2 - v, 0, p2 + v]
d2[x + step] = [prob * c2, t2, R]
elif R[1]:
t2 = [0, m2 + c2 / 2, p2 + c2 / 2]
d2[x + step] = [prob * c2, t2, (False, m_r, p_r)]
else:
t2 = [0, 0, 1]
d2[x + step] = [prob * c2, t2, (False, m_r, p_r)]
elif i == 1 and R[1]:
step = "M"
if m2 > v and m2 - v > 1e-05:
if R[0]:
t2 = [c2 + v / 2, m2 - v, p2 + v / 2]
d2[x + step] = [prob * m2, t2, R]
else:
t2 = [0, m2 - v, p2 + v]
d2[x + step] = [prob * m2, t2, R]
elif R[0]:
t2 = [c2 + m2 / 2, 0, p2 + m2 / 2]
d2[x + step] = [prob * m2, t2, (c_r, False, p_r)]
else:
t2 = [0, 0, 1]
d2[x + step] = [prob * m2, t2, (c_r, False, p_r)]
elif i == 2:
answer += I * prob * p2
I += 1
d = d2
return answer
t = int(input())
for i in range(t):
c, m, p, v = [float(x) for x in input().split()]
print(process(c, m, p, v)) | FUNC_DEF ASSIGN VAR DICT STRING LIST NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def main():
c, m, p, v = map(float, input().split())
eps = 10**-9
def dfs(cnt, x, c, m, p):
cnt += 1
ret = 0
ret = x * p * cnt
if c != 0:
if c < v + eps:
if m == 0:
ret += dfs(cnt, x * c, 0, 0, 1)
else:
ret += dfs(cnt, x * c, 0, m + c / 2, p + c / 2)
elif m == 0:
ret += dfs(cnt, x * c, c - v, 0, p + v)
else:
ret += dfs(cnt, x * c, c - v, m + v / 2, p + v / 2)
if m != 0:
if m < v + eps:
if c == 0:
ret += dfs(cnt, x * m, 0, 0, 1)
else:
ret += dfs(cnt, x * m, c + m / 2, 0, p + m / 2)
elif c == 0:
ret += dfs(cnt, x * m, 0, m - v, p + v)
else:
ret += dfs(cnt, x * m, c + v / 2, m - v, p + v / 2)
return ret
print(dfs(0, 1, c, m, p))
for _ in range(int(input())):
main() | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def solve(c, m, p, v, cnt):
if 1 - p < 1e-05:
return cnt
ret = 0
if c >= 1e-05 and m >= 1e-05:
if c <= v:
ret += c * solve(0, m + c / 2, p + c / 2, v, cnt + 1)
else:
ret += c * solve(c - v, m + v / 2, p + v / 2, v, cnt + 1)
if m <= v:
ret += m * solve(c + m / 2, 0, p + m / 2, v, cnt + 1)
else:
ret += m * solve(c + v / 2, m - v, p + v / 2, v, cnt + 1)
elif c < 1e-05:
if m <= v:
ret += m * solve(0, 0, 1, v, cnt + 1)
else:
ret += m * solve(0, m - v, p + v, v, cnt + 1)
elif m < 1e-05:
if c <= v:
ret += c * solve(0, 0, 1, v, cnt + 1)
else:
ret += c * solve(c - v, 0, p + v, v, cnt + 1)
ret += p * cnt
return ret
for _ in range(int(input())):
qc, qm, qp, qv = map(float, input().split())
print(solve(qc, qm, qp, qv, 1)) | FUNC_DEF IF BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def E(c, m, p, v):
ans = 0
if p > 1e-06:
ans += p
if c > 1e-06:
m2 = m
p2 = p
if m > 1e-06 and p > 1e-06:
m2 += min(c, v) / 2
p2 += min(c, v) / 2
elif p > 1e-06:
p2 += min(c, v)
ans += c * (1 + E(max(c - v, 0), m2, p2, v))
if m > 1e-06:
c1 = c
p1 = p
if c > 1e-06 and p > 1e-06:
c1 += min(m, v) / 2
p1 += min(m, v) / 2
elif p > 1e-06:
p1 += min(m, v)
ans += m * (1 + E(c1, max(m - v, 0), p1, v))
return ans
for _ in range(int(input())):
c, m, p, v = list(map(float, input().split()))
print(E(c, m, p, v)) | FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | T = int(input())
for t in range(T):
[c, m, p, v] = list(map(float, input().split()))
def dp(c, m, p, k):
if c < 1e-06:
c = 0
if m < 1e-06:
m = 0
a = min(c, v)
b = min(m, v)
if c == 0 and m == 0:
return (k + 1) * p
if m == 0:
return c * dp(c - a, m, p + a, k + 1) + (k + 1) * p
if c == 0:
return m * dp(c, m - b, p + b, k + 1) + (k + 1) * p
return (
c * dp(c - a, m + a / 2.0, p + a / 2.0, k + 1)
+ m * dp(c + b / 2.0, m - b, p + b / 2.0, k + 1)
+ (k + 1) * p
)
print(dp(c, m, p, 0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def rec(c, m, p, v):
ret = 0
ret += p * 0
if 0 < m <= v + 1e-05:
tmp = 0
if c > 0:
tmp = rec(c + m / 2, 0, p + m / 2, v)
else:
tmp = rec(0, 0, 1, v)
ret += m * tmp
elif m > v:
tmp = 0
if c > 0:
tmp = rec(c + v / 2, m - v, p + v / 2, v)
else:
tmp = rec(0, m - v, p + v, v)
ret += m * tmp
if 0 < c <= v + 1e-05:
tmp = 0
if m > 0:
tmp = rec(0, m + c / 2, p + c / 2, v)
else:
tmp = rec(0, 0, 1, v)
ret += c * tmp
elif c > v:
tmp = 0
if m > 0:
tmp = rec(c - v, m + v / 2, p + v / 2, v)
else:
tmp = rec(c - v, 0, p + v, v)
ret += c * tmp
return ret + c + m
t = int(input())
for case in range(t):
c, m, p, v = map(float, input().split())
print(1 + rec(c, m, p, v)) | FUNC_DEF ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | cache = {}
def expected(P, v):
c, m, p = P
ans = p
if 0 < c:
P1 = [c, m, p]
if c <= v:
P1 = [0, 0, p + c] if m == 0 else [0, m + c / 2, p + c / 2]
if c > v:
P1 = [c - v, 0, p + v] if m == 0 else [c - v, m + v / 2, p + v / 2]
ans += c * (1 + expected(P1, v))
if 0 < m:
P2 = [c, m, p]
if m <= v:
P2 = [0, 0, p + m] if c == 0 else [c + m / 2, 0, p + m / 2]
if m > v:
P2 = [0, m - v, p + v] if c == 0 else [c + v / 2, m - v, p + v / 2]
ans += m * (1 + expected(P2, v))
return ans * 0.0001
t = int(input())
for _ in range(t):
L = list(map(float, input().split()))
P, v = L[:3], L[3]
P = [(x * 10000) for x in P]
v = 10000 * v
print(expected(P, v)) | ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF NUMBER VAR ASSIGN VAR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER BIN_OP VAR VAR LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF NUMBER VAR ASSIGN VAR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER BIN_OP VAR VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER BIN_OP VAR VAR BIN_OP VAR VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | def f(c, m, p, v):
if abs(1 - p) < 10**-7:
return 1
if c < 10**-7:
ans = 0
if m - v > 0:
ans += m * (f(0, m - v, p + v, v) + 1)
else:
ans += m * 2
ans += p
return ans
elif m < 10**-7:
ans = 0
if c > v:
ans += c * (f(c - v, 0, p + v, v) + 1)
else:
ans += c * 2
ans += p
return ans
else:
ans = 0
if c - v > 0:
ans += c * (f(c - v, m + v / 2, p + v / 2, v) + 1)
else:
ans += c * (f(0, m + c / 2, p + c / 2, v) + 1)
if m - v > 0:
ans += m * (f(c + v / 2, m - v, p + v / 2, v) + 1)
else:
ans += m * (f(c + m / 2, 0, p + m / 2, v) + 1)
ans += p
return ans
for __ in range(int(input())):
c, m, p, v = map(float, input().split())
print(f(c, m, p, v)) | FUNC_DEF IF FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
After defeating a Blacklist Rival, you get a chance to draw $1$ reward slip out of $x$ hidden valid slips. Initially, $x=3$ and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are $c$, $m$, and $p$, respectively. There is also a volatility factor $v$. You can play any number of Rival Races as long as you don't draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the $x$ valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was $a$. Then,
If the item was a Pink Slip, the quest is over, and you will not play any more races.
Otherwise,
If $a\leq v$, the probability of the item drawn becomes $0$ and the item is no longer a valid item for all the further draws, reducing $x$ by $1$. Moreover, the reduced probability $a$ is distributed equally among the other remaining valid items.
If $a > v$, the probability of the item drawn reduces by $v$ and the reduced probability is distributed equally among the other valid items.
For example,
If $(c,m,p)=(0.2,0.1,0.7)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,0.15,0.75)$.
If $(c,m,p)=(0.1,0.2,0.7)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,0.25,0.75)$.
If $(c,m,p)=(0.2,Invalid,0.8)$ and $v=0.1$, after drawing Cash, the new probabilities will be $(0.1,Invalid,0.9)$.
If $(c,m,p)=(0.1,Invalid,0.9)$ and $v=0.2$, after drawing Cash, the new probabilities will be $(Invalid,Invalid,1.0)$.
You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.
-----Input-----
The first line of input contains a single integer $t$ ($1\leq t\leq 10$) — the number of test cases.
The first and the only line of each test case contains four real numbers $c$, $m$, $p$ and $v$ ($0 < c,m,p < 1$, $c+m+p=1$, $0.1\leq v\leq 0.9$).
Additionally, it is guaranteed that each of $c$, $m$, $p$ and $v$ have at most $4$ decimal places.
-----Output-----
For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
-----Examples-----
Input
4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
Output
1.532000000000
1.860000000000
5.005050776521
4.260163673896
-----Note-----
For the first test case, the possible drawing sequences are:
P with a probability of $0.6$;
CP with a probability of $0.2\cdot 0.7 = 0.14$;
CMP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
CMMP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$;
MP with a probability of $0.2\cdot 0.7 = 0.14$;
MCP with a probability of $0.2\cdot 0.3\cdot 0.9 = 0.054$;
MCCP with a probability of $0.2\cdot 0.3\cdot 0.1\cdot 1 = 0.006$.
So, the expected number of races is equal to $1\cdot 0.6 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 + 2\cdot 0.14 + 3\cdot 0.054 + 4\cdot 0.006 = 1.532$.
For the second test case, the possible drawing sequences are:
P with a probability of $0.4$;
CP with a probability of $0.4\cdot 0.6 = 0.24$;
CMP with a probability of $0.4\cdot 0.4\cdot 1 = 0.16$;
MP with a probability of $0.2\cdot 0.5 = 0.1$;
MCP with a probability of $0.2\cdot 0.5\cdot 1 = 0.1$.
So, the expected number of races is equal to $1\cdot 0.4 + 2\cdot 0.24 + 3\cdot 0.16 + 2\cdot 0.1 + 3\cdot 0.1 = 1.86$. | t = int(input())
def eşittir(a, b):
if round(a, 9) == round(b, 9):
return True
return False
for i in range(t):
c, m, p, v = map(float, input().split(" "))
stack = [(c, m, p, v, 1, 1)]
res = 0
while len(stack) != 0:
c, m, p, v, dismiss, turn = stack.pop()
dismiss = dismiss
if not eşittir(c, 0.0):
crit = max(0.0, c - v)
adder = (c - crit) / 2
if not eşittir(m, 0.0):
stack.append((crit, m + adder, p + adder, v, dismiss * c, turn + 1))
else:
stack.append((crit, 0.0, p + adder * 2, v, dismiss * c, turn + 1))
if not eşittir(m, 0.0):
crit = max(0.0, m - v)
adder = (m - crit) / 2
if not eşittir(c, 0.0):
stack.append((c + adder, crit, p + adder, v, dismiss * m, turn + 1))
else:
stack.append((0.0, crit, p + adder * 2, v, dismiss * m, turn + 1))
if p != 0:
res += dismiss * p * turn
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.