output_description stringlengths 15 956 | submission_id stringlengths 10 10 | status stringclasses 3 values | problem_id stringlengths 6 6 | input_description stringlengths 9 2.55k | attempt stringlengths 1 13.7k | problem_description stringlengths 7 5.24k | samples stringlengths 2 2.72k |
|---|---|---|---|---|---|---|---|
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s258411892 | Accepted | p03243 | Input is given from Standard Input in the following format:
N | print(111 * (10 - (1110 - int(input())) // 111))
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s918938679 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | from math import ceil
n = int(input())
print(111 * ceil(n / 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s134800516 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
for i in range(n, 1000):
if i % 111 = 0:
print(i)
exit()
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s990488561 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | x = input()
print(int(x[0]) * 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s402340244 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
for i in range(n, 1000):
if i % 111 = 0:
print(i)
break | Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s283227680 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
if n%111 = 0:
print(n)
else:
print((n//111+1)*111) | Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s082909682 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | from math import ceil
n = int(input())
print(111 * ceil(n // 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s286604243 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | print(((int(input()) - 1) // 111) * 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s877790200 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | print(111 * (int(input()) // 111 + 1))
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s554395791 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | p_100 = n // 100
tmp = n % 100
p_10 = tmp // 10
p_1 = tmp % 10
if p_100 == p_10:
if p_10 == p_1:
print(str(p_1) * 3)
elif p_10 <= p_1:
print(str(p_10 + 1) * 3)
elif p_10 >= p_1:
print(str(p_10) * 3)
elif p_100 <= p_10:
print(str(p_100 + 1) * 3)
elif p_100 >= p_10:
print(str(p_100) * 3)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s755374060 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | n = input()
m = n[0]
for i in range(len(n) - 1):
m += n[0]
if int(n) > int(m):
if n[0] != 9:
a = str(int(n[0] + 1))
for j in range(len(n) - 1):
a += str(int(n[0] + 1))
print(a)
else:
b = str(1)
for k in range(len(n)):
b += str(1)
print(b)
else:
print(m)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s193998218 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
cs = [1111, 999, 888, 777, 666, 555, 444, 333, 222, 111]
f = False
for i, c in enumerate(cs):
if c <= n:
print(cs[i - 1])
f = True
break
if not f:
print(cs[len(cs) - 1])
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s807108230 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | N=int(input())
ans=0
while(True):
ans+=111
if(ans>=N):
print(ans)
break
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s358033190 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
k = 0
if int(n/111) == n/111:
k = n/111
else k = int(n/111) + 1
print(111*k) | Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s189906028 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | a=input()
if int(a) <= int(a[0])*111:
print(int(a[0])*111)
else:
print((int(a[0])+1)*111
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s592021463 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | in = int(input())
a = in //100
b = (in % 100)
c = b % 10
b = b //10
if a == b and a == c:
pass
else:
a = a+1
print('{}{}{}'.format(a, a, a)) | Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s162249563 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | a = int(input()))
b = int(a /100)
if a > (100b + 10b +b -1):
print(100(b+1) + 10(b+1) +b +1)
else:
print(100b + 10b + b) | Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s623273292 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | a = list(str(input()))
e = int("".join(a))
b = int(a[1]) - int(a[1] * 111)
c = a[0]
c = int(c)
b = e - c * 111
if b < 0:
print(c * 111)
else:
c += 1
print(c * 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s457476948 | Accepted | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
c = 0
while (n + c) % 111 != 0:
c += 1
print(n + c)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s509874943 | Accepted | p03243 | Input is given from Standard Input in the following format:
N | print(str((int(input()) - 1) // 111 + 1) * 3)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s598417866 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | x = ((N - (N % 111)) / 111) * 111
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s226082136 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | input_line = input()
n = int(input_line / 111)
print(111 + n * 111)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s411257599 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | S = input()
ans = max(list(map(int, list(S))))
print(str(ans) * 3)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s831384041 | Accepted | p03243 | Input is given from Standard Input in the following format:
N | n = list(map(int, list(input())))
v1 = n[0] * 100 + n[1] * 10 + n[2] * 1
v2 = n[0] * 100 + n[0] * 10 + n[0] * 1
if v1 > v2:
n[0] += 1
s = str(n[0])
print("".join([s, s, s]))
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s814507560 | Runtime Error | p03243 | Input is given from Standard Input in the following format:
N | a = input()
if (a[-1] == '0' and (int(a[0]) >= int(a[2])) or (int(a[0]) >= int(a[2]) and int(a[0]) >= int(a[1])):
ans = int(a[0] * 3)
else:
ans = str(int(a[0])+1)*3
print(ans)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s597838701 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | line = int(input())
lina = list(map(int, [line / 100, (line / 10) % 10, line % 10]))
contest = [111 * lina[0], 111 * (lina[0] + 1)]
if contest[0] - line > 0:
print(contest[0])
else:
print(contest[1])
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s490377280 | Accepted | p03243 | Input is given from Standard Input in the following format:
N | x = list(map(int, input()))
print(str(x[0]) * 3 if (x[0] >= x[1] and x[0] >= x[2]) else str(x[0] + 1) * 3)
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
* * * | s298046388 | Wrong Answer | p03243 | Input is given from Standard Input in the following format:
N | n = int(input())
max = 0
cnt = 0
while n != 0:
if n % 10 > max:
max = n % 10
n = n // 10
cnt += 1
for i in range(cnt):
print(max, end="")
| Statement
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to
make his debut in some ABC x such that all the digits of x in base ten are the
same.
What is the earliest ABC where Kurohashi can make his debut? | [{"input": "111", "output": "111\n \n\nThe next ABC to be held is ABC 111, where Kurohashi can make his debut.\n\n* * *"}, {"input": "112", "output": "222\n \n\nThe next ABC to be held is ABC 112, which means Kurohashi can no longer\nparticipate in ABC 111. Among the ABCs where Kurohashi can make his debut, the\nearliest one is ABC 222.\n\n* * *"}, {"input": "750", "output": "777"}] |
Print the maximum possible number of i such that a_i=X.
* * * | s813440911 | Wrong Answer | p03613 | The input is given from Standard Input in the following format:
N
a_1 a_2 .. a_N | lis = [0] * ((10**5) + 1)
N = int(input().strip())
a = list(map(int, input().split()))
for i in range(N):
lis[a[i] - 1] += 1
lis[a[i]] += 1
lis[a[i] + 1] += 1
lis.sort(reverse=True)
print(lis[0])
| Statement
You are given an integer sequence of length N, a_1,a_2,...,a_N.
For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or
do nothing.
After these operations, you select an integer X and count the number of i such
that a_i=X.
Maximize this count by making optimal choices. | [{"input": "7\n 3 1 4 1 5 9 2", "output": "4\n \n\nFor example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4,\nthe maximum possible count.\n\n* * *"}, {"input": "10\n 0 1 2 3 4 5 6 7 8 9", "output": "3\n \n\n* * *"}, {"input": "1\n 99999", "output": "1"}] |
Print the maximum possible number of i such that a_i=X.
* * * | s900044203 | Accepted | p03613 | The input is given from Standard Input in the following format:
N
a_1 a_2 .. a_N | a = int(input())
b = list(map(int, input().split()))
c = {}
for i in b:
if i not in c:
c[i] = 1
else:
c[i] += 1
if i + 1 not in c:
c[i + 1] = 1
else:
c[i + 1] += 1
if i - 1 not in c:
c[i - 1] = 1
else:
c[i - 1] += 1
c = sorted(c.items(), key=lambda x: x[1], reverse=True)
print(c[0][1])
| Statement
You are given an integer sequence of length N, a_1,a_2,...,a_N.
For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or
do nothing.
After these operations, you select an integer X and count the number of i such
that a_i=X.
Maximize this count by making optimal choices. | [{"input": "7\n 3 1 4 1 5 9 2", "output": "4\n \n\nFor example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4,\nthe maximum possible count.\n\n* * *"}, {"input": "10\n 0 1 2 3 4 5 6 7 8 9", "output": "3\n \n\n* * *"}, {"input": "1\n 99999", "output": "1"}] |
Print the maximum possible number of i such that a_i=X.
* * * | s798717154 | Accepted | p03613 | The input is given from Standard Input in the following format:
N
a_1 a_2 .. a_N | # -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache
class Scanner:
def int():
return int(sys.stdin.readline().rstrip())
def string():
return sys.stdin.readline().rstrip()
def map_int():
return [int(x) for x in Scanner.string().split()]
def string_list(n):
return [input() for i in range(n)]
def int_list_list(n):
return [Scanner.mapInt() for i in range(n)]
def int_cols_list(n):
return [int(input()) for i in range(n)]
class Math:
def gcd(a, b):
if b == 0:
return a
return Math.gcd(b, a % b)
def lcm(a, b):
return (a * b) // Math.gcd(a, b)
def roundUp(a, b):
return -(-a // b)
def toUpperMultiple(a, x):
return Math.roundUp(a, x) * x
def toLowerMultiple(a, x):
return (a // x) * x
def nearPow2(n):
if n <= 0:
return 0
if n & (n - 1) == 0:
return n
ret = 1
while n > 0:
ret <<= 1
n >>= 1
return ret
def isPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
d = int(n**0.5) + 1
for i in range(3, d + 1, 2):
if n % i == 0:
return False
return True
MOD = int(1e09) + 7
def main():
N = Scanner.int()
A = Scanner.map_int()
M = defaultdict(int)
for i in range(N):
M[A[i]] += 1
M[A[i] + 1] += 1
M[A[i] - 1] += 1
ans = 0
for v in M.values():
ans = max(ans, v)
print(ans)
return
if __name__ == "__main__":
main()
| Statement
You are given an integer sequence of length N, a_1,a_2,...,a_N.
For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or
do nothing.
After these operations, you select an integer X and count the number of i such
that a_i=X.
Maximize this count by making optimal choices. | [{"input": "7\n 3 1 4 1 5 9 2", "output": "4\n \n\nFor example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4,\nthe maximum possible count.\n\n* * *"}, {"input": "10\n 0 1 2 3 4 5 6 7 8 9", "output": "3\n \n\n* * *"}, {"input": "1\n 99999", "output": "1"}] |
Print the maximum possible number of i such that a_i=X.
* * * | s923110106 | Wrong Answer | p03613 | The input is given from Standard Input in the following format:
N
a_1 a_2 .. a_N | A = int(input())
n_l = list(map(int, input().split()))
n_l.sort()
n_l_s = []
for i in range(A - 1):
n_l_s.append(n_l[i + 1] - n_l[i])
ans = 1
n = 0
c = 0
for i in range(A - 1):
n += n_l_s[i]
c += 1
if n > 2:
ans = max(ans, c)
c = 0
ans = max(ans, c)
print(ans)
| Statement
You are given an integer sequence of length N, a_1,a_2,...,a_N.
For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or
do nothing.
After these operations, you select an integer X and count the number of i such
that a_i=X.
Maximize this count by making optimal choices. | [{"input": "7\n 3 1 4 1 5 9 2", "output": "4\n \n\nFor example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4,\nthe maximum possible count.\n\n* * *"}, {"input": "10\n 0 1 2 3 4 5 6 7 8 9", "output": "3\n \n\n* * *"}, {"input": "1\n 99999", "output": "1"}] |
Print the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1.
* * * | s917627290 | Wrong Answer | p02901 | Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}} | from fractions import gcd as bltin_gcd
a, b = [int(x) for x in input().split()]
d = 1
d_list = []
while d <= a and d <= b:
if a % d == 0 and b % d == 0:
d_list.append(d)
d += 1
count = 0
length = len(d_list)
answer = 0
is_sosu = True
for i in range(length):
is_sosu = True
for j in range(length):
print(d_list[i], d_list[j], i == j, is_sosu, bltin_gcd(d_list[i], d_list[j]))
if i != j:
if bltin_gcd(d_list[i], d_list[j]) != 1:
is_sosu = False
length = len(d_list)
break
if not is_sosu:
answer += 1
print(answer)
| Statement
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan),
and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each
key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1. | [{"input": "2 3\n 10 1\n 1\n 15 1\n 2\n 30 2\n 1 2", "output": "25\n \n\nWe can unlock all the boxes by purchasing the first and second keys, at the\ncost of 25 yen, which is the minimum cost required.\n\n* * *"}, {"input": "12 1\n 100000 1\n 2", "output": "-1\n \n\nWe cannot unlock all the boxes.\n\n* * *"}, {"input": "4 6\n 67786 3\n 1 3 4\n 3497 1\n 2\n 44908 3\n 2 3 4\n 2156 3\n 2 3 4\n 26230 1\n 2\n 86918 1\n 3", "output": "69942"}] |
Print the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1.
* * * | s582086341 | Runtime Error | p02901 | Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}} | # 142e'' (高速化)
N, M = map(int, input().split()) # N<=12,M<=10**3
openable_box_and_price = {} # 開けられる箱のtaple:値段 なdict
# 同じ箱を開けられる鍵は最安値のものを選びつつ、読み込み
for i in range(M): # 10**3
a, b = map(int, input().split())
c = tuple(map(int, input().split()))
# print(c,":",a)
if c in openable_box_and_price:
# print("重複")
if openable_box_and_price[c] > a:
# print("安値更新")
openable_box_and_price[c] = a
else:
openable_box_and_price[c] = a
# 鍵に番号(0~)を付けるためlist化
key_price = list(openable_box_and_price.values())
key_openable_box = list(openable_box_and_price.keys())
# ある箱を開けられる鍵のリストを作る
box_and_openable_key = [[] for _ in range(N)]
key_number = 0
# for openable_boxes in key_openable_box: #10**3
openable_boxes = [openable_boxes for openable_boxes in key_openable_box]
for openable_box in openable_boxes: # 12
box_and_openable_key[openable_box - 1].append(key_number)
key_number += 1
# print("########################")
# print(key_price)
# print(key_openable_box)
# print(box_and_openable_key)
# print("########################")
# そもそも開けられる鍵が無い箱があったら-1を出力
for openable_key in box_and_openable_key:
if not openable_key:
print(-1)
break
else:
# すべての箱を開けられる鍵の組み合わせのうち、最安値を求める。
cheapest_price = sum(key_price) # 仮として最高値(全ての鍵を用いた場合)を入れる。
keys_and_sum_price = {} # 高速化のためにメモする
key_combination = []
base_key_combination = []
# key_combination_append = base_key_combination.append #appendを外に出して高速化
# key_combination_pop = key_combination.pop #popを外に出して高速化
def get_price_of_cheap_key_combination(box_number, cheapest_price, key_combination):
# print("箱:",box_number)
base_key_combination = key_combination
for openable_key in box_and_openable_key[box_number]:
# 箱に対する鍵を追加する
key_combination = base_key_combination + [openable_key]
# print(key_combination,":",end="")
# 値段を見る…dictにあったら省略
tuple_key_combination = tuple(set(key_combination))
if tuple_key_combination not in keys_and_sum_price:
# print("新規計算")
if base_key_combination != []:
now_price = (
keys_and_sum_price[tuple(set(base_key_combination))]
+ key_price[openable_key]
) # sum(key_price[i] for i in tuple_key_combination) #重複していなければ新しいのはopenable_key
else:
now_price = key_price[openable_key]
keys_and_sum_price[tuple_key_combination] = now_price
else:
# print("重複")
now_price = keys_and_sum_price[tuple_key_combination]
# print(now_price,",",cheapest_price)
# 既に高かったら、そこで枝刈り
if now_price < cheapest_price:
# 全部の箱に対して鍵を選択済みか
if box_number == N - 1: # Yes→価格比較
# print("堀り終了")
# print("最安値更新:",now_price)
cheapest_price = now_price
else: # No→再帰する
# print("深堀り")
cheapest_price = get_price_of_cheap_key_combination(
box_number + 1, cheapest_price, key_combination
)
# else:
# print("枝刈り")
# key_combination_pop(-1)
return cheapest_price
print(get_price_of_cheap_key_combination(0, cheapest_price, key_combination))
| Statement
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan),
and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each
key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1. | [{"input": "2 3\n 10 1\n 1\n 15 1\n 2\n 30 2\n 1 2", "output": "25\n \n\nWe can unlock all the boxes by purchasing the first and second keys, at the\ncost of 25 yen, which is the minimum cost required.\n\n* * *"}, {"input": "12 1\n 100000 1\n 2", "output": "-1\n \n\nWe cannot unlock all the boxes.\n\n* * *"}, {"input": "4 6\n 67786 3\n 1 3 4\n 3497 1\n 2\n 44908 3\n 2 3 4\n 2156 3\n 2 3 4\n 26230 1\n 2\n 86918 1\n 3", "output": "69942"}] |
Print the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1.
* * * | s198479292 | Wrong Answer | p02901 | Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}} | print([] * 3)
| Statement
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan),
and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each
key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is
impossible to unlock all of them, print -1. | [{"input": "2 3\n 10 1\n 1\n 15 1\n 2\n 30 2\n 1 2", "output": "25\n \n\nWe can unlock all the boxes by purchasing the first and second keys, at the\ncost of 25 yen, which is the minimum cost required.\n\n* * *"}, {"input": "12 1\n 100000 1\n 2", "output": "-1\n \n\nWe cannot unlock all the boxes.\n\n* * *"}, {"input": "4 6\n 67786 3\n 1 3 4\n 3497 1\n 2\n 44908 3\n 2 3 4\n 2156 3\n 2 3 4\n 26230 1\n 2\n 86918 1\n 3", "output": "69942"}] |
Print 1, if $A$ contains all elements of $B$, otherwise 0. | s672189174 | Accepted | p02452 | The input is given in the following format.
$n$
$a_0 \; a_1 \; ,..., \; a_{n-1}$
$m$
$b_0 \; b_1 \; ,..., \; b_{m-1}$
The number of elements in $A$ and its elements $a_i$ are given in the first
and second lines respectively. The number of elements in $B$ and its elements
$b_i$ are given in the third and fourth lines respectively. | input()
a = set(input().split())
input()
b = set(input().split())
print(1 if len(b - a) == 0 else 0)
| Includes
For given two sequneces $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and $B = \\{b_0,
b_1, ..., b_{m-1}\\}$, determine whether all elements of $B$ are included in
$A$. Note that, elements of $A$ and $B$ are sorted by ascending order
respectively. | [{"input": "4\n 1 2 3 4\n 2\n 2 4", "output": "1"}, {"input": "4\n 1 2 3 4\n 3\n 1 2 5", "output": "0"}] |
If the objective is achievable, print `Yes`; if it is not, print `No`.
* * * | s530300429 | Accepted | p03490 | Input is given from Standard Input in the following format:
s
x y | import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque
# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")
##### stdin ####
def LM(t, r):
return list(map(t, r))
def R():
return stdin.readline()
def RS():
return R().split()
def I():
return int(R())
def F():
return float(R())
def LI():
return LM(int, RS())
def LF():
return LM(float, RS())
def ONE_SL():
return list(input())
def ONE_IL():
return LM(int, ONE_SL())
def ALL_I():
return map(int, stdin)
def ALL_IL():
return LM(int, stdin)
##### tools #####
def ap(f):
return f.append
def pll(li):
print("\n".join(LM(str, li)))
def pljoin(li, s):
print(s.join(li))
##### main #####
def main():
ss = input().split("T")
x, y = map(int, input().split())
dpx = dict()
dpy = dict()
dpx[0 + len(ss[0])] = True
dpy[0] = True
direct = 1 # 0:x, 1:y
for s in ss[1:]:
tmp = dict()
n = len(s)
if direct % 2 == 0:
for k, v in dpx.items():
tmp[k + n] = True
tmp[k - n] = True
dpx = tmp
else:
for k, v in dpy.items():
tmp[k + n] = True
tmp[k - n] = True
dpy = tmp
# print(dpx, dpy)
direct += 1
if x in dpx and y in dpy:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| Statement
A robot is put at the origin in a two-dimensional plane. Initially, the robot
is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the
following two kinds of letters, and will be executed in order from front to
back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the
instructions are executed. Determine whether this objective is achievable. | [{"input": "FTFFTFFF\n 4 2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FTFFTFFF\n -2 -2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning clockwise in the first\n`T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "TF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "FFTTFF\n 0 0", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning counterclockwise in the second `T`.\n\n* * *"}, {"input": "TTTT\n 1 0", "output": "No"}] |
If the objective is achievable, print `Yes`; if it is not, print `No`.
* * * | s227470942 | Wrong Answer | p03490 | Input is given from Standard Input in the following format:
s
x y | s = input().strip()
Xs = list(map(int, input().split()))
bs = [[], []]
bb = t = 0
for c in s:
if c == "T":
bs[bb].append(t)
bb = 1 - bb
t = 0
else:
t += 1
bs[bb].append(t)
for i in range(2):
t = 1 << 8000
for x in bs[i]:
t = (t << x) | (t >> x)
if not (t >> (Xs[i] + 8000)) % 2:
print("No")
break
else:
print("Yes")
| Statement
A robot is put at the origin in a two-dimensional plane. Initially, the robot
is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the
following two kinds of letters, and will be executed in order from front to
back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the
instructions are executed. Determine whether this objective is achievable. | [{"input": "FTFFTFFF\n 4 2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FTFFTFFF\n -2 -2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning clockwise in the first\n`T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "TF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "FFTTFF\n 0 0", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning counterclockwise in the second `T`.\n\n* * *"}, {"input": "TTTT\n 1 0", "output": "No"}] |
If the objective is achievable, print `Yes`; if it is not, print `No`.
* * * | s853979928 | Runtime Error | p03490 | Input is given from Standard Input in the following format:
s
x y | hoge
| Statement
A robot is put at the origin in a two-dimensional plane. Initially, the robot
is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the
following two kinds of letters, and will be executed in order from front to
back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the
instructions are executed. Determine whether this objective is achievable. | [{"input": "FTFFTFFF\n 4 2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FTFFTFFF\n -2 -2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning clockwise in the first\n`T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "TF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "FFTTFF\n 0 0", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning counterclockwise in the second `T`.\n\n* * *"}, {"input": "TTTT\n 1 0", "output": "No"}] |
If the objective is achievable, print `Yes`; if it is not, print `No`.
* * * | s632733242 | Wrong Answer | p03490 | Input is given from Standard Input in the following format:
s
x y | print("Yes")
| Statement
A robot is put at the origin in a two-dimensional plane. Initially, the robot
is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the
following two kinds of letters, and will be executed in order from front to
back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the
instructions are executed. Determine whether this objective is achievable. | [{"input": "FTFFTFFF\n 4 2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FTFFTFFF\n -2 -2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning clockwise in the first\n`T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "TF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "FFTTFF\n 0 0", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning counterclockwise in the second `T`.\n\n* * *"}, {"input": "TTTT\n 1 0", "output": "No"}] |
If the objective is achievable, print `Yes`; if it is not, print `No`.
* * * | s825423518 | Runtime Error | p03490 | Input is given from Standard Input in the following format:
s
x y | s = input()
x, y = (int(i) for i in input().split())
while s:
if s[0] == "F":
x -= 1
if len(s) != 1:
s = s[1:]
else:
s = ""
def pm(L):
DP =
for i in range
for i in range(len(s)):
pass
| Statement
A robot is put at the origin in a two-dimensional plane. Initially, the robot
is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the
following two kinds of letters, and will be executed in order from front to
back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the
instructions are executed. Determine whether this objective is achievable. | [{"input": "FTFFTFFF\n 4 2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FTFFTFFF\n -2 -2", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning clockwise in the first\n`T` and turning clockwise in the second `T`.\n\n* * *"}, {"input": "FF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "TF\n 1 0", "output": "No\n \n\n* * *"}, {"input": "FFTTFF\n 0 0", "output": "Yes\n \n\nThe objective can be achieved by, for example, turning counterclockwise in the\nfirst `T` and turning counterclockwise in the second `T`.\n\n* * *"}, {"input": "TTTT\n 1 0", "output": "No"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s582534860 | Accepted | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | dic, link = (dict() for i in range(2))
n = int(input())
se = set(int(x) for x in range(n))
for i in range(n):
a, b, c = map(int, input().split())
link[a] = list()
for bc in [b, c]:
link[a].append(bc)
se.discard(bc)
rt = se.pop()
pre_odr, in_odr, pst_odr = ([] for i in range(3))
def dfs_pr(u):
pre_odr.append(u)
for e in link[u]:
if e >= 0:
dfs_pr(e)
def dfs_in(u):
for i in range(2):
if i < len(link[u]):
if i != 0 and u not in used:
in_odr.append(u)
used.add(u)
e = link[u][i]
if e < 0:
continue
dfs_in(e)
if e not in used:
in_odr.append(e)
used.add(e)
def dfs_ps(u):
for e in link[u]:
if e >= 0:
dfs_ps(e)
pst_odr.append(u)
print("Preorder")
dfs_pr(rt)
print("", *pre_odr)
print("Inorder")
used = set()
dfs_in(rt)
if len(in_odr) == 0:
in_odr.append(0)
print("", *in_odr)
print("Postorder")
dfs_ps(rt)
print("", *pst_odr)
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s950695177 | Accepted | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | # 1_7_C
N = int(input())
preorder_list = []
inorder_list = []
postorder_list = []
rooted_tree = [{} for i in range(N)]
for _ in range(N):
row = input()
id, left, right = list(map(int, row.split()))
rooted_tree[id]["id"] = id
rooted_tree[id]["right"] = right
rooted_tree[id]["left"] = left
# 子供のparentを左右で設定する。
if left > -1:
left_child_id = left
rooted_tree[left_child_id]["parent"] = id
if right > -1:
right_child_id = right
rooted_tree[right_child_id]["parent"] = id
def preParse(node):
preorder_list.append(node["id"])
if node["left"] != -1:
preParse(rooted_tree[node["left"]])
if node["right"] != -1:
preParse(rooted_tree[node["right"]])
def inParse(node):
if node["left"] != -1:
inParse(rooted_tree[node["left"]])
inorder_list.append(node["id"])
if node["right"] != -1:
inParse(rooted_tree[node["right"]])
def postParse(node):
if node["left"] != -1:
postParse(rooted_tree[node["left"]])
if node["right"] != -1:
postParse(rooted_tree[node["right"]])
postorder_list.append(node["id"])
def get_parent(node):
parent = -1
if "parent" in node:
return node["parent"]
return parent
for node in rooted_tree:
if get_parent(node) == -1:
root_id = node["id"]
break
print("Preorder")
preParse(rooted_tree[root_id])
print(" " + " ".join(map(str, preorder_list)))
# print(*preorder_list)
print("Inorder")
inParse(rooted_tree[root_id])
# print(*inorder_list)
print(" " + " ".join(map(str, inorder_list)))
print("Postorder")
postParse(rooted_tree[root_id])
# print(*postorder_list)
print(" " + " ".join(map(str, postorder_list)))
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s331455779 | Wrong Answer | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | def Pre(i):
if dali[i].node != -1:
ret = dali[i].node
dali[i].node = -1
order.append(ret)
if dali[i].left != -1:
Pre(dali[i].left)
if dali[i].right != -1:
Pre(dali[i].right)
def Pre2(u):
if u == -1:
return
order.append(u)
Pre2(dali[u].left)
Pre2(dali[u].right)
def In(i):
if dali2[i].left != -1:
In(dali2[i].left)
if dali2[i].node != -1:
ret = dali2[i].node
dali2[i].node = -1
order.append(ret)
if dali2[i].right != -1:
In(dali2[i].right)
def In2(u):
if u == -1:
return
In2(dali2[u].left)
order.append(u)
In2(dali2[u].right)
def Postorder(u):
if u == -1:
return None
Postorder(dali3[u].left)
Postorder(dali3[u].right)
order.append(dali3[u].node)
dali3[u].node = -1
class BiTree:
def __init__(self, node, left, right):
self.node = node
self.left = left
self.right = right
def inputdata(i):
node, left, right = list(map(int, input().split()))
dali[node] = BiTree(node, left, right)
dali2[node] = BiTree(node, left, right)
dali3[node] = BiTree(node, left, right)
def getParent(i):
for k in range(n):
if (dali[k].right == dali[i].node) or (dali[k].left == dali[i].node):
dali[i].parent = dali[k].node
return dali[i].parent
dali[i].parent = -1
def getParent3(i):
for k in range(n):
if (dali3[k].right == dali3[i].node) or (dali3[k].left == dali3[i].node):
dali3[i].parent = dali3[k].node
return dali3[i].parent
dali3[i].parent = -1
# Getting Root
n = int(input())
dali = [0] * n
dali2 = [0] * n
dali3 = [0] * n
order = []
for i in range(n):
inputdata(i)
"""
for i in range(n):
Pre(dali[i].node)
"""
for i in range(n):
for j in range(n):
if i == dali[j].left or i == dali[j].right:
break
if j == n - 1:
root = i
break
order = []
for i in range(n):
getParent(dali2[i].node)
for i in range(n):
getParent(dali[i].node)
for i in range(n):
getParent3(dali3[i].node)
Pre2(root)
print("Preoeder")
print(" ", end="")
print(*order)
order = []
In2(root)
print("Inorder")
print(" ", end="")
print(*order)
order = []
print("Postorder")
print(" ", end="")
Postorder(root)
print(*order)
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s939698139 | Wrong Answer | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | # coding:utf-8
N = int(input())
T = [None for i in range(N)]
class Tree:
def __init__(self, ID, p=-1):
self.ID = ID
def makeTree(self, tree):
ID = tree.ID
if ID == -1:
return
else:
for i in range(N):
if trees[i][0] == ID:
[ID, l, r] = trees[i]
tree.l = Tree(l)
tree.r = Tree(r)
treeL = tree.l
treeR = tree.r
treeL.p = tree
treeR.p = tree
self.makeTree(self, treeL)
self.makeTree(self, treeR)
def preParse(tree):
ID = tree.ID
if ID == -1:
return
else:
global preList
preList.append(ID)
treeL = tree.l
treeR = tree.r
preParse(treeL)
preParse(treeR)
def inParse(tree):
ID = tree.ID
if ID == -1:
return
else:
global inList
treeL = tree.l
treeR = tree.r
inParse(treeL)
inList.append(ID)
inParse(treeR)
def posParse(tree):
ID = tree.ID
if ID == -1:
return
else:
global posList
treeL = tree.l
treeR = tree.r
posParse(treeL)
posParse(treeR)
posList.append(ID)
trees = [list(map(int, input().split())) for i in range(N)]
ID = trees[0][0]
tree = Tree(ID)
Tree.makeTree(Tree, tree)
preList = []
inList = []
posList = []
preParse(tree)
inParse(tree)
posParse(tree)
def convert(List, order):
a = " " + " ".join([str(num) for num in List])
print("{}".format(order))
print(a)
convert(preList, "Preorder")
convert(inList, "Inorder")
convert(posList, "Postorder")
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s662853430 | Accepted | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | # -*- coding: utf-8 -*-
"""
Created on Sat Feb 23 19:42:05 2019
@author: Yamazaki Kenichi
"""
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
tree = [[-1, -1, -1] for i in range(N)]
for i in range(N):
tree[A[i][0]][1], tree[A[i][0]][2] = A[i][1], A[i][2]
if A[i][1] != -1:
tree[A[i][1]][0] = A[i][0]
if A[i][2] != -1:
tree[A[i][2]][0] = A[i][0]
def parent(u):
return tree[u][0]
def lchild(u):
return tree[u][1]
def rchild(u):
return tree[u][2]
def depth(u):
res = 0
while parent(u) != -1:
res += 1
u = parent(u)
return res
H = [-1 for i in range(N)]
def h(u):
hl = h(lchild(u)) + 1 if lchild(u) != -1 else 0
hr = h(rchild(u)) + 1 if rchild(u) != -1 else 0
H[u] = max(hl, hr)
return max(hl, hr)
for i in range(N):
if parent(i) == -1:
k = i
ans_pre = []
def pre(u):
if u != -1:
ans_pre.append(u)
if lchild(u) != -1:
pre(lchild(u))
if rchild(u) != -1:
pre(rchild(u))
pre(k)
ans_ino = []
def ino(u):
if lchild(u) != -1:
ino(lchild(u))
if u != -1:
ans_ino.append(u)
if rchild(u) != -1:
ino(rchild(u))
ino(k)
ans_pos = []
def pos(u):
if lchild(u) != -1:
pos(lchild(u))
if rchild(u) != -1:
pos(rchild(u))
if u != -1:
ans_pos.append(u)
pos(k)
print("Preorder")
print(" " + " ".join(map(str, ans_pre)))
print("Inorder")
print(" " + " ".join(map(str, ans_ino)))
print("Postorder")
print(" " + " ".join(map(str, ans_pos)))
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
In the 1st line, print "Preorder", and in the 2nd line print a list of node
IDs obtained by the preorder tree walk.
In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs
obtained by the inorder tree walk.
In the 5th line, print "Postorder", and in the 6th line print a list of node
IDs obtained by the postorder tree walk.
Print a space character before each node ID. | s511626339 | Accepted | p02281 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ linen, the information of each node is given in the following
format:
_id left right_
_id_ is the node ID, _left_ is ID of the left child and _right_ is ID of the
right child. If the node does not have the left (right) child, the
_left_(_right_) is indicated by -1 | class BinaryTree:
class Node:
def __init__(self, num, left_child, right_child):
self.id = num
self.left_child = left_child
self.right_child = right_child
def __init__(self, n):
self.nodes = [None] * n
self.root_id = int(n * (n - 1) / 2)
def add_node(self, i_s):
num, left_child, right_child = map(int, i_s.split())
self.nodes[num] = BinaryTree.Node(num, left_child, right_child)
if left_child != -1:
self.root_id -= left_child
if right_child != -1:
self.root_id -= right_child
def preorder_walk(self):
print("Preorder")
root_node = self.nodes[self.root_id]
def _pre_walk(node):
print(" {0}".format(node.id), end="")
if node.left_child != -1:
_pre_walk(self.nodes[node.left_child])
if node.right_child != -1:
_pre_walk(self.nodes[node.right_child])
_pre_walk(root_node)
print("")
def inorder_walk(self):
print("Inorder")
root_node = self.nodes[self.root_id]
def _in_walk(node):
if node.left_child != -1:
_in_walk(self.nodes[node.left_child])
print(" {0}".format(node.id), end="")
if node.right_child != -1:
_in_walk(self.nodes[node.right_child])
_in_walk(root_node)
print("")
def postorder_walk(self):
print("Postorder")
root_node = self.nodes[self.root_id]
def _post_walk(node):
if node.left_child != -1:
_post_walk(self.nodes[node.left_child])
if node.right_child != -1:
_post_walk(self.nodes[node.right_child])
print(" {0}".format(node.id), end="")
_post_walk(root_node)
print("")
n = int(input())
BT = BinaryTree(n)
for i in range(n):
BT.add_node(input())
BT.preorder_walk()
BT.inorder_walk()
BT.postorder_walk()
| Tree Walk
Binary trees are defined recursively. A binary tree _T_ is a structure defined
on a finite set of nodes that either
* contains no nodes, or
* is composed of three disjoint sets of nodes:
\- a root node.
\- a binary tree called its left subtree.
\- a binary tree called its right subtree.
Your task is to write a program which perform tree walks (systematically
traverse all nodes in a tree) based on the following algorithms:
1. Print the root, the left subtree and right subtree (preorder).
2. Print the left subtree, the root and right subtree (inorder).
3. Print the left subtree, right subtree and the root (postorder).
Here, the given binary tree consists of _n_ nodes and evey node has a unique
ID from 0 to _n_ -1. | [{"input": "9\n 0 1 4\n 1 2 3\n 2 -1 -1\n 3 -1 -1\n 4 5 8\n 5 6 7\n 6 -1 -1\n 7 -1 -1\n 8 -1 -1", "output": "Preorder\n 0 1 2 3 4 5 6 7 8\n Inorder\n 2 1 3 0 6 5 7 4 8\n Postorder\n 2 3 1 6 7 5 8 4 0\n \n\n"}] |
For each dataset, output a line that contains the width and the height of the
piece of artwork as decimal numbers, separated by a space. Each line should
not contain any other characters. | s707533189 | Accepted | p00746 | The input consists of a number of datasets. Each dataset represents the way
Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
> n1 d1
> n2 d2
> ...
> nN-1 dN-1
>
The first line contains the number of squares (= N) used to make the piece of
artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions.
The line "ni di" indicates placement of the square numbered i (≤ N-1). The
rules of numbering squares are as follows. The first square is numbered
"zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that
the input does not give any placement instruction to the first square, which
is numbered zero.
A square placement instruction for the square numbered i, namely "ni di",
directs it to be placed next to the one that is numbered ni, towards the
direction given by di, which denotes leftward (= 0), downward (= 1), rightward
(= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in
**Sample Input** are depicted below. Squares are labeled by their numbers.

The end of the input is indicated by a line that contains a single zero. | while 1:
N = int(input())
if N == 0:
break
tile = [[0, 0]]
xma, xmi, yma, ymi = 0, 0, 0, 0
for i in range(N - 1):
n, d = map(int, input().split())
if d == 0:
tile.append([tile[n][0] - 1, tile[n][1]])
xmi = min(xmi, tile[n][0] - 1)
elif d == 1:
tile.append([tile[n][0], tile[n][1] - 1])
ymi = min(ymi, tile[n][1] - 1)
elif d == 2:
tile.append([tile[n][0] + 1, tile[n][1]])
xma = max(xma, tile[n][0] + 1)
elif d == 3:
tile.append([tile[n][0], tile[n][1] + 1])
yma = max(yma, tile[n][1] + 1)
print(xma - xmi + 1, yma - ymi + 1)
| A: Pablo Squarson's Headache
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo
Squarson is "Squares". Today we are visiting his studio to see how his
masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many,
many squares of the same size. Pablo Squarson puts one of the squares on the
table. Then he places some other squares on the table in sequence. It seems
his methodical nature forces him to place each square side by side to the one
that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with
it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to
find a box that fits the new piece of work but he has trouble figuring out its
size. Let's help him!
Your mission is to write a program that takes instructions that record how
Pablo made a piece of his artwork and computes its width and height. It is
known that the size of each square is 1. You may assume that Pablo does not
put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his
head, is grumbling "My square style does not seem to be understood by
illiterates".
 | [] |
For each dataset, output a line that contains the width and the height of the
piece of artwork as decimal numbers, separated by a space. Each line should
not contain any other characters. | s975478197 | Wrong Answer | p00746 | The input consists of a number of datasets. Each dataset represents the way
Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
> n1 d1
> n2 d2
> ...
> nN-1 dN-1
>
The first line contains the number of squares (= N) used to make the piece of
artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions.
The line "ni di" indicates placement of the square numbered i (≤ N-1). The
rules of numbering squares are as follows. The first square is numbered
"zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that
the input does not give any placement instruction to the first square, which
is numbered zero.
A square placement instruction for the square numbered i, namely "ni di",
directs it to be placed next to the one that is numbered ni, towards the
direction given by di, which denotes leftward (= 0), downward (= 1), rightward
(= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in
**Sample Input** are depicted below. Squares are labeled by their numbers.

The end of the input is indicated by a line that contains a single zero. | while True:
n = int(input())
if n == 0:
break
w, h = 1, 1
edges = [[0], [0], [0], [0]] # [left, bottom, right, top]
for i in range(1, n):
p, d = map(int, input().split())
if d == 0 or d == 2:
if p in edges[d]:
w += 1
edges[d] = [i]
if p in edges[1]:
edges[1].append(i)
if p in edges[3]:
edges[3].append(i)
elif d == 1 or d == 3:
if p in edges[d]:
h += 1
edges[d] = [i]
if p in edges[0]:
edges[0].append(i)
if p in edges[2]:
edges[2].append(i)
# print(edges)
print(w, h)
| A: Pablo Squarson's Headache
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo
Squarson is "Squares". Today we are visiting his studio to see how his
masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many,
many squares of the same size. Pablo Squarson puts one of the squares on the
table. Then he places some other squares on the table in sequence. It seems
his methodical nature forces him to place each square side by side to the one
that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with
it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to
find a box that fits the new piece of work but he has trouble figuring out its
size. Let's help him!
Your mission is to write a program that takes instructions that record how
Pablo made a piece of his artwork and computes its width and height. It is
known that the size of each square is 1. You may assume that Pablo does not
put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his
head, is grumbling "My square style does not seem to be understood by
illiterates".
 | [] |
In case the answer exists, print the answer in the following format:
x_1 y_1
:
x_N y_N
Here (x_i, y_i) are the coordinates of the i-th vertex.
In case the answer doesn't exist, print a single `-1`.
* * * | s880906366 | Runtime Error | p03886 | The input is given from Standard Input in the following format:
N
a_1
:
a_N | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
from heapq import heappop, heapify
N, *A = map(int, read().split())
n90 = sum(x == 90 for x in A)
if n90 - (N - n90) != 4:
print(-1)
exit()
x = 0
temp = list(itertools.accumulate(1 if x == 90 else -1 for x in A))
slide = temp.index(min(temp)) + 1
A = A[slide:] + A[:slide]
def F(left_node, right_node, R, depth):
step = 1 << depth
# L:左に曲がるインデックス集合
# R:右に曲がるインデックスのヒープ
if not R:
n1, n2, n3, n4 = [i for i, x in enumerate(left_node) if x is not None]
X = [None] * N
Y = [None] * N
X[n1] = step
Y[n1] = 0
X[n2] = step
Y[n2] = step
X[n3] = 0
Y[n3] = step
X[n4] = 0
Y[n4] = 0
return X, Y
r = heappop(R)
l = left_node[r]
# l番:90度、r番:270度 を消し飛ばす
ll = left_node[l]
rr = right_node[r]
left_node[rr] = ll
right_node[ll] = rr
left_node[l] = None
left_node[r] = None
right_node[l] = None
right_node[r] = None
X, Y = F(left_node, right_node, R, depth + 1)
# 90,270を追加する
dx = X[rr] - X[ll]
dy = Y[rr] - Y[ll]
if dx > 0:
Y[rr] += step
X[l] = X[rr] - step
Y[l] = Y[ll]
X[r] = X[l]
Y[r] = Y[rr]
elif dx < 0:
Y[rr] -= step
X[l] = X[rr] + step
Y[l] = Y[ll]
X[r] = X[l]
Y[r] = Y[rr]
elif dy > 0:
X[rr] -= step
X[l] = X[ll]
Y[l] = Y[rr] - step
X[r] = X[rr]
Y[r] = Y[l]
elif dy < 0:
X[rr] += step
X[l] = X[ll]
Y[l] = Y[rr] + step
X[r] = X[rr]
Y[r] = Y[l]
return X, Y
R = [i for i, x in enumerate(A) if x == 270]
heapify(R)
X, Y = F(list(range(-1, N - 1)), list(range(1, N + 1)), R, 0)
# 最初にずらしていた分
X = X[N - slide :] + X[: N - slide]
Y = Y[N - slide :] + Y[: N - slide]
# 最後に座圧して完成
x_to_i = {x: i for i, x in enumerate(sorted(set(X)))}
y_to_i = {y: i for i, y in enumerate(sorted(set(Y)))}
X = [x_to_i[x] for x in X]
Y = [y_to_i[y] for y in Y]
print("\n".join("{} {}".format(x, y) for x, y in zip(X, Y)))
| Statement
Construct an N-gon that satisfies the following conditions:
* The polygon is simple (see notes for the definition).
* Each edge of the polygon is parallel to one of the coordinate axes.
* Each coordinate is an integer between 0 and 10^9, inclusive.
* The vertices are numbered 1 through N in counter-clockwise order.
* The internal angle at the i-th vertex is exactly a_i degrees.
In case there are multiple possible answers, you can output any. | [{"input": "8\n 90\n 90\n 270\n 90\n 90\n 90\n 270\n 90", "output": "0 0\n 2 0\n 2 1\n 3 1\n 3 2\n 1 2\n 1 1\n 0 1\n \n\n* * *"}, {"input": "3\n 90\n 90\n 90", "output": "-1"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s586528928 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | from copy import deepcopy
from itertools import product
class EulerTour:
def __init__(self, N):
self.N = N
self.G = [[] for _ in range(N)]
def add_edge(self, a, b):
self.G[a].append(b)
self.G[b].append(a)
def from_graph(self, G, copy=False):
if copy:
self.G = deepcopy(G)
else:
self.G = G
def build(self, root=0, lca=True):
self.root = root
self._dfs(root)
self.In = [0] * self.N
self.Out = [0] * self.N
for i, v in enumerate(self.node_list):
if self.In[v] == 0:
self.In[v] = i
self.Out[v] = i
if lca:
self.seg = SegTree(len(self.node_list))
self.seg.build(
[self.d[self.node_list[i]] for i in range(len(self.node_list))]
)
def lca(self, a, b):
if self.root in (a, b):
return self.root
a_out, b_in = self.Out[a], self.In[b]
if a_out > b_in:
a_out, b_in = self.Out[b], self.In[a]
if a_out > b_in:
a_out, b_in = self.Out[b], self.Out[a]
if a_out > b_in:
a_out, b_in = self.Out[a], self.Out[b]
idx = self.seg.find(a_out, b_in + 1)
return self.node_list[idx]
def _dfs(self, root=0):
self.idx2edge = {}
self.edge2idx = {}
self.node_list = []
self.edge_list = []
seen = [0] * len(self.G)
prev = [0] * len(self.G)
self.d = [-1] * len(self.G)
todo = [~root, root]
count = 0
while todo:
a = todo.pop()
if a >= 0:
seen[a] = 1
self.d[a] = self.d[prev[a]] + 1
edge = (prev[a], a)
self.idx2edge[count] = edge
self.edge2idx[edge] = count
self.edge_list.append(count)
self.node_list.append(a)
count += 1
for b in reversed(self.G[a]):
if seen[b]:
continue
todo.append(~b)
todo.append(b)
prev[b] = a
else:
edge = (prev[~a], ~a)
idx = self.edge2idx[edge]
self.edge_list.append(-idx)
if ~a != root:
self.node_list.append(prev[~a])
class SegTree:
"""segment tree with point modification and range product."""
# # https://yukicoder.me/submissions/452850
def __init__(self, N, data_f=min, data_unit=1 << 30):
self.N = N
self.data_f = data_f
self.data_unit = data_unit
self.data = [self.data_unit] * (N + N)
def build(self, raw_data):
data = self.data
f = self.data_f
N = self.N
data[N:] = raw_data[:]
for i in range(N - 1, 0, -1):
data[i] = f(data[i << 1], data[i << 1 | 1])
def set_val(self, i, x):
data = self.data
f = self.data_f
i += self.N
data[i] = x
while i > 1:
data[i >> 1] = f(data[i], data[i ^ 1])
i >>= 1
def fold(self, L, R):
"""compute for [L, R)"""
vL = vR = self.data_unit
data = self.data
f = self.data_f
L += self.N
R += self.N
while L < R:
if L & 1:
vL = f(vL, data[L])
L += 1
if R & 1:
R -= 1
vR = f(data[R], vR)
L >>= 1
R >>= 1
return f(vL, vR)
def find(self, L, R):
x = self.fold(L, R)
while R - L > 1:
M = (R + L) // 2
if self.fold(L, M) == x:
R = M
else:
L = M
return L
from collections import deque
def bfs(G, s, edge2idx):
seen = [0] * N
todo = deque()
seen[s] = 1
todo.append(s)
cumedge = [set() for _ in range(N)]
while len(todo):
a = todo.popleft()
for b in G[a]:
if seen[b] == 0:
seen[b] = 1
todo.append(b)
cumedge[b] = deepcopy(cumedge[a])
idx = edge2idx[(a, b)]
cumedge[b].add(idx)
return cumedge
N = int(input())
et = EulerTour(N)
for i in range(N - 1):
a, b = map(int, input().split())
et.add_edge(a - 1, b - 1)
et.build(0)
cumedge = bfs(et.G, 0, et.edge2idx)
M = int(input())
path_list = [0] * M
for i in range(M):
u, v = map(int, input().split())
u, v = u - 1, v - 1
w = et.lca(u, v)
path_list[i] = (cumedge[u] - cumedge[w]) | (cumedge[v] - cumedge[w])
ans = 2 ** (N - 1)
for i, p in enumerate(product(range(2), repeat=M)):
if i == 0:
continue
union = set.union(*[path_list[i] for i in range(M) if p[i]])
ans += (-1) ** sum(p) * 2 ** (N - 1 - len(union))
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s365400192 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | from itertools import combinations
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
m = int(input())
uv = [list(map(int, input().split())) for _ in range(m)]
adj = [[] for _ in range(n)]
ids = [[-1] * n for _ in range(n)]
for i, (a, b) in enumerate(ab):
a -= 1
b -= 1
adj[a].append(b)
adj[b].append(a)
ids[a][b] = i
ids[b][a] = i
def dfs(s, g):
d = [-1] * n
d[s] = 0
p = [-1] * n
stack = [s]
while stack:
u = stack.pop()
for v in adj[u]:
if d[v] == -1:
d[v] = d[u] + 1
p[v] = u
stack.append(v)
v = g
ret = 0
while v != s:
i = ids[v][p[v]]
ret += 1 << i
v = p[v]
return ret
def bin_count(tpl):
sm_or = 0
for e in tpl:
sm_or |= e
return bin(sm_or).count("1")
path = [dfs(u - 1, v - 1) for u, v in uv]
ex = 0
for i in range(1, m + 1):
for tpl in combinations(path, i):
ex += pow(2, n - 1 - bin_count(tpl)) * (-1) ** (i % 2 + 1)
ans = pow(2, n - 1) - ex
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s467514078 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | n = int(input())
ab = {i: [] for i in range(n)}
for j in range(n - 1):
a, b = [int(i) - 1 for i in input().split()]
ab[a].append((b, j))
ab[b].append((a, j))
def dfs(a, b, d=[], c=-1):
if a == b:
return d
for i in ab[a]:
if i[0] == c:
continue
e = dfs(i[0], b, d + [i[1]], a)
if len(e) != 0:
return set(e)
return set()
m = int(input())
ms = []
for _ in range(m):
u, p = [int(i) - 1 for i in input().split()]
ms.append(dfs(u, p))
ans = 0
for i in range(1 << m):
k = 0
a = set()
for j in range(m):
if (i >> j) & 1 == 1:
k += 1
a |= ms[j]
if k % 2 == 1:
ans += 2 ** (n - 1 - len(a))
elif k != 0:
ans -= 2 ** (n - 1 - len(a))
print(2 ** (n - 1) - ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s429686677 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | n = int(input())
g = [[] for _ in range(n)]
e = []
for _ in range(n - 1):
a, b = map(int, input().split())
a, b = min(a, b), max(a, b)
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
e.append((a - 1, b - 1))
m = int(input())
l = [set() for _ in range(m)]
for i in range(m):
u, v = map(int, input().split())
s = [u - 1]
dist = [-1] * n
dist[u - 1] = 0
r = [-1] * n
while s:
d = s.pop()
for node in g[d]:
if dist[node] == -1:
s.append(node)
dist[node] = dist[d] + 1
r[node] = d
d = v - 1
while r[d] != -1:
a, b = min(d, r[d]), max(d, r[d])
l[i].add(e.index((a, b)))
d = r[d]
ans = 0
for i in range(2**m):
cnt = 0
s = set()
for j in range(m):
if (i >> j) & 1:
cnt += 1
s |= l[j]
if cnt % 2:
ans -= 2 ** (n - len(s) - 1)
else:
ans += 2 ** (n - len(s) - 1)
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s373697272 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | """
包除原理
すべての条件を満たしている
2^{N-1}から「どれかの条件を満たしていない」を引けば良い
i番目の条件を満たしていない確率はかんたんに計算できる
Sに属していいないやつが全て条件を満たしていない確率については
Sに対応する辺が全て白となる確率を考えれば良い
"""
from collections import deque
N = int(input())
G = [[] for i in range(N)]
a = [0 for i in range(N - 1)]
b = [0 for i in range(N - 1)]
edges = dict()
for i in range(N - 1):
a[i], b[i] = map(int, input().split())
a[i] -= 1
b[i] -= 1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
edges[(min(a[i], b[i]), max(a[i], b[i]))] = i
M = int(input())
u = [0 for i in range(M)]
v = [0 for i in range(M)]
for i in range(M):
u[i], v[i] = map(int, input().split())
u[i] -= 1
v[i] -= 1
root = [0 for i in range(M)]
for i in range(M):
q = deque([u[i]])
parents = [-1 for i in range(N)]
reached = [0 for i in range(N)]
while len(q) > 0:
r = q.popleft()
reached[r] = 1
for p in G[r]:
if reached[p] == 1:
continue
parents[p] = r
q.append(p)
x = v[i]
while 1:
if parents[x] == -1:
break
s = min(x, parents[x])
t = max(x, parents[x])
root[i] += 2 ** edges[(s, t)]
x = parents[x]
ans = 0
def cnt(x):
ans = 0
for i in range(M):
if x | (2**i) == x:
ans += 1
return ans
q = deque([])
q.append(0)
S = [0 for i in range(2**M)]
reached = [0 for i in range(2**M)]
while len(q) > 0:
i = q.popleft()
for j in range(M):
if i & (2**j) == 0:
S[i | (2**j)] = S[i] | root[j]
if reached[i | (2**j)] == 0:
q.append(i | (2**j))
reached[i | (2**j)] = 1
ans += (-1) ** (cnt(i)) * 2 ** (N - bin(S[i]).count("1") - 1)
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s165315294 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | import sys
readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)
def main():
from collections import defaultdict, deque
from itertools import combinations
N = int(readline())
edge = defaultdict(list)
for i in range(N - 1):
a, b = map(int, readline().split())
edge[a - 1].append((b - 1, i))
edge[b - 1].append((a - 1, i))
def bfs(s, g):
que = deque()
que.append((s, []))
visited = [False] * N
visited[s] = True
while que:
cur, path = que.popleft()
if cur == g:
return set(path)
for nx, i in edge[cur]:
if not visited[nx]:
new_path = path + [i]
que.append((nx, new_path))
visited[nx] = True
M = int(readline())
constraints = []
for _ in range(M):
u, v = map(int, readline().split())
u -= 1
v -= 1
constraints.append(bfs(u, v))
cnt = 0
for i in range(1, M + 1):
sign = 1 if i % 2 == 1 else -1
for per in combinations(range(M), r=i):
incl = set()
for x in per:
incl = incl.union(constraints[x])
cnt += sign * 2 ** ((N - 1) - len(incl))
print(2 ** (N - 1) - cnt)
if __name__ == "__main__":
main()
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s025101382 | Wrong Answer | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # 多分TLE
from scipy.sparse.csgraph import dijkstra
from itertools import combinations
N = int(input())
edge_id = {}
D = [[-float("inf")] * N for i in range(N)]
for i in range(N - 1):
a, b = map(int, input().split())
D[a - 1][b - 1] = D[b - 1][a - 1] = 1
edge_id[(a - 1, b - 1)] = i
edge_id[(b - 1, a - 1)] = i
paths = dijkstra(D, return_predecessors=True)[1]
M = int(input())
edges = [0] * M
for i in range(M):
u, v = map(int, input().split())
now, p = paths[u - 1][v - 1], v - 1
edges[i] += 1 << edge_id[(now, p)]
while now != u - 1:
now, p = paths[u - 1][now], now
edges[i] += 1 << edge_id[(now, p)]
ans = pow(2, N - 1)
for m in range(1, M + 1):
coeff = -1
for comb in combinations(edges, m):
e = 0
for c in comb:
e |= c
ans += coeff * pow(2, N - 1 - bin(e).count("1"))
coeff *= -1
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s461299160 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | def main():
from itertools import combinations
n = int(input())
ab = [tuple(sorted(list(map(int, input().split())))) for _ in [0] * (n - 1)]
m = int(input())
uv = [list(map(int, input().split())) for _ in [0] * m]
g = [[] for _ in [0] * n]
[g[a - 1].append(b - 1) for a, b in ab]
[g[b - 1].append(a - 1) for a, b in ab]
d = dict()
for i, (a, b) in enumerate(ab):
d[(a - 1, b - 1)] = i
def tree_path(root, goal):
d = [-1] * n # 根からの距離
d[root] = 0
q = [root]
cnt = 0
while q: # BFS
cnt += 1
qq = []
while q:
i = q.pop()
for j in g[i]:
if d[j] == -1:
d[j] = cnt
qq.append(j)
q = qq
now = goal
path = [goal]
while True:
dist = d[now]
for i in g[now]:
if d[i] < dist:
now = i
path.append(i)
break
if dist == 1:
break
return path
seiyaku = [set() for _ in [0] * m]
for i, (u, v) in enumerate(uv):
path = tree_path(u - 1, v - 1)
for j in range(len(path) - 1):
x, y = min(path[j], path[j + 1]), max(path[j], path[j + 1])
seiyaku[i].add(d[(x, y)])
cnt = 2 ** (n - 1)
for i in range(1, m + 1):
flag = 1
if i % 2 == 1:
flag = -1
cc = combinations(list(range(m)), i)
for c in cc:
s = set()
for j in c:
s |= seiyaku[j]
cnt += (2 ** (n - 1 - len(s))) * flag
print(cnt)
main()
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s935271617 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # from itertools import combinations
# nは頂点数。lは[頂点,頂点]がn-1個入ったリスト。Tree(n, l)としてobjectを生成。
class Tree:
def __init__(self, n, l):
self.n = n
# 隣接する頂点のリストが格納された辞書
self.w = {}
for i in range(n - 1):
a, b, path = l[i]
if a in self.w:
self.w[a].append((b, path))
else:
self.w[a] = [(b, path)]
if b in self.w:
self.w[b].append((a, path))
else:
self.w[b] = [(a, path)]
# rootを根とした時の、各頂点の一つ上の親の頂点が格納された辞書を返す。
def make_parents_list(self, root):
self.parents = {}
que = []
seen = {}
for i in range(1, n + 1):
seen[i] = 0
self.parents[root] = (None, None)
seen[root] = 1
que.append(root)
while len(que) != 0:
node = que.pop()
for k in range(len(self.w[node])):
node_2, path_2 = self.w[node][k]
if seen[node_2] == 0:
seen[node_2] = 1
que.append(node_2)
self.parents[node_2] = (node, path_2)
else:
continue
return self.parents
# 頂点uと頂点vの最小共通祖先(LCA)を求める。
def lca(self, root, u, v):
ancestors = set()
if self.parents is None:
parents = self.make_parents_list(root)
else:
parents = self.parents
parent = u
while parent is not None:
ancestors.add(parent)
parent = parents[parent][0]
node = v
while node not in ancestors:
node = parents[node][0]
return node
n = int(input())
l = []
for i in range(n - 1):
a, b = [int(_) for _ in input().split()]
l.append((a, b, i))
tree = Tree(n, l)
seiyaku = []
parents = tree.make_parents_list(1)
m = int(input())
for i in range(m):
u, v = [int(_) for _ in input().split()]
parent_node = tree.lca(1, u, v)
s = set()
node = u
while node != parent_node:
s.add(parents[node][1])
node = parents[node][0]
node = v
while node != parent_node:
s.add(parents[node][1])
node = parents[node][0]
seiyaku.append(s)
ans = 2 ** (n - 1)
for i in range(1, 2**m):
i_bin = format(i, "0" + str(m) + "b")
k = 0
q = []
for j in range(m):
if i_bin[j] == "1":
q.append(j)
k += 1
else:
continue
s = set()
for x in q:
s |= seiyaku[x]
ans += (-1) ** k * 2 ** (n - 1 - len(s))
# print('*****************')
# print('i_bin = ' ,i_bin)
# print('k = ', k)
# print('q = ', q)
# print('s = ', s)
# print('len(s) = ', len(s))
# print('(-1) ** k * 2 ** (n - 1 - len(s)) = ', (-1) ** k * 2 ** (n - 1 - len(s)))
# print('ans = ', ans)
print(ans)
# print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# cnt = 2**(n-1)
# f = 0
# for i in range(1, m+1):
# flag = 1
# if i % 2 == 1:
# flag = -1
# cc = combinations(list(range(m)), i)
# for c in cc:
# s = set()
# for j in c:
# s |= seiyaku[j]
# cnt += (2**(n-1-len(s)))*flag
# f += 1
# print('*****************')
# print('c = ' ,c)
# print('flag = ', flag)
# print('s = ', s)
# print('len(s) = ', len(s))
# print('(2**(n-1-len(s)))*flag = ', (2**(n-1-len(s)))*flag)
# print('cnt = ', cnt)
# print(cnt)
# print('f = ', f)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s546834578 | Wrong Answer | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # from collections import deque,defaultdict
printn = lambda x: print(x, end="")
inn = lambda: int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda: input().strip()
DBG = True and False
BIG = 10**18
R = 10**9 + 7
# R = 998244353
def ddprint(x):
if DBG:
print(x)
def popcnt(x):
c = 0
while x > 0:
c += x % 2
x >>= 1
return c
def dfs(u, v, p):
global d
d[u][v] = p
for x in dst[v]:
if u != x and d[u][x] == 0:
p |= 1 << dst[v][x]
dfs(u, x, p)
p ^= 1 << dst[v][x]
n = inn()
dst = [{} for i in range(n + 1)]
for i in range(n - 1):
a, b = inm()
dst[a][b] = dst[b][a] = i
m = inn()
u = []
v = []
for i in range(m):
uu, vv = inm()
u.append(uu)
v.append(vv)
if True and n == 2 and m == 1:
print(0)
exit()
d = [[0] * (n + 1) for i in range(n + 1)]
for i in range(1, n + 1):
dfs(i, i, 0)
dd = []
for i in range(m):
dd.append(d[u[i]][v[i]])
ddprint(dst)
ddprint(d)
ddprint(u)
ddprint(v)
ddprint(dd)
y = 0
for i in range(1, 2**m):
nbits = x = 0
for j in range(m):
if (i >> j) % 2 > 0:
x |= dd[j]
nbits += 1
nsegs = popcnt(x)
s = 2 * (nbits % 2) - 1
y += s * 2 ** (n - 1 - nsegs)
print(2 ** (n - 1) - y)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s465948071 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def MI1():
return map(int1, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
def dfs(u, v, pu=-1):
if u == v:
return True
for cu in to[u]:
if cu == pu:
continue
if dfs(cu, v, u):
path.append(uvtoi[u, cu])
return True
return False
to = defaultdict(list)
n = II()
uvtoi = {}
for i in range(n - 1):
a, b = MI1()
to[a].append(b)
to[b].append(a)
uvtoi[a, b] = i
uvtoi[b, a] = i
m = II()
itoj = defaultdict(list)
for j in range(m):
u, v = MI1()
path = []
dfs(u, v)
# print(path)
for i in path:
itoj[i].append(j)
# print(itoj)
en = len(itoj)
dp = [[0] * (1 << m) for _ in range(en + 1)]
dp[0][0] = 1
for i, jj in enumerate(itoj.values()):
mask = sum(1 << j for j in jj)
for bit in range(1 << m):
pre = dp[i][bit]
if pre == 0:
continue
dp[i + 1][bit | mask] += pre
dp[i + 1][bit] += pre
# print(dp)
ans = dp[-1][-1] * pow(2, n - 1 - en)
print(ans)
main()
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s737233955 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | import sys
input = sys.stdin.buffer.readline
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
M = int(input())
UV = [list(map(int, input().split())) for _ in range(M)]
def popcnt(n):
c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555)
c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333)
c = (c & 0x0F0F0F0F0F0F0F0F) + ((c >> 4) & 0x0F0F0F0F0F0F0F0F)
c = (c & 0x00FF00FF00FF00FF) + ((c >> 8) & 0x00FF00FF00FF00FF)
c = (c & 0x0000FFFF0000FFFF) + ((c >> 16) & 0x0000FFFF0000FFFF)
c = (c & 0x00000000FFFFFFFF) + ((c >> 32) & 0x00000000FFFFFFFF)
return c
graph = [[] for _ in range(N + 1)]
for a, b in AB:
graph[a].append(b)
graph[b].append(a)
def make_tree(graph, root) -> (list, list):
INF = 10**15
dist = [INF] * len(graph)
parent = [-1] * len(graph)
stack = [root]
dist[root] = 0
parent[root] = root
while stack:
s = stack.pop()
for g in graph[s]:
if dist[g] > dist[s] + 1:
dist[g] = dist[s] + 1
parent[g] = s
stack.append(g)
return dist, parent
def lca(u, v, dist, parent):
distu, distv = dist[u], dist[v]
if distu > distv:
for _ in range(distu - distv):
u = parent[u]
if distv > distu:
for _ in range(distv - distu):
v = parent[v]
while u != v:
u, v = parent[u], parent[v]
return u
dist, parent = make_tree(graph, 1)
tree_lca = []
for i in range(M):
tmp = 0
u, v = UV[i]
l = lca(u, v, dist, parent)
while u != l:
tmp |= 1 << u
u = parent[u]
while v != l:
tmp |= 1 << v
v = parent[v]
tree_lca.append(tmp)
answer = 2 ** (N - 1)
for i in range(1, 2**M):
tmp = 0
for j in range(M):
if (i >> j) & 1:
tmp |= tree_lca[j]
cnt = popcnt(tmp)
if popcnt(i) % 2 == 1:
answer -= 2 ** (N - 1 - cnt)
else:
answer += 2 ** (N - 1 - cnt)
print(answer)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s523927119 | Runtime Error | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # 入力
N = int(input())
ab = []
# 頂点1,3,4が直接つながっている時G[1]=[3,4],G[3]=[1,4],G[4]=[1,3]となるグラフ表現
G = [[] for i in range(N + 1)]
for i in range(N - 1):
ab.append(list(map(int, input().split())))
G[ab[i][0]].append(ab[i][1])
G[ab[i][1]].append(ab[i][0])
M = int(input())
print(M)
ans = 2 ** (N - 1)
for k in range(M):
u, v = map(int, input().split())
start = u
goal = v
# ノードが探索済みか否かを判別するリスト
# used[6]=1ならばノード6は探索済み
used = [0 for y in range(N + 1)]
d = [100 for y in range(N + 1)]
queue = []
queue.append(start)
d[start] = 0
print(queue)
while len(queue) > 0:
# キューからノードを取り出して、探索済みにする
num = queue.pop(0)
used[num] = 1
# 現在のノードから伸びている枝の数だけ探索
for j in range(1, len(G[num]) + 1):
# 探索するノードが未探索かの判定
if used[G[num][j]] == 0:
d[G[num][j]] = d[num] + 1
queue.append(G[num][j])
if G[num][j] == goal:
break
break
ans -= 2 ** d[goal - 1]
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s910071493 | Wrong Answer | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | from collections import deque, defaultdict
N = int(input())
G = [[] for _ in range(N)]
for i in range(N - 1):
a, b = map(int, input().split())
G[a - 1].append(b - 1)
G[b - 1].append(a - 1)
def bfs(s):
seen = [0] * N
d = [0] * N
prev = [0] * N
todo = deque()
seen[s] = 1
todo.append(s)
while len(todo):
a = todo.popleft()
for b in G[a]:
if seen[b] == 0:
seen[b] = 1
todo.append(b)
d[b] += d[a] + 1
prev[b] = a
return d, prev
M = int(input())
path_list = [0] * M
for i in range(M):
u, v = map(int, input().split())
u -= 1
v -= 1
d, prev = bfs(u)
path = [v]
for j in range(d[v]):
prev_v = prev[v]
path.append(prev_v)
v = prev_v
path_list[i] = path
count = defaultdict(int)
for path in path_list:
for i in range(len(path) - 1):
a, b = path[i], path[i + 1]
a, b = sorted((a, b))
count[(a, b)] += 1
print(count)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s242475748 | Wrong Answer | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # -*- coding: utf-8 -*-
from fractions import gcd
from functools import reduce
# functions
def lcm(x, y):
if x % y == 0:
return x
elif y % x == 0:
return y
else:
return (x * y) // gcd(x, y)
# const
THRESHOLED = 10**9 + 7
# input
N = int(input())
nums = list(map(int, input().split()))
# calculate lcm_num
lcm_num = reduce(lcm, nums)
# make answer
ans = 0
for n in nums:
ans += lcm_num // n
if ans >= THRESHOLED:
ans = ans % THRESHOLED
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s633617300 | Wrong Answer | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | n = int(input())
edges = [list(map(int, input().split())) for _ in range(n - 1)]
m = int(input())
mm = [list(map(int, input().split())) for _ in range(m)]
dp = [(1 << m) * [0] for _ in range(n)]
for i in range(n):
dp[i][0] = 1
edge = [[] for _ in range(n)]
for a, b in edges:
edge[a - 1].append(b - 1)
edge[b - 1].append(a - 1)
tree = [-1] * n
Q = [0]
visited = {0}
cnt = 0
v = [[] for _ in range(n)]
vv = [0] * n
while Q:
P = []
for i in Q:
f = True
for j in edge[i]:
if j in visited:
continue
f = False
tree[j] = i
visited.add(j)
P.append(j)
v[cnt].append(i)
vv[i] = cnt
Q = P
cnt += 1
v = v[:cnt]
ms = [set() for _ in range(m)]
for i in range(m):
ma, mb = mm[i]
ma -= 1
mb -= 1
ms[i] = {ma, mb}
if vv[ma] < vv[mb]:
ma, mb = mb, ma
if vv[ma] > vv[mb]:
for j in range(vv[ma] - vv[mb]):
ma = tree[ma]
ms[i].add(ma)
while ma != mb:
ma = tree[ma]
mb = tree[mb]
ms[i] |= {ma, mb}
for c in range(len(v) - 1, 0, -1):
for i in v[c]:
if c == len(v) - 1:
dp[i][0] = 1
jj = 0
for j in range(m):
jj *= 2
if i in ms[j]:
jj += 1
for j in range(1 << m):
dp[tree[i]][j | jj] += dp[i][j]
ans = 0
jj = 0
for j in range(m):
jj *= 2
if 0 in ms[j]:
jj += 1
for j in range(1 << m):
if j | jj == (1 << m):
ans += dp[0][j]
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s086710241 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | import sys
input = sys.stdin.readline
n = int(input())
Edge = [[] for i in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
Edge[a - 1].append((b - 1, i))
Edge[b - 1].append((a - 1, i))
m = int(input())
M = []
for i in range(m):
M.append(tuple(map(int, input().split())))
inf = float("inf")
path = [-1] * n # 根から各点までのパスが通過する辺をbitで管理したもの
path[0] = 0
stack = [0]
while stack:
cur = stack.pop()
for next, num in Edge[cur]:
if path[next] == -1:
path[next] = path[cur] | (1 << num)
stack.append(next)
def popcount(x):
x = x - ((x >> 1) & 0x5555555555555555)
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F
x = x + (x >> 8)
x = x + (x >> 16)
x = x + (x >> 32)
return x & 0x0000007F
passing = [] # Mの各u,vについて、通過する辺をbitで持ったもの
for u, v in M:
passing.append((path[u - 1] ^ path[v - 1]))
ans = 2 ** (n - 1)
# Mのすべての部分集合について符号付で足し合わせる
for i in range(1, 2**m):
i_ct = popcount(i) # 満たすべき条件の数、これで符号を決める
condition = 0
for j in range(m):
if i & (1 << j): # j番目の条件を満たす
condition = condition | passing[j]
cond_ct = popcount(
condition
) # iが表すMの部分集合の条件をすべて満たす場合にすべて白になる辺集合の要素数
ans += pow((-1), i_ct) * pow(2, n - 1 - cond_ct)
print(ans)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the number of ways to paint the edges that satisfy all of the M
conditions.
* * * | s922726713 | Accepted | p02794 | Input is given from Standard Input in the following format:
N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M | # F - Tree and Constraints
# Read input
n = int(input())
adj = {a: [] for a in range(1, n + 1)}
for i in range(n - 1):
a, b = map(int, input().split())
adj[a].append((b, i))
adj[b].append((a, i))
m = int(input())
ranges = [tuple(map(int, input().split())) for i in range(m)]
# Subroutines
def findpath(u, v, parent=None):
"""uからvへの経路 (辺の集合)"""
if u == v:
return set()
for x, i in adj[u]:
if x == parent:
continue
p = findpath(x, v, u)
if p is not None:
p.add(i) # id of edge
return p
return None
def powerset(seq, bag):
"""[(seqの部分集合s, bag[e] for e in s の和集合)]"""
if len(seq) <= 1:
yield ((seq[0],), bag[seq[0]])
yield ((), set())
else:
for s, b in powerset(seq[1:], bag):
yield ((seq[0],) + s, b | bag[seq[0]])
yield (s, b)
# Find paths
paths = [findpath(u, v) for u, v in ranges]
# いずれかの区間をすべて白にする塗り方の個数
white = 0
for s, b in powerset(list(range(m)), paths):
l = len(s)
if l <= 0:
continue
# b中の辺をすべて白にする塗り方の個数
w = 2 ** (n - 1 - len(b))
# 包除原理
sign = -1 if l % 2 == 0 else 1
white += sign * w
# どの区間も黒を含む塗り方の個数
coloring = 2 ** (n - 1) - white
print(coloring)
| Statement
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i.
Consider painting each of these edges white or black. There are 2^{N-1} such
ways to paint the edges. Among them, how many satisfy all of the following M
restrictions?
* The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. | [{"input": "3\n 1 2\n 2 3\n 1\n 1 3", "output": "3\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively\npainted (white, black), (black, white), or (black, black), so the answer is 3.\n\n* * *"}, {"input": "2\n 1 2\n 1\n 1 2", "output": "1\n \n\nThe tree in this input is shown below: \n \n \n \nAll of the M restrictions will be satisfied only if Edge 1 is painted black,\nso the answer is 1.\n\n* * *"}, {"input": "5\n 1 2\n 3 2\n 3 4\n 5 3\n 3\n 1 3\n 2 4\n 2 5", "output": "9\n \n\nThe tree in this input is shown below: \n \n \n \n\n* * *"}, {"input": "8\n 1 2\n 2 3\n 4 3\n 2 5\n 6 3\n 6 7\n 8 6\n 5\n 2 7\n 3 5\n 1 6\n 2 8\n 7 8", "output": "62\n \n\nThe tree in this input is shown below: \n \n"}] |
Print the minimum possible total cost incurred.
* * * | s305695465 | Accepted | p03185 | Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N | class Lichaotree:
# 区間は1-indexed
# min
def __init__(self, X):
self.N = len(X)
self.N0 = 2 ** (self.N - 1).bit_length()
self.comp = {v: k for k, v in enumerate(X)}
self.inf = 10**19
self.X = X + [self.inf] * (self.N0 - self.N)
self.data = [None] * (2 * self.N0)
self.size = [0] * self.N0 + [1] * self.N0
for i in range(self.N0 - 1, 0, -1):
self.size[i] = 2 * self.size[2 * i]
def f(self, line, x):
return line[0] * x + line[1]
def _update(self, k, line):
if self.data[k] is None:
self.data[k] = line
return
l = k - (1 << (k.bit_length() - 1))
r = l + self.size[k]
while True:
if self.data[k] is None:
self.data[k] = line
return
m = (l + r) // 2
lx = self.X[l]
mx = self.X[m]
rx = self.X[r - 1]
gl = self.f(self.data[k], lx) > self.f(line, lx)
gm = self.f(self.data[k], mx) > self.f(line, mx)
gr = self.f(self.data[k], rx) > self.f(line, rx)
if gl and gr:
self.data[k] = line
return
if not gl and not gr:
return
if gm:
self.data[k], line = line, self.data[k]
gl = not gl
if gl:
r = m
k = 2 * k
else:
l = m
k = 2 * k + 1
def query(self, x):
k = self.comp[x] + self.N0
s = self.inf
while k > 0:
if self.data[k]:
s = min(s, self.f(self.data[k], x))
k = k >> 1
return s
def addline(self, line):
self._update(1, line)
N, C = map(int, input().split())
H = list(map(int, input().split()))
T = Lichaotree(H)
dp = [10**19] * N
dp[0] = 0
T.addline((-2 * H[0], H[0] ** 2))
for i in range(1, N):
mi = T.query(H[i])
dp[i] = mi + C + H[i] ** 2
T.addline((-2 * H[i], H[i] ** 2 + dp[i]))
print(dp[-1])
| Statement
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N),
the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following
action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N. | [{"input": "5 6\n 1 2 3 4 5", "output": "20\n \n\nIf we follow the path 1 \u2192 3 \u2192 5, the total cost incurred would be ((3 - 1)^2 +\n6) + ((5 - 3)^2 + 6) = 20.\n\n* * *"}, {"input": "2 1000000000000\n 500000 1000000", "output": "1250000000000\n \n\nThe answer may not fit into a 32-bit integer type.\n\n* * *"}, {"input": "8 5\n 1 3 4 5 10 11 12 13", "output": "62\n \n\nIf we follow the path 1 \u2192 2 \u2192 4 \u2192 5 \u2192 8, the total cost incurred would be ((3\n- 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s819633205 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | (*a,) = map(int, open(0).read().split())
print(a[1] + a[2] if a[0] > 1 else "Hello World")
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s301812390 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n = list(map(int, open(0).read().split()))
print((n[1] + n[2], "Hello World")[n[0] == 1])
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s875612979 | Wrong Answer | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World" if int(input()) == 1 else int(input()) * int(input()))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s743024485 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n= int(input())
if n = 1:
print("Hello World")
exit()
a = int(input())
b = int(input())
print(a+b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s606794103 | Wrong Answer | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n, *p = map(int, open(0))
print("Hello world" if n == 1 else sum(p))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s867337079 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | a = [int(input()) for i in range(3)]
N = a[0]
A = a[1]
B = a[2]
if N = 1:
print("Hello World")
elif:
print(A + B) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s222606040 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | N = int(input())
if N == 1
print("Hello World")
exit()
else:
A = int(input())
B = int(input())
print(A+B) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s067943945 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | ###############################################################################
from bisect import bisect_left as binl
from copy import copy, deepcopy
def intin():
input_tuple = input().split()
if len(input_tuple) <= 1:
return int(input_tuple[0])
return tuple(map(int, input_tuple))
def intina():
return [int(i) for i in input().split()]
def intinl(count):
return [intin() for _ in range(count)]
def modadd(x, y):
global mod
return (x + y) % mod
def modmlt(x, y):
global mod
return (x * sy) % mod
def lcm(x, y):
while y != 0:
z = x % y
x = y
y = z
return x
def make_linklist(xylist):
linklist = {}
for a, b in xylist:
linklist.setdefault(a, [])
linklist.setdefault(b, [])
linklist[a].append(b)
linklist[b].append(a)
return linklist
def calc_longest_distance(linklist, v=1):
distance_list = {}
distance_count = 0
distance = 0
vlist_previous = []
vlist = [v]
nodecount = len(linklist)
while distance_count < nodecount:
vlist_next = []
for v in vlist:
distance_list[v] = distance
distance_count += 1
vlist_next.extend(linklist[v])
distance += 1
vlist_to_del = vlist_previous
vlist_previous = vlist
vlist = list(set(vlist_next) - set(vlist_to_del))
max_distance = -1
max_v = None
for v, distance in distance_list.items():
if distance > max_distance:
max_distance = distance
max_v = v
return (max_distance, max_v)
def calc_tree_diameter(linklist, v=1):
_, u = calc_longest_distance(linklist, v)
distance, _ = calc_longest_distance(linklist, u)
return distance
###############################################################################
def main():
n = intin()
if n == 1:
print("Hello World")
else:
a = intin()
b = intin()
print(a + b)
if __name__ == "__main__":
main()
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s057818014 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | N = int(input())
if N == 1:
print('Hello World')
else if N == 2:
a = int(input())
b = int(input())
print(a + b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s545500514 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | import math
n, m = map(int, input().split())
for i in range(math.floor(m/n), 0, -1):
if m % i == 0:
ans = i
break
print(ans)
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s961826582 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | def soinsuubunkai(n):
i = 2
list = [1]
while i**2 <= n:
if n % i == 0:
list.append(i)
if i != n // i:
list.append(n // i)
i += 1
list.append(n)
return list
n, m = map(int, input().split())
l = soinsuubunkai(m)
r = len(l)
for i in range(r):
if m // l[r - i - 1] >= n:
print(l[r - i - 1])
quit()
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s018107235 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | N = int(input())
if N == 1:
print('Hello World')
else if N == 2:
a = input(input())
b = input(input())
print(a + b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s981369827 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n = int(input())
a = int(input())
b = int(input())
if n == 1:
print("Hello World")
else n == 2:
print(A+B) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s140326010 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n= int(input())
if n = 1:
print("Hello World")
elif n = 2:
a = int(input())
b = int(input())
print(a+b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s225882982 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n= int(input())
if n = 1:
print("Hello World")
exit()
else:
a = int(input())
b = int(input())
print(a+b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s724820569 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n = int(input())
if n == 2:
a = int(input())
b = int(input())
print(a + b)
else:
print('Hello World') | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s473186248 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n = int(input())
if n == 2:
a = int(input())
b = int(input())
print(a + b)
else:
print('Hello World') | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s888463409 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World" if int(input()) == 1 else (int(input()) + int(input())))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s125992611 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | a = list(map(int, open(0).read().split()))
print(sum(a) - 2 if a[0] == 2 else "Hello World")
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s171724537 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | I = lambda: int(input())
print(I() + I() if ~-I() else "Hello World")
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s233960854 | Accepted | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World" if input() == "1" else sum([int(input()) for i in range(2)]))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s709893824 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World" if print() == "1" else (int(input()) + int(input())))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s398800094 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n = int(input())
if n = 1:
print("Hello World")
else:
print(int(input())+int(input()))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s348811573 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World" if input() == "1" int(input()) + int(input()))
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s578592342 | Wrong Answer | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | a = input()
print("Hello World") if a == "1" else int(input()) + int(input())
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s165200934 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | n=int(input())
a=int(input())
b=int(input())
if n=1:
print('Hello World')
else:
print(a+b) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s404696407 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | if int(input())==1
print("Hello World")
else
print(int(input())+int(input())) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s038693107 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | N, A, B = map(int, input().split())
if N == 1;
print("Hallo World")
else print(A+B) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s491422794 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | N=int(input())
A=int(input())
B=int(input())
if N == 1;
print("Hallo World")
else print(A+B) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s890265623 | Runtime Error | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | if input() == '1':
print('Hello world')
else:
print(int(input())+int(input()))) | Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
If N=1, print `Hello World`; if N=2, print A+B.
* * * | s342794371 | Wrong Answer | p03238 | Input is given from Standard Input in one of the following formats:
1
2
A
B | print("Hello World")
| Statement
In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the
currency of Japan) has started a business in programming education.
One day, there was an exam where a one-year-old child must write a program
that prints `Hello World`, and a two-year-old child must write a program that
receives integers A, B and prints A+B.
Takahashi, who is taking this exam, suddenly forgets his age.
He decides to write a program that first receives his age N (1 or 2) as input,
then prints `Hello World` if N=1, and additionally receives integers A, B and
prints A+B if N=2.
Write this program for him. | [{"input": "1", "output": "Hello World\n \n\nAs N=1, Takahashi is one year old. Thus, we should print `Hello World`.\n\n* * *"}, {"input": "2\n 3\n 5", "output": "8\n \n\nAs N=2, Takahashi is two years old. Thus, we should print A+B, which is 8\nsince A=3 and B=5."}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.