text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
import collections
n = int(input())
x = list(map(int, input().split()))
x = list(enumerate(x, 1))
x.sort(key = lambda x: x[1])
li = []
dic = {j: i for i, j in x}
for i, j in x:
li += [i] * (i - 1)
for i, j in x:
li += [i] * (n - i)
li = collections.deque(li)
ans = []
cnt = [0 for i in range(n + 1)]
for i in range(1, n ** 2 + 1):
if i in dic:
m = dic[i]
if cnt[m] != m - 1:
print("No")
exit()
else:
m = li.popleft()
cnt[m] += 1
ans.append(m)
print("Yes")
print(*ans)
```
Yes
| 14,900 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = [(a[i], i) for i in range(N)]
b.sort()
res = [0] * pow(N, 2)
for i in range(N): res[a[i] - 1] = i + 1
#print(res, b)
for k in range(N):
i = b[k][1]
x = i
if i == 0: continue
for j in range(a[i] - 1):
if res[j]: continue
res[j] = i + 1
x -= 1
if x == 0: break
#print(res, i, a[i])
if x:
print("No")
exit(0)
#print(res)
#print(res)
for i in range(N):
x = N - i - 1
for j in range(a[i], N ** 2):
if res[j]: continue
res[j] = i + 1
x -= 1
if x == 0: break
#print(res)
if x:
print("No")
exit(0)
print("Yes")
print(*res)
```
Yes
| 14,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
N=int(input())
x=list(map(int,input().split()))
ans=[0]*(N**2)
x=[(x[i]-1,i+1) for i in range(N)]
x.sort()
for i in range(N):
p,v=x[i]
ans[p]=v
pos=0
for i in range(N):
val,count=x[i]
count-=1
while count:
while ans[pos]:
pos+=1
if pos>=val:
print("No")
exit()
else:
ans[pos]=x[i][1]
count-=1
pos=N**2-1
for i in range(N-1,-1,-1):
val,count=x[i]
count-=1
count=N-1-count
while count:
while ans[pos]:
pos-=1
if val>=pos:
print("No")
exit()
else:
ans[pos]=x[i][1]
count-=1
print("Yes")
print(*ans)
```
Yes
| 14,902 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
N = int(input())
X = sorted((x,i) for i,x in enumerate((int(x) for x in input().split()),1))
arr = []
for x,i in X:
arr += [i] * (i-1)
for x,i in X:
arr += [i] * (N-i)
x_to_i = dict(X)
arr = arr[::-1]
answer = []
counter = [0] * (N+1)
bl = True
for x in range(1,N*N+1):
if x in x_to_i:
i = x_to_i[x]
counter[i] += 1
bl &= (counter[i] == i)
else:
i = arr.pop()
counter[i] += 1
answer.append(i)
if bl:
print('Yes')
print(*answer)
else:
print('No')
```
Yes
| 14,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
n=int(input())
x=[]
xx=list(map(int,input().split()))
for i in range(n):x.append([xx[i],i+1])
x.sort()
ans=[0]*n*n
c=[n]*n
l=[0]*(n*n+1)
r=[0]*(n*n+1)
for t,i in x:r[t]=n-i
for i in range(n*n):
if r[i]!=0:
r[i+1]+=r[i]-1
if r[n]:exit(print("No"))
for t,i in x[::-1]:l[t-1]=i-1
for i in range(n*n,0,-1):
if l[i]!=0:
l[i-1]+=l[i]-1
if l[0]:exit(print("No"))
x+=[[10**10,10**10]]
ind=0
d=[]
for i in range(n*n):
if i==x[ind][0]:
d+=[x[ind][1]]*(n-x[ind][1])
ind+=1
if d:ans[i]=d.pop()
x=x[:-1][::-1]+[[-10,-10]]
ind=0
d=[]
for i in range(n*n,-1,-1):
if i==x[ind][0]:
ans[i-1]=x[ind][1]
d+=[x[ind][1]]*x[ind][1]
ind+=1
if d:ans[i-1]=d.pop()
print("Yes")
print(*ans)
```
No
| 14,904 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
import os
import sys
from collections import deque, defaultdict
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353
N = int(sys.stdin.buffer.readline())
X = list(map(int, sys.stdin.buffer.readline().split()))
ans = [None] * (N ** 2 + 1)
# 左にあるものから使う
P = list(sorted([(pos, i) for i, pos in enumerate(X, 1)]))
que = deque()
for p, i in P:
ans[p] = i
for _ in range(i - 1):
que.append(i)
for p in range(1, N ** 2 + 1):
if not que:
break
if ans[p] is not None:
i = ans[p]
for _ in range(N - i):
que.append(i)
else:
ans[p] = que.popleft()
def is_ok(ans, que):
if que:
return False
for a in ans[1:]:
if a is None:
return False
counts = defaultdict(int)
C = [0]
for a in ans[1:]:
counts[a] += 1
C.append(counts[a])
for i, x in enumerate(X, 1):
if C[x] != i:
return False
return True
if is_ok(ans, que):
print('Yes')
print(*ans[1:])
else:
print('No')
```
No
| 14,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def readln(ch):
_res = list(map(int,str(input()).split(ch)))
return _res
def construct(x):
a = sorted([(x[i] - 1) * 1000 + i for i in range(0,n)])
res = [0 for i in range(0,n) for j in range(0,n)]
r = 0
for i in range(0,n):
res[a[i] // 1000] = i + 1
while res[r] > 0 : r = r + 1
for i in range(0,n):
index = a[i] % 1000
x = a[i] // 1000
for j in range(0,index):
if r > x:
return [True]
res[r] = index + 1
while res[r] > 0: r = r + 1
for i in range(0,n):
for j in range(0,n - i - 1):
res[r] = i + 1
while r < n * n and res[r] > 0 :
r = r + 1
return [False,res]
n = int(input())
x = readln(' ')
res = construct(x)
if res[0]:
print('No')
else:
print('Yes')
print(' '.join(list(map(str,res[1]))))
```
No
| 14,906 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
* a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N.
* For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left.
Constraints
* 1 ≤ N ≤ 500
* 1 ≤ x_i ≤ N^2
* All x_i are distinct.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 ... x_N
Output
If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween.
Examples
Input
3
1 5 9
Output
Yes
1 1 1 2 2 2 3 3 3
Input
2
4 1
Output
No
Submitted Solution:
```
N = int(input())
ans = [-1] * (N ** 2)
X = [int(x)-1 for x in input().split()]
for i, x in enumerate(X):
ans[x] = i+1
left, right = 0, N ** 2 - 1
for i in range(N):
l_cnt, r_cnt = i, N - (i+1)
for _ in range(l_cnt):
while ans[left] != -1:
left += 1
ans[left] = i+1
for _ in range(r_cnt):
while ans[right] != -1:
right -= 1
ans[right] = i+1
counter = [[] for _ in range(N)]
for i, a in enumerate(ans):
counter[a-1].append(i)
if all(counter[i].index(x) == i for i, x in zip(range(N), X)):
print("Yes")
print(*ans)
else:
print("No")
assert(all(a != -1 for a in ans))
assert(all(len(row) == N for row in counter))
```
No
| 14,907 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if a[0] != 1:
ans += 1
child = [[] for _ in range(n)]
for i in range(1, n):
child[a[i]-1].append(i)
def dfs(x):
global ans
M = 0
for v in child[x]:
M = max(M, (dfs(v)+1)%k)
if M == k-1 and a[x] != 1 and x != 0:
ans += 1
return M
dfs(0)
print(ans)
```
| 14,908 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
import sys
sys.setrecursionlimit(200000)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if a[0] != 1:
a[0] = 1
ans += 1
b = [[]for i in range(n)]
for i in range(n):
b[a[i]-1].append(i)
b[0].remove(0)
huka = 0
kyo = [float("inf")] * n
def dfs(x,y):
kyo[x] = y
for i in b[x]:
dfs(i,y+1)
dfs(0,0)
def dfs2(x,y):
if kyo[x] <=k-y:
return
if y == k-1 and x != 0:
kyo[x] = 0
global ans
ans += 1
return
kyo[x] = 0
dfs2(a[x]-1,y+1)
hukai = []
for i in range(n):
hukai.append([kyo[i],i])
hukai.sort(key = lambda x:-x[0])
for j,i in hukai:
if j <= k:
continue
dfs2(i,0)
print(ans)
```
| 14,909 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n,k = LI()
a = LI_()
r = 0
if a[0] != 0:
r += 1
e = collections.defaultdict(set)
for i in range(1,n):
e[a[i]].add(i)
def f(i, p):
m = 0
r = 0
for c in e[i]:
ct,cr = f(c, i)
r += cr
if ct > m:
m = ct
if (m == k-1 and p != 0) or (m == k and p == 0):
r += 1
m = -1
return (m+1,r)
for c in e[0]:
m,t = f(c,0)
r += t
return r
print(main())
```
| 14,910 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
def examA():
A, B, C = LI()
ans = min(A*B*(C%2),A*C*(B%2),B*C*(A%2))
print(ans)
return
def examB():
class segment_():
def __init__(self, A, n, segfunc):
#####単位元######要設定0or1orinf
self.ide_ele = inf
####################
self.num = 1 << (n - 1).bit_length()
self.seg = [self.ide_ele] * 2 * self.num
self.segfunc = segfunc
# set_val
for i in range(n):
self.seg[i + self.num] = A[i]
# built
for i in range(self.num - 1, 0, -1):
self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])
def update(self, k, r):
k += self.num
self.seg[k] = r
while k:
k >>= 1
self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])
# 値xに1加算
def update1(self, k):
k += self.num
self.seg[k] += 1
while k:
k >>= 1
self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])
def updateneg1(self, k):
k += self.num
self.seg[k] -= 1
while k:
k >>= 1
self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])
def query(self, p, q):
if q < p:
return self.ide_ele
p += self.num;
q += self.num
res = self.ide_ele
while p < q:
if p & 1 == 1:
res = self.segfunc(res, self.seg[p])
p += 1
if q & 1 == 1:
q -= 1
res = self.segfunc(res, self.seg[q])
p >>= 1;
q >>= 1
return res
N, x = LI()
A = LI()
Seg_min = segment_(A, N, lambda a, b: min(a,b))
ans = inf
for k in range(N):
cur = 0
for j in range(N):
if j-k>=0:
now = Seg_min.query(j-k,j+1)
else:
now = min(Seg_min.query(0,j+1),Seg_min.query(N-(k-j),N))
# print(now,k,j)
cur += now
ans = min(ans,cur+k*x)
print(ans)
return
def examC():
H, W = LI()
A = [SI()for _ in range(H)]
ans = 0
print(ans)
return
def examD():
N, K = LI()
A = LI()
V = [[]for _ in range(N)]
ans = 0
if A[0]!=1:
ans += 1
A[0] = 1
for i in range(1,N):
V[A[i]-1].append(i)
def dfs(p,s):
depth = 0
cnt = 0
for i in V[s]:
d,c = dfs(s,i)
depth = max(depth,d)
cnt += c
depth += 1
if depth==K and p!=0:
depth = 0
cnt += 1
return depth,cnt
_,cnt = dfs(0,0)
ans += cnt
print(ans)
return
def examE():
ans = 0
print(ans)
return
def examF():
ans = 0
print(ans)
return
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]
sys.setrecursionlimit(10**6)
if __name__ == '__main__':
examD()
"""
"""
```
| 14,911 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
#d tree
#code自体はあっている。ただ、そもそもアルゴリズムが違う。
#下から調べていかないと最適でない。
#Right Answer.
import sys
sys.setrecursionlimit(2 * 10 ** 5 + 10)
n,k = [int(i) for i in input().split()]
a = [int(i)-1 for i in input().split()]
#this is directed tree so every node is not reached yet
#return the depth from its lowest bottom
def dfs(u):
global cnt
depth = 0 #default, this value is used when bottom
for v in edges[u]:
depth = max(depth, (dfs(v)+1)%k) #apply the maximum depth
#because the edge was alerady cut, by the following procedure
if depth==k-1 and u!=0 and a[u] != 0:
cnt += 1
return depth
cnt = 0
if a[0]!=0:
cnt += 1
edges = [[] for _ in range(n)]
for i in range(1,n):
pa = a[i]
edges[a[i]].append(i)
dfs(0)
print(cnt)
```
| 14,912 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
import sys
sys.setrecursionlimit(200000)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if a[0] != 1:
a[0] = 1
ans += 1
b = [[]for i in range(n)]
for i in range(1,n):
b[a[i]-1].append(i)
huka = 0
kyo = [float("inf")] * n
z =[[]for i in range(n)]
def dfs(x,y):
kyo[x] = y
z[y].append(x)
for i in b[x]:
dfs(i,y+1)
dfs(0,0)
def dfs2(x,y):
if kyo[x] <=k-y:
return
if y == k-1 and x != 0:
kyo[x] = 0
global ans
ans += 1
return
kyo[x] = 0
dfs2(a[x]-1,y+1)
for i in range(n-1,k,-1):
for j in z[i]:
dfs2(j,0)
print(ans)
```
| 14,913 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
from heapq import heappop,heappush
n,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
ans = 0
if(a[1] != 1):
ans += 1
links_c = [set() for _ in range(n+1)]
for i,ai in enumerate(a[2:],2):
links_c[ai].add(i)
depth = [-1] * (n+1)
depth[1] = 0
stack = [1]
hq = []
while(stack):
next = []
while(stack):
i = stack.pop()
for j in links_c[i]:
depth[j] = depth[i] + 1
heappush(hq, (depth[j]*-1,j))
next.append(j)
stack = next[::]
while( hq[0][0]*-1 > k ):
di,i = heappop(hq)
if( depth[i] <= k ):
continue
for j in range(k-1):
i = a[i]
ans += 1
links_c[a[i]].remove(i)
stack = [i]
while(stack):
next = []
while(stack):
i = stack.pop()
depth[i] = 1
for j in links_c[i]:
next.append(j)
stack = next[::]
print(ans)
'''
首都の自己ループを除くと、首都を根として、
子→親の有向木になっている。
・首都のテレポート先は首都でないとダメ
→ 長さ2以上の閉路を作るとうまくいかない
・首都以外は、首都までの距離をk以下にできればよい
もっとも根からの距離が遠い頂点xをとる。
xが根からの距離k以下 → 終了してOK
xが根からの距離k以上
→ xからk-1個離れた親を首都につなぎなおす
そして再度最も遠い頂点を探す、を繰り返す。
'''
```
| 14,914 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
"Correct Solution:
```
from collections import deque
N,K = map(int,input().split())
a = list(map(int,input().split()))
for i in range(N):
a[i] -= 1
ans = 0
if a[0] != 0:
ans += 1
a[0] = 0
dis = [float("-inf")] * N
lnum = [0] * N
for i in range(N):
lnum[a[i]] += 1
q = deque([])
for i in range(N):
if lnum[i] == 0:
q.append(i)
dis[i] = 0
while len(q) > 0:
now = q.popleft()
if a[now] == 0:
continue
elif dis[now] == K-1:
ans += 1
dis[now] = -1
dis[a[now]] = max(dis[a[now]] , dis[now] + 1)
lnum[a[now]] -= 1
if lnum[a[now]] == 0:
q.append(a[now])
#print (dis)
print (ans)
```
| 14,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
# import heapq as hp
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
graph = [[] for _ in range(N)]
Par = [-1]*N
for i, a in enumerate(A):
if i == 0:
if a != 1:
ans += 1
continue
Par[i] = a-1
graph[a-1].append(i)
qs = []
stack = [0]
Depth = [-1]*N
Depth[0] = 0
while stack:
p = stack.pop()
for np in graph[p]:
Depth[np] = Depth[p] + 1
stack.append(np)
qs.append((Depth[p], p))
qs.sort(reverse=True)
checked = [False]*N
for d, s in qs:
if d <= K: break
if checked[s]: continue
for _ in range(K-1):
s = Par[s]
que = [s]
checked[s] = True
while que:
qq = []
for p in que:
for np in graph[p]:
if not checked[np]:
checked[np] = True
qq.append(np)
que = qq
ans += 1
print(ans)
```
Yes
| 14,916 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
#設定
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
#ライブラリインポート
from collections import defaultdict
answer = 0
#入力受け取り
def getlist():
return list(map(int, input().split()))
INF = float("inf")
class Graph(object):
def __init__(self):
self.graph = defaultdict(list)
def __len__(self):
return len(self.graph)
def add_edge(self, a, b):
self.graph[a].append(b)
def get_nodes(self):
return self.graph.keys()
def DFS(G, visit, W, Wlist, node, K):
for i in G.graph[node]:
if visit[i] != "Yes":
visit[i] = "Yes"
DFS(G, visit, W, Wlist, i, K)
Wlist[node].append(W[i] + 1)
val = 0
for i in Wlist[node]:
if i == K and node != 0:
global answer
answer += 1
else:
val = max(val, i)
W[node] = val
#処理内容
def main():
N, K = getlist()
a = getlist()
G = Graph()
for i in range(1, N):
G.add_edge(i, a[i] - 1)
G.add_edge(a[i] - 1, i)
ans = 0
if a[0] != 1:
ans += 1
if K == 1:
for i in range(1, N):
if a[i] != 1:
ans += 1
print(ans)
return
#DFS
W = [0] * N
Wlist = [[] for i in range(N)]
visit = ["No"] * N
visit[0] = "Yes"
DFS(G, visit, W, Wlist, 0, K)
print(ans + answer)
if __name__ == '__main__':
main()
```
Yes
| 14,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
# coding:utf-8
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
def main():
n, k = LI()
A = LI_()
D = defaultdict(list)
for i in range(1, n):
D[A[i]].append(i)
# 首都は必ず自己ループでなければならない
global res
res = 0 if A[0] == 0 else 1
def DFS(v, par):
global res
h = 0
for to in D[v]:
# print('dfs {} -> {}'.format(v+1, to+1))
# h: 葉からの距離の最大値
h = max(h, DFS(to, v))
# 根が首都ではなく,高さがk - 1の場合は首都につなぎ直す
if h == k - 1 and par != 0:
res += 1
h = -1
# print('Finish {} <- {}'.format(par+1, v+1))
return h + 1
DFS(0, 0)
return res
print(main())
```
Yes
| 14,918 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
def DFS(graph,root,parent):
dist = [-1]*(n+1)
dist[root] = 0
stack = [root]
while stack:
x = stack.pop()
for y in graph[x]:
if dist[y] == -1:
parent[y] = x
dist[y] = dist[x]+1
stack.append(y)
return dist
#親のリスト、頂点v、kが与えられたときに「vのk個上の祖先」を見る
class Doubling:
def __init__(self,graph,root):
self.root = root
n = len(graph)-1
self.ancestor_ls = [[0]*(n+1)]
self.distance = DFS(graph,root,self.ancestor_ls[0])
self.bitn = n.bit_length()
for i in range(1,self.bitn+1):
ancestor_ls_app = [0]*(n+1)
for j in range(1,n+1):
ancestor_ls_app[j] = self.ancestor_ls[i-1][self.ancestor_ls[i-1][j]]
self.ancestor_ls.append(ancestor_ls_app)
def dist(self):
return self.distance
def parent(self):
return self.ancestor_ls[0]
def ancestor(self,v,depth):
if depth == 1:
return self.ancestor_ls[0][v]
if depth == 2:
return self.ancestor_ls[1][v]
if depth == 0:
return v
if n<depth:
return self.root
ret = v
for i in range(self.bitn):
if depth&1<<i:
ret = self.ancestor_ls[i][ret]
return ret
import sys
input = sys.stdin.readline
from collections import deque
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if a[0] != 1:
ans += 1
graph = [[] for i in range(n+1)]
for i in range(1,n):
graph[a[i]].append(i+1)
graph[i+1].append(a[i])
if k > n:
print(ans)
exit()
sol = Doubling(graph,1)
dist = sol.dist()
if k == 1:
print(n-dist.count(1)-dist.count(0)+ans)
exit()
dls = []
for i in range(1,n+1):
dls.append((dist[i],i))
vis = [0]*(n+1)
dls.sort()
dlsq = deque(dls)
parent = sol.parent()
while True:
d,x = dlsq.pop()
if d <= k:
break
if vis[x] == 1:
continue
ans += 1
anc = sol.ancestor(x,k-1)
stack = deque([anc])
while stack:
v = stack.pop()
vis[v] = 1
for u in graph[v]:
if parent[u] == v and vis[u] != 1:
stack.append(u)
print(ans)
```
Yes
| 14,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
if K >= N - 1:
print(int(a[0] != 1))
exit(0)
res = 0
if a[0] != 1:
a[0] = 1
res += 1
e = dd(set)
for i in range(N):
e[a[i]].add((i + 1, 0))
e[i + 1].add((a[i], 0))
#print(e)
Q = dq([1])
d = [float("inf")] * (N + 1)
d[1] = 0
while len(Q):
p = Q.popleft()
for q, _ in e[p]:
if d[q] > d[p] + 1:
d[q] = d[p] + 1
Q.append(q)
#print(d)
for u in range(1, N + 1):
x = -(-d[u] // K)
t = []
for v, _ in e[u]:
y = -(-d[v] // K)
if x != y:
if u == 1 or (v == 1): continue
t.append((v, 0))
for v, _ in t: e[u].discard((v, 0))
#print(e)
for i in range(1, N + 1):
e[i].add((1, 1))
e[1].add((i, 1))
#print(e)
import heapq
class prim:
def __init__(self, n, e):
self.e = e
self.n = n
def MSTcost(self):
h = []
visited = [0] * (self.n + 1)
ks = list(self.e.keys())
b = pow(10, 10)
for edge in self.e[ks[0]]:
heapq.heappush(h, edge[1] * b + edge[0])
res = 0
visited[ks[0]] = 1
while len(h):
p = heapq.heappop(h)
p0 = p // b
p1 = p % b
if visited[p1]: continue
visited[p1] = 1
for q in self.e[p1]:
if visited[q[0]]:
continue
heapq.heappush(h, q[1] * b + q[0])
res += p0
return res
pri = prim(N, e)
print(pri.MSTcost() + res)
```
No
| 14,920 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
if K >= N - 1:
print(int(a[0] != 1))
exit(0)
res = 0
if a[0] != 1:
a[0] = 1
res += 1
e = dd(set)
for i in range(N):
e[a[i]].add(i + 1)
#print(e)
Q = dq([1])
d = [float("inf")] * (N + 1)
d[1] = 0
while len(Q):
p = Q.popleft()
for q in e[p]:
if d[q] > d[p] + 1:
d[q] = d[p] + 1
Q.append(q)
#print(d)
eulbase = pow(10, 6)
euler = []
depth = [0] * (N + 1)
table = [0] * (N + 1)
def eulerdfs(x, d):
global euler
global depth
global table
table[x] = len(euler)
depth[x] = d
euler.append(d * eulbase + x)
for y in e[x]:
if x == y: continue
eulerdfs(y, d + 1)
euler.append(d * eulbase + x)
for i in range(len(euler) - 1, -1, -1): table[euler[i] % eulbase] = i
eulerdfs(1, 0)
#print(euler)
#print(table)
class minSegTree:
def segfunc(self, x, y):
return min(x, y)
def __init__(self, n, ide_ele, init_val):
#####単位元######
self.ide_ele = ide_ele
#num:n以上の最小の2のべき乗
self.num = 2 ** (n - 1).bit_length()
self.seg = [self.ide_ele] * 2 * self.num
#set_val
for i in range(n):
self.seg[i + self.num - 1] = init_val[i]
#built
for i in range(self.num - 2, -1, -1) :
self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])
def update(self, k, x):
k += self.num - 1
self.seg[k] = x
while k + 1:
k = (k - 1) // 2
self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
def query(self, p, q):
if q <= p:
return self.ide_ele
p += self.num - 1
q += self.num - 2
res = self.ide_ele
while q - p > 1:
if p & 1 == 0:
res = self.segfunc(res, self.seg[p])
if q & 1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = self.segfunc(res, self.seg[p])
else:
res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
return res
class maxSegTree:
def segfunc(self, x, y):
return max(x, y)
def __init__(self, n, ide_ele, init_val):
#####単位元######
self.ide_ele = ide_ele
#num:n以上の最小の2のべき乗
self.num = 2 ** (n - 1).bit_length()
self.seg = [self.ide_ele] * 2 * self.num
#set_val
for i in range(n):
self.seg[i + self.num - 1] = init_val[i]
#built
for i in range(self.num - 2, -1, -1) :
self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])
def update(self, k, x):
k += self.num - 1
self.seg[k] = x
while k + 1:
k = (k - 1) // 2
self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
def query(self, p, q):
if q <= p:
return self.ide_ele
p += self.num - 1
q += self.num - 2
res = self.ide_ele
while q - p > 1:
if p & 1 == 0:
res = self.segfunc(res, self.seg[p])
if q & 1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = self.segfunc(res, self.seg[p])
else:
res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
return res
euler.pop()
minseg = minSegTree(len(euler), N * eulbase, euler)
maxseg = maxSegTree(len(euler), 0, euler)
#print(euler)
ex = dd(list)
for i in range(len(euler)):
ok = i + 1
ng = len(euler) + 1
while ng - ok > 1:
m = (ok + ng) // 2
if (maxseg.query(i, m) // eulbase) - (minseg.query(i, m) // eulbase) <= K: ok = m
else: ng = m
ex[i].append((ok, 1))
for i in range(len(euler), 0, -1):
ex[i].append((i - 1, 0))
#print(ex)
import heapq
class dijkstra:
def __init__(self, n, e):
self.e = e
self.n = n
def path(self, s, t):
d = [float("inf")] * (self.n + 1)
vis = set()
d[s] = 0
h = [s]
while not t in vis and len(h):
v = heapq.heappop(h)
v1 = v % (10 ** 6)
v0 = v // (10 ** 6)
if v1 in vis: continue
vis.add(v1)
for p in self.e[v1]:
d[p[0]] = min(d[p[0]], d[v1] + p[1])
if p[0] in vis: continue
heapq.heappush(h, d[p[0]] * (10 ** 6) + p[0])
return d[t]
dij = dijkstra(len(euler), ex)
print(dij.path(0, len(euler)) - 1 + res)
```
No
| 14,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
if A[0] != 1:
ans += 1
T = [[] for _ in range(n+1)]
for i, a in enumerate(A[1:], 2):
T[a].append(i)
dist = [-1]*(n+1)
dist[1] = 0
stack = [1]
while stack:
v = stack.pop()
d = dist[v]+1
for nv in T[v]:
if d > k:
ans += 1
dist[nv] = 1
else:
dist[nv] = d
stack.append(nv)
print(ans)
```
No
| 14,922 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.
Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.
King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:
* Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.
Constraints
* 2≤N≤10^5
* 1≤a_i≤N
* One can get to the capital from any town by using the Teleporters some number of times.
* 1≤K≤10^9
Input
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N
Output
Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.
Examples
Input
3 1
2 3 1
Output
2
Input
4 2
1 1 2 2
Output
0
Input
8 2
4 1 2 3 1 2 3 4
Output
3
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**6)
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
edg = [[] for i in range(n)]
for i in range(n):
edg[a[i]].append(i)
ans = [0] * n
ans[0] = a[0] != 0
c = [1] * n
def dfs(now, rank):
c[now] = 0
for e in edg[now]:
if c[e]:
if rank == k:
ans[e] = 1
dfs(e, 1)
else:
dfs(e, time + 1)
dfs(0, 1)
print(sum(ans))
```
No
| 14,923 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a,b=(int(x) for x in input().split())
print("{:.6f}".format(a*b/3.305785))
```
| 14,924 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a,b=map(int,input().split())
x=3.305785
print((a*b)/x)
```
| 14,925 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a, b = map(int,input().split())
print(format(a*b/3.305785,'.5f'))
```
| 14,926 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a, b = map(int, input().split())
s=3.305785
print('{:.6f}'.format(a*b/s))
```
| 14,927 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
def test():
a,b=list(map(int,input().split()))
c=3.305785
s=a*b/c
return s
if __name__ == '__main__':
print(test())
```
| 14,928 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a,b=map(int,input().split())
print('{:.6f}'.format((a*b)/3.305785))
```
| 14,929 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a,b = map(float,input().split())
S = a*b/3.305785
print(S)
```
| 14,930 |
Provide a correct Python 3 solution for this coding contest problem.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
"Correct Solution:
```
a,b=map(int,input().split())
S=round(a*b/3.305785,6)
print(S)
```
| 14,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
a,b=input().split()
a=int(a)
b=int(b)
c=a*b/3.305785
print(f"{c:.6f}")
```
Yes
| 14,932 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
###
### atcorder test program
###
import sys
### math class
class math:
### pi
pi = 3.14159265358979323846264338
### GCD
def gcd(self, a, b):
if b == 0:
return a
return self.gcd(b, a%b)
### LCM
def lcm(self, a, b):
return (a*b)//self.gcd(a,b)
### Prime number search
def Pnum(self, a):
if a == 1: return False
for i in range(2,int(a**0.5)+1):
if a % i == 0:
return False
return True
### Circle area
def caria(self, r):
return r*r*self.pi
math = math()
### output class
class output:
### list
def list(self, l):
l = list(l)
print(" ", end="")
for i, num in enumerate(l):
print(num, end="")
if i != len(l)-1:
print(" ", end="")
print()
output = output()
### input sample
#i = input()
#A, B, C = [x for x in input().split()]
#inlist = [int(w) for w in input().split()]
#R = float(input())
#A = [int(x) for x in input().split()]
#for line in sys.stdin.readlines():
# x, y = [int(temp) for temp in line.split()]
### output sample
#print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
#print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
#print(" {}".format(i), end="")
#A, B, C = [int(x) for x in input().split()]
a, b = [float(x) for x in input().split()]
print(a*b/3.305785)
```
Yes
| 14,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
a, b = list(map(float, input().split()))
print(a*b / 3.305785)
```
Yes
| 14,934 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
a,b=map(float,input().split())
print(f'{(a*b/3.305785):.6f}')
```
Yes
| 14,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
def tsubo(x,y):
return (x * y) / 3.305785
S = tsubo(a,b)
print(S)
```
No
| 14,936 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
#coding:utf-8
a, b = map(int, raw_input(). split())
print(a * b / 3.305785)
```
No
| 14,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
def tsubo(a,b):
return (a * b) / 3.305785
tsubo(a,b)
```
No
| 14,938 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Have you ever heard of the unit "○○ tsubo" that expresses the area of land? Since ancient times, one samurai has said the area for making rice to eat in a day.
There is a land of a [m] x b [m]. Enter a and b and create a program that outputs the tsubo area S [tsubo] of the land. 1 tsubo = 3.305785 [m2], and a and b are integers less than or equal to 100.
input
a b
A and b separated by one space are given on one line.
output
Output the tsubo area S in one line. An error of 0.0001 or less is allowed.
Example
Input
15 25
Output
113.437508
Submitted Solution:
```
import sys
f = sys.stdin
a, b = map(int, f.readline().split())
print('{:.6f}'.format(a * b * 0.3025))
```
No
| 14,939 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while True:
a, b = input().split()
if a == "0" and b == "0":
break
a1 = 0
a2 = 0
for i in range(4):
if a[i] == b[i]:
a1 += 1
for i in range(4):
for j in range(4):
if a[i] == b[j]:
a2 += 1
print(a1, a2-a1)
```
| 14,940 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
for q in range(12000):
a, b = input().split()
if a[0] is '0' and b[0] is '0':
break
hit = 0
for i in range(4):
if a[i] is b[i]:
hit = hit + 1
blow = 0
for j in range(4):
for i in range(4):
if (b[j] is a[i]) and (a[i] is not b[i]) and (a[j] is not b[j]):
blow = blow + 1
print(hit, blow)
```
| 14,941 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while True:
r, a= input().split()
if r==a=='0': break
print(sum(1 for i, j in zip(r, a) if i==j), sum(1 for i in range(len(r)) for j in range(len(a)) if r[i]==a[j] and i!=j))
```
| 14,942 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while 1:
r, a = map(int, input().split())
if r == 0:
break
pro = list(str(r).zfill(4))
ans = list(str(a).zfill(4))
hit = 0
blow = 0
for p, a in zip(pro, ans):
if p == a:
hit += 1
elif a in pro:
blow += 1
print(hit, blow)
```
| 14,943 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while True :
a, b = map(int, input().split())
if a == 0 and b == 0 :
break
hit = 0
blow = 0
if a//1000 == b//1000 :
hit += 1
if (a%1000)//100 == (b%1000)//100 :
hit += 1
if (a%100)//10 == (b%100)//10 :
hit += 1
if a%10 == b%10 :
hit += 1
if a//1000 == (b%1000)//100 or a//1000 == (b%100)//10 or a//1000 == b%10 :
blow += 1
if (a%1000)//100 == b//1000 or (a%1000)//100 == (b%100)//10 or (a%1000)//100 == b%10 :
blow += 1
if (a%100)//10 == b//1000 or (a%100)//10 == (b%1000)//100 or (a%100)//10 == b%10 :
blow += 1
if a%10 == b//1000 or a%10 == (b%1000)//100 or a%10 == (b%100)//10 :
blow += 1
print(hit, blow)
```
| 14,944 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while True:
r, a = input().split()
if r == a == '0': break
h, b = 0, 0
for i in range(4):
if r[i] == a[i]:
h += 1
elif a[i] in r:
b += 1
print(h, b)
```
| 14,945 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
for q in range(12000):
a, b = input().split()
if a[0] is '0' and b[0] is '0':
break
hit = sum(1 for c, d in zip(a, b) if d is c)
blow = sum(1 for e in b if e in a) - hit
print(hit, blow)
```
| 14,946 |
Provide a correct Python 3 solution for this coding contest problem.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
"Correct Solution:
```
while 1:
a,b=input().split()
if a=='0':break
h=0
for i,j in zip(a,b):h+=i==j
print(h,len(set(a)&set(b))-h)
```
| 14,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0226
"""
import sys
from sys import stdin
input = stdin.readline
def solve(r, a):
hit = 0
blow = 0
for i in range(4):
if r[i] == a[i]:
hit += 1
else:
if a[i] in r:
blow += 1
return hit, blow
def main(args):
while True:
r, a = input().split()
if r == '0' and a == '0':
break
hit, blow = solve(r, a)
print(hit, blow)
if __name__ == '__main__':
main(sys.argv[1:])
```
Yes
| 14,948 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
while True:
s, t = input().split()
if s == "0" and t == "0":
break
S = [int(x) for x in list(s)]
T = [int(x) for x in list(t)]
hit = 0
blow = 0
for i in range(len(T)):
if T[i] in S:
blow += 1
if T[i] == S[i]:
blow -= 1
hit += 1
print(hit, blow)
```
Yes
| 14,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
while True:
a, b = input().split()
if a[0] is '0' and b[0] is '0':
break
hit = sum(1 for c, d in zip(a, b) if d is c)
blow = sum(1 for e in b if e in a) - hit
print(hit, blow)
```
Yes
| 14,950 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
while 1:
x,y = map(list,input().split())
if x == ['0'] and y== ['0']: break
h = b = 0
for i in range(len(x)):
if x[i] == y[i]:
h += 1
elif y[i] in x:
b += 1
print ('%d %d' % (h,b))
```
Yes
| 14,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
answer_list = []
while True:
r,a = list(input().split(" "))
if r == "0" and a == "0":
break
r_list = [int(s) for s in list(r)]
a_list = [int(s) for s in list(a)]
hit_count = 0
blow_count = 0
for i in range(0,len(a_list)):
if r_list[i] == a_list[i]:
hit_count += 1
a_list[i] = -1
r_list[i] = -1
while -1 in a_list:
a_list.remove(-1)
for i in range(0,len(a_list)):
if a_list[i] in r_list:
blow_count += 1
r_list[i] = -1
answer_list.append(str(hit_count) + " " + str(blow_count))
for ans in answer_list:
print(ans)
```
No
| 14,952 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
while True:
r, a= input().split()
if r==a==0: break
print(sum(1 for i, j in zip(r, a) if i==j), sum(1 for i in range(len(r)) for j in range(len(a)) if r[i]==a[j] and i!=j))
```
No
| 14,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
while True:
r,a = list(input().split(" "))
if r == "0" and a == "0":
break
r_list = [int(s) for s in list(r)]
a_list = [int(s) for s in list(a)]
hit_count = 0
blow_count = 0
for i in range(0,len(a_list)):
if r_list[i] == a_list[i]:
hit_count += 1
a_list[i] = -1
r_list[i] = -1
while -1 in a_list:
a_list.remove(-1)
for i in range(0,len(a_list)):
if a_list[i] in r_list:
blow_count += 1
r_list[i] = -1
print(hit_count,blow_count)
```
No
| 14,954 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako decided to play hit-and-blow. The hit-and-blow rules are as follows.
* Separated into questioners and respondents.
* The questioner decides a 4-digit number (correct answer) that does not include duplicate numbers.
* Respondents guess the 4-digit number (answer).
* For the answer, the questioner gives a hint by the number of hits and blows.
* Comparing the answer and the correct answer, the fact that both the number and the digit position are the same is called a hit, and the fact that only the number is the same but the digit position is different is called a blow. For example, if the correct answer is 1234 and the answer is 1354, the questioner gives the hint "2 hits, 1 blow" and repeats until the correct answer.
* The questioner and the respondent take turns playing the game, and the one who guesses the correct answer with fewer answers wins.
Taro and Hanako seem to find it a little annoying to judge the number of hits and the number of blows each time. For those two, let's create a program that instantly shows the number of hits and the number of blows.
Create a program that inputs the correct answer r and the answer a and outputs the number of hits and the number of blows. r and a are a sequence of four numbers, 0 to 9, respectively.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. For each dataset, r and a are given on one line, separated by blanks.
The number of datasets does not exceed 12000.
Output
Outputs the number of hits and the number of blows on one line for each input dataset.
Example
Input
1234 5678
1234 1354
1234 1234
1230 1023
0123 1234
0 0
Output
0 0
2 1
4 0
1 3
0 3
Submitted Solution:
```
for q in range(12000):
a = tuple(map(int, input().split()))
b = input()
if a[0] == 0 and b[0] == '0':
break
hit = 0
for i in range(4):
if a[i] == int(b[i]):
hit = hit + 1
blow = 0
for j in range(4):
for i in range(4):
if (int(b[j]) == a[i]) and (a[i] != int(b[i])) and (a[j] != int(b[j])):
blow = blow + 1
print(hit, blow)
```
No
| 14,955 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
H,A,B = map(int,input().split())
a = list(range(A,B+1))
x = 0
for i in a:
if H % i == 0:
x += 1
print(x)
```
| 14,956 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
H,A,B=map(int,input().split())
i=0
while A<=B:
if H%A==0:
i=i+1
A=A+1
print(i)
```
| 14,957 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
h,a,b = map(int,input().split())
print(len([i for i in range(a,b+1) if h%i==0]))
```
| 14,958 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
#標準入力とカウンタ変数の初期化
a,b,c = map(int,input().split())
kei = 0
#割り切れるならカウンタ変数を更新する
for i in range(b,c + 1):
if a % i == 0:kei += 1
#カウンタ変数を出力する
print(kei)
```
| 14,959 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
h, a, b = map(int, input().split())
print([(h%(a+c)==0) for c in range(b-a+1)].count(True))
```
| 14,960 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
H,a,b=map(int,input().split())
c=0
for i in range(a,b+1):
if H%i==0 :
c+=1
print(c)
```
| 14,961 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
H,A,B = [int(i) for i in input().split()]
cnt = 0
for i in range(A,B+1):
if (H // i)*i == H:
cnt = cnt + 1
print(cnt)
```
| 14,962 |
Provide a correct Python 3 solution for this coding contest problem.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
"Correct Solution:
```
H, A, B = map(int, input().split())
ans = 0
for i in range(A, B+1) :
if H % i == 0 :
ans += 1
print(ans)
```
| 14,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
H, A, B = map(int, input().split())
cnt = 0;
for i in range(A, B+1):
cnt += (H % i == 0 )
print(cnt)
```
Yes
| 14,964 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
h,a,b=map(int,input().split())
c=0
for i in range(a,b+1):
if h%i==0:
c=c+1
print(c)
```
Yes
| 14,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
h, a, b = map(int, input().split())
cnt = 0
for i in range(a, b+1):
if h % i == 0:
cnt += 1
print(cnt)
```
Yes
| 14,966 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
H,A,B=map(int,input().split())
c=0
for i in range(A,B+1):
if H%i==0:
c+=1
print(c)
```
Yes
| 14,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
H,a,b=map(int,input().split())
c=0
for i in range(a,b):
if H%i==0 :
c+=1
print(c)
```
No
| 14,968 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Our master carpenter is designing a condominium called Bange Hills Mansion. The condominium is constructed by stacking up floors of the same height. The height of each floor is designed so that the total height of the stacked floors coincides with the predetermined height of the condominium. The height of each floor can be adjusted freely with a certain range.
The final outcome of the building depends on clever height allotment for each floor. So, he plans to calculate possible combinations of per-floor heights to check how many options he has.
Given the height of the condominium and the adjustable range of each floor’s height, make a program to enumerate the number of choices for a floor.
Input
The input is given in the following format.
$H$ $A$ $B$
The input line provides the height of the condominium $H$ ($1 \leq H \leq 10^5$) and the upper and lower limits $A$ and $B$ of the height adjustable range for one floor ($1 \leq A \leq B \leq H$). All data are given as integers.
Output
Output the number of possible height selections for each floor in a line.
Examples
Input
100 2 4
Output
2
Input
101 3 5
Output
0
Submitted Solution:
```
H,a,b=map(int,input().split())
c=0
for i in range(a,b):
if H%a==0 :
c+=1
elif H%b==0:
c+=1
else:
pass
print(c)
```
No
| 14,969 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
while True:
N=0
times=[]
try:
N=int(input())
times=list(map(int,input().split()))
except:
break
times.sort(reverse=True)
ans=0
for i in range(len(times)):
ans+=(i+1)*times[i]
print(ans)
```
| 14,970 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
import sys
def sumItUp(List):
result = []
for i in range(0,len(List)):
result.append(sum(List[0:i+1]))
return sum(result)
List = []
for i in sys.stdin:
List.append(i)
for i in range(1,len(List),2):
List[i] = List[i].split()
for j in range(0,len(List[i])):
List[i][j] = int(List[i][j])
List[i] = sorted(List[i])
print(sumItUp(List[i]))
```
| 14,971 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
# AOJ 1018: Cheating on ICPC
# Python3 2018.7.4 bal4u
while True:
try: n = int(input())
except: break
p = sorted(list(map(int, input().split())))
ans, k = 0, n
for x in p:
ans += x*k
k -= 1
print(ans)
```
| 14,972 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
while 1:
try:n=int(input())
except:break
print(sum((n-i)*x for i,x in enumerate(sorted(list(map(int,input().split()))))))
```
| 14,973 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
while True:
try:
_= int(input())
except:
break
t= 0
ans= []
for v in sorted(list(map(int, input().split()))):
t+= v
ans.append(t)
print(sum(ans))
```
| 14,974 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
try:
while 1:
N = int(input())
*A, = map(int, input().split())
A.sort()
ans = 0; su = 0
for a in A:
su += a; ans += su
print(ans)
except EOFError:
...
```
| 14,975 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
while True:
try:
n=int(input())
A=sorted(list(map(int,input().split())))
S=0
K=0
for i in range(n):
S+=A[i]*(n-i)
print(S)
except EOFError:
break
```
| 14,976 |
Provide a correct Python 3 solution for this coding contest problem.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
"Correct Solution:
```
while True:
try:
num = int(input())
except:
break
times = list(map(int, input().split()))
times.sort()
ans = sum([(num-index) * time for index, time in enumerate(times)])
print(ans)
```
| 14,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
Submitted Solution:
```
while True:
try:
n=int(input())
except:
exit()
a=list(map(int,input().split()))
a.sort()
for i in range(n-1):
a[i+1]+=a[i]
print(sum(a))
```
Yes
| 14,978 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
Submitted Solution:
```
from sys import stdin
for num in stdin:
penalty = [0] * int(num)
time = [int(i) for i in stdin.readline().split()]
time.sort()
penalty[0] = time[0]
for j in range(1,len(time)):
penalty[j] += penalty[j-1] + time[j]
print(sum(penalty))
```
Yes
| 14,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest.
Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory.
Teams are ranked by following rules.
1. Team that solved more problems is ranked higher.
2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher.
Here, Penalty is calculated by these rules.
1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.
2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.
You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given.
Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems.
Input
Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≤ N ≤ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≤ P[i] ≤ 10800) represents time to solve problems.
Input ends with EOF. The number of datasets is less than or equal to 100.
Output
Output minimal possible Penalty, one line for one dataset.
Example
Input
3
10 20 30
7
56 26 62 43 25 80 7
Output
100
873
Submitted Solution:
```
n=int(input())
print(sum((n-i)*x for i,x in enumerate(sorted(list(map(int,input().split()))))))
```
No
| 14,980 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
import sys
sys.setrecursionlimit(2500)
def erase(x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
erase(nx, ny)
while True:
w, h = map(int, sys.stdin.readline().split())
if w == h == 0:
break
c = [sys.stdin.readline().split() for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(x, y)
print(ans)
```
| 14,981 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
import sys
sys.setrecursionlimit(10 ** 7)
def dfs(y, x):
if not (0 <= y < h) or not (0 <= x < w) or not L[y][x]:
return
else:
L[y][x] = 0
dy = [1, 1, 1, 0, 0, -1, -1, -1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(8):
ny = y + dy[i]
nx = x + dx[i]
dfs(ny, nx)
while True:
w, h = map(int, input().split())
if w == h == 0:
break
L = [list(map(int, input().split())) for _ in range(h)]
cnt = 0
for i in range(h):
for j in range(w):
if L[i][j]:
cnt += 1
dfs(i, j)
print(cnt)
```
| 14,982 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
# 25
import sys
import itertools
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
c_l = [[int(x) for x in input().split()] for y in range(h)]
s_l = [[True] * w for x in range(h)]
v_l = list(itertools.product([-1,0,1],repeat=2))
def dfs(y, x):
if x < 0 or y < 0 or x >=w or y >= h:
return False
if not s_l[y][x]:
return False
s_l[y][x] = False
_c = c_l[y][x]
if _c == 0:
return False
for _v in v_l:
dfs(y+_v[0],x+_v[1])
return True
ans = 0
for i in range(h):
for j in range(w):
if s_l[i][j]:
if dfs(i, j):
ans += 1
print(ans)
```
| 14,983 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
from collections import deque
while True:
w,h=map(int,input().split())
if w==0 and h==0:
exit()
field=[input().split() for i in range(h)]
option=deque()
for i in range(h):
for j in range(w):
if field[i][j]=="1":
option.append([i,j])
break
else:
continue
break
completed=[[0]*w for i in range(h)]
cnt=1
while option:
y,x=option.pop()
completed[y][x]=cnt
for dy,dx in ((-1,0),(1,0),(0,-1),(0,1),(-1,-1),(1,-1),(-1,1),(1,1)):
if 0<=y+dy<=h-1 and 0<=x+dx<=w-1 and field[y+dy][x+dx]=="1" and completed[y+dy][x+dx]==0:
ny=y+dy
nx=x+dx
option.append([ny,nx])
if len(option)==0:
for i in range(h):
for j in range(w):
if field[i][j]=="1" and completed[i][j]==0:
option.append([i,j])
cnt+=1
break
else:
continue
break
ans=0
for i in completed:
tmp=max(i)
ans=max(ans,tmp)
print(ans)
```
| 14,984 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
import sys
sys.setrecursionlimit(200000)
readline = sys.stdin.buffer.readline
def dfs(C, h, w):
# 一度訪ねた陸地は'0'(海)に置き換える
C[h][w] = '0'
for i in range(-1, 2):
for j in range(-1, 2):
if 0 <= h+i < len(C) and 0 <= w+j < len(C[0]) and C[h+i][w+j] == '1':
dfs(C, h+i, w+j)
W, H = 1, 1
while W != 0 or H != 0:
W, H = map(int, input().split())
C = []
for h in range(H):
c = list(input().split())
#c = readline().split()
C.append(c)
count = 0
for h in range(H):
for w in range(W):
if C[h][w] == '1':
dfs(C, h, w)
count += 1
if W != 0 or H != 0:
print(count)
```
| 14,985 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
def solve():
from sys import stdin, setrecursionlimit
setrecursionlimit(4000)
f_i = stdin
def dfs(pos):
area_map[pos] = '0'
for mv in move:
next_pos = pos + mv
if area_map[next_pos] == '1':
dfs(next_pos)
while True:
w, h = map(int, f_i.readline().split())
if w == 0:
break
area_map = ['0'] * (w + 2)
for i in range(h):
area_map += ['0'] + f_i.readline().split() + ['0']
area_map += ['0'] * (w + 2)
move = (1, -1, w + 1, w + 2, w + 3, -w - 3, -w - 2, -w - 1)
ans = 0
for i in range((w + 2) * (h + 2)):
if area_map[i] == '1':
dfs(i)
ans += 1
print(ans)
solve()
```
| 14,986 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
def resolve():
while True:
w, h = map(int, input().split())
if w==h==0:
break
c = [list(map(int, input().split())) for _ in range(h)]
stk = []
visited = [[False]*w for _ in range(h)]
dir = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1))
ans = 0
for i in range(h):
for j in range(w):
if c[i][j]==1 and not visited[i][j]:
stk.append((i, j))
while len(stk)>0:
a = stk.pop()
visited[a[0]][a[1]] = True
for d in dir:
ad = (a[0]+d[0], a[1]+d[1])
if 0<=ad[0]<h and 0<=ad[1]<w and c[ad[0]][ad[1]]==1 and not visited[ad[0]][ad[1]]:
stk.append(ad)
ans += 1
print(ans)
if __name__ == '__main__':
resolve()
```
| 14,987 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
"Correct Solution:
```
import sys
sys.setrecursionlimit(10 ** 7)
def dfs(h, w, grid):
grid[h][w] = 0
# 八方向を探索
for dh in range(-1, 2):
for dw in range(-1, 2):
nh = dh + h
nw = dw + w
# 場外だった場合はするー(番兵)
if nh < 0 or nw < 0:
continue
if grid[nh][nw] == 0:
continue
# 次に進む
dfs(nh, nw, grid)
while True:
grid = []
W, H = map(int, input().split())
if W == 0 and H == 0:
break
grid.append([0] * (W+2))
grid += [[0] + list(map(int, input().split())) + [0] for _ in range(H)]
grid.append([0] * (W+2))
cnt = 0
for h in range(1, H+1):
for w in range(1, W+1):
if grid[h][w] == 0:
continue
dfs(h, w, grid)
cnt += 1
print(cnt)
```
| 14,988 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**5)
def erase(x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
erase(nx, ny)
while True:
w, h = map(int, input().split())
if w == h == 0:
break
c = [[i for i in input().split()] for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(x, y)
print(ans)
```
Yes
| 14,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
def bfs(field, start_i, start_j, w, h):
OFFSETS = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
queue = [(start_i,start_j)]
while len(queue) > 0:
i, j = queue.pop()
field[i][j] = "0"
for di, dj in OFFSETS:
if i+di < 0 or i+di >= h:
continue
if j+dj < 0 or j+dj >= w:
continue
if field[i+di][j+dj] == "1":
queue.append((i+di, j+dj))
def main():
while True:
w, h = [int(s) for s in input().strip().split()]
if w == 0 and h == 0:
break
num_island = 0
field = []
for _ in range(h):
field.append(input().strip().split())
#print(field)
for i in range(h):
for j in range(w):
if field[i][j] == "1":
num_island += 1
bfs(field, i, j, w, h)
print(num_island)
if __name__ == "__main__":
main()
```
Yes
| 14,990 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
# coding: utf-8
import sys
sys.setrecursionlimit(1000000)
class DFSSolver:
def __init__(self, table, h, w):
self.table = table
self.count = 0
self.h = h
self.w = w
def delta(self, i, j):
candididate = [(i-1, j-1), (i-1, j), (i-1, j+1),
(i, j-1), (i, j+1),
(i+1, j-1), (i+1, j), (i+1, j+1)]
return candididate
def dfs(self, i, j):
self.table[i][j] = 0
for i, j in self.delta(i, j):
if i < 0 or j < 0 or i >= self.h or j >= self.w:
pass
elif self.table[i][j] == 1:
self.dfs(i, j)
def solve(self):
for i in range(self.h):
for j in range(self.w):
if self.table[i][j] == 1:
self.dfs(i, j)
self.count += 1
print(self.count)
W, H = list(map(int, input().split(" ")))
while W != 0 and H != 0:
t = []
for _ in range(H):
t.append(list(map(int, input().split(" "))))
d = DFSSolver(t, H, W)
d.solve()
W, H = list(map(int, input().split(" ")))
```
Yes
| 14,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
import sys
read = sys.stdin.read
readline = sys.stdin.readline
def main():
ans = []
while True:
w, h = map(int, readline().split())
if w == 0:
break
c = [[0] * (w+2)]
for _ in range(h):
c.append([0] + [int(i) for i in readline().split()] + [0])
c.append([0] * (w+2))
cnt = 0
seen = [[-1] * (w+2) for _ in range(h+2)]
def dfs(sx, sy):
stack = [(sx, sy)]
while stack:
x, y = stack.pop()
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
nx = x + dx
ny = y + dy
if c[nx][ny] and seen[nx][ny] == -1:
seen[nx][ny] = 1
stack.append((nx, ny))
for i in range(1,h+1):
for j in range(1,w+1):
if c[i][j] and seen[i][j] == -1:
cnt += 1
dfs(i, j)
ans.append(cnt)
print("\n".join(map(str, ans)))
if __name__ == "__main__":
main()
```
Yes
| 14,992 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
def dfs(w,h,W,H,ID,islands,islandsID):
if islands[h][w]==1 and islandsID[h][w]!=0:
for i in range(-1,2):
for j in range(-1,2):
dw=w+i
dh=h+j
if dw>=0 and dw<W and dh>=0 and dh<H:
if islands[dh][dw]==1:
islandsID[dh][dw]=islandsID[h][w]
if islands[h][w]==1 and islandsID[h][w]==0:
for i in range(-1,2):
for j in range(-1,2):
dw=w+i
dh=h+j
if dw>=0 and dw<W and dh>=0 and dh<H:
if islands[dh][dw]==1:
islandsID[dh][dw]=ID
ID+=1
return ID
while True:
islands=[]
islandsID=[]
ID=1
W,H=map(int,input().split())
if W==0:
break
for i in range(H):
islandsID.append([0 for j in range(W)])
for _ in range(H):
islands.append(list(map(int,input().split())))
for h in range(H):
for w in range(W):
ID=dfs(w,h,W,H,ID,islands,islandsID)
print(ID-1)
```
No
| 14,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
import sys
def solve():
while 1:
w, h = map(int, sys.stdin.readline().split())
if w == h == 0:
return
room = [[int(j) for j in sys.stdin.readline().split()] for i in range(h)]
cnt = 0
for i in range(h):
for j in range(w):
if room[i][j]:
cnt += 1
dfs(w, h, room, i, j)
print(cnt)
def dfs(w, h, room, i, j):
if i < 0 or i >= h or j < 0 or j >= w or (not room[i][j]):
return
room[i][j] = 0
dfs(w, h, room, i + 1, j)
dfs(w, h, room, i - 1, j)
dfs(w, h, room, i, j + 1)
dfs(w, h, room, i, j - 1)
dfs(w, h, room, i + 1, j + 1)
dfs(w, h, room, i + 1, j - 1)
dfs(w, h, room, i - 1, j + 1)
dfs(w, h, room, i - 1, j - 1)
if __name__ == '__main__':
solve()
```
No
| 14,994 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
def erase(x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
if c[ny][nx] == '1':
erase(nx, ny)
while True:
w, h = map(int, input().split())
c = [[i for i in input().split()] for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(x, y)
print(ans)
```
No
| 14,995 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
<image>
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
> w h
> c1,1 c1,2 ... c1,w
> c2,1 c2,2 ... c2,w
> ...
> ch,1 ch,2 ... ch,w
>
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output for the Sample Input
0
1
1
3
1
9
Example
Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Output
0
1
1
3
1
9
Submitted Solution:
```
def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
l[y-1][x+1]=0
fill(y-1,x+1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
```
No
| 14,996 |
Provide a correct Python 3 solution for this coding contest problem.
The configuration of three circles packed inside a triangle such that each circle is tangent to the other two circles and to two of the edges of the triangle has been studied by many mathematicians for more than two centuries. Existence and uniqueness of such circles for an arbitrary triangle are easy to prove. Many methods of numerical calculation or geometric construction of such circles from an arbitrarily given triangle have been discovered. Today, such circles are called the Malfatti circles.
Figure 7 illustrates an example. The Malfatti circles of the triangle with the vertices (20, 80), (-40, -20) and (120, -20) are approximately
* the circle with the center (24.281677, 45.219486) and the radius 21.565935,
* the circle with the center (3.110950, 4.409005) and the radius 24.409005, and
* the circle with the center (54.556724, 7.107493) and the radius 27.107493.
Figure 8 illustrates another example. The Malfatti circles of the triangle with the vertices (20, -20), (120, -20) and (-40, 80) are approximately
* the circle with the center (25.629089, −10.057956) and the radius 9.942044,
* the circle with the center (53.225883, −0.849435) and the radius 19.150565, and
* the circle with the center (19.701191, 19.203466) and the radius 19.913790.
Your mission is to write a program to calculate the radii of the Malfatti circles of the given triangles.
<image>
Input
The input is a sequence of datasets. A dataset is a line containing six integers x1, y1 , x2 , y2, x3 and y3 in this order, separated by a space. The coordinates of the vertices of the given triangle are (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ), respectively. You can assume that the vertices form a triangle counterclockwise. You can also assume that the following two conditions hold.
* All of the coordinate values are greater than −1000 and less than 1000.
* None of the Malfatti circles of the triangle has a radius less than 0.1.
The end of the input is indicated by a line containing six zeros separated by a space.
Output
For each input dataset, three decimal fractions r1 , r2 and r3 should be printed in a line in this order separated by a space. The radii of the Malfatti circles nearest to the vertices with the coordinates (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ) should be r1 , r2 and r3 , respectively.
None of the output values may have an error greater than 0.0001. No extra character should appear in the output.
Example
Input
20 80 -40 -20 120 -20
20 -20 120 -20 -40 80
0 0 1 0 0 1
0 0 999 1 -999 1
897 -916 847 -972 890 -925
999 999 -999 -998 -998 -999
-999 -999 999 -999 0 731
-999 -999 999 -464 -464 999
979 -436 -955 -337 157 -439
0 0 0 0 0 0
Output
21.565935 24.409005 27.107493
9.942044 19.150565 19.913790
0.148847 0.207107 0.207107
0.125125 0.499750 0.499750
0.373458 0.383897 0.100456
0.706768 0.353509 0.353509
365.638023 365.638023 365.601038
378.524085 378.605339 378.605339
21.895803 22.052921 5.895714
"Correct Solution:
```
from functools import reduce
import math
def length(p1, p2):
sub2 = [(a - b)**2 for a, b in zip(p1, p2)]
return math.sqrt(sum(sub2))
def r_center(lines):
lines_sum = reduce(lambda a, b: a + b, lines)
s = lines_sum / 2
area = math.sqrt(s * (s - lines[0]) * (s - lines[1]) * (s - lines[2]))
return 2 * area / (lines_sum)
def r_length(lines, r_center):
x = (lines[0] + lines[2] - lines[1]) / 2
b_l = math.sqrt(x**2 + r_center**2)
a_l = math.sqrt((lines[2] - x)**2 + r_center**2)
c_l = math.sqrt((lines[0] - x)**2 + r_center**2)
return [a_l, b_l, c_l]
while True:
positions = list(map(int, input().split()))
if reduce(lambda a, b: a | b, positions) == 0:
break
p1, p2, p3 = [[i, j] for i, j in zip(positions[::2], positions[1::2])]
lines = [length(a, b) for a, b in ((p1, p2), (p2, p3), (p3, p1))]
lines_sum = reduce(lambda a, b: a + b, lines)
r_c = r_center(lines)
round_half = lines_sum / 2
rc_len = r_length(lines, r_c)
r1 = r_c * (round_half + rc_len[0] - r_c - rc_len[1] -
rc_len[2]) / (2 * (round_half - lines[0]))
r2 = r_c * (round_half + rc_len[1] - r_c - rc_len[0] -
rc_len[2]) / (2 * (round_half - lines[1]))
r3 = r_c * (round_half + rc_len[2] - r_c - rc_len[1] -
rc_len[0]) / (2 * (round_half - lines[2]))
print(r2, r3, r1)
```
| 14,997 |
Provide a correct Python 3 solution for this coding contest problem.
The configuration of three circles packed inside a triangle such that each circle is tangent to the other two circles and to two of the edges of the triangle has been studied by many mathematicians for more than two centuries. Existence and uniqueness of such circles for an arbitrary triangle are easy to prove. Many methods of numerical calculation or geometric construction of such circles from an arbitrarily given triangle have been discovered. Today, such circles are called the Malfatti circles.
Figure 7 illustrates an example. The Malfatti circles of the triangle with the vertices (20, 80), (-40, -20) and (120, -20) are approximately
* the circle with the center (24.281677, 45.219486) and the radius 21.565935,
* the circle with the center (3.110950, 4.409005) and the radius 24.409005, and
* the circle with the center (54.556724, 7.107493) and the radius 27.107493.
Figure 8 illustrates another example. The Malfatti circles of the triangle with the vertices (20, -20), (120, -20) and (-40, 80) are approximately
* the circle with the center (25.629089, −10.057956) and the radius 9.942044,
* the circle with the center (53.225883, −0.849435) and the radius 19.150565, and
* the circle with the center (19.701191, 19.203466) and the radius 19.913790.
Your mission is to write a program to calculate the radii of the Malfatti circles of the given triangles.
<image>
Input
The input is a sequence of datasets. A dataset is a line containing six integers x1, y1 , x2 , y2, x3 and y3 in this order, separated by a space. The coordinates of the vertices of the given triangle are (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ), respectively. You can assume that the vertices form a triangle counterclockwise. You can also assume that the following two conditions hold.
* All of the coordinate values are greater than −1000 and less than 1000.
* None of the Malfatti circles of the triangle has a radius less than 0.1.
The end of the input is indicated by a line containing six zeros separated by a space.
Output
For each input dataset, three decimal fractions r1 , r2 and r3 should be printed in a line in this order separated by a space. The radii of the Malfatti circles nearest to the vertices with the coordinates (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ) should be r1 , r2 and r3 , respectively.
None of the output values may have an error greater than 0.0001. No extra character should appear in the output.
Example
Input
20 80 -40 -20 120 -20
20 -20 120 -20 -40 80
0 0 1 0 0 1
0 0 999 1 -999 1
897 -916 847 -972 890 -925
999 999 -999 -998 -998 -999
-999 -999 999 -999 0 731
-999 -999 999 -464 -464 999
979 -436 -955 -337 157 -439
0 0 0 0 0 0
Output
21.565935 24.409005 27.107493
9.942044 19.150565 19.913790
0.148847 0.207107 0.207107
0.125125 0.499750 0.499750
0.373458 0.383897 0.100456
0.706768 0.353509 0.353509
365.638023 365.638023 365.601038
378.524085 378.605339 378.605339
21.895803 22.052921 5.895714
"Correct Solution:
```
def solve():
from sys import stdin
file_input = stdin
while True:
x1, y1, x2, y2, x3, y3 = map(int, file_input.readline().split())
if x1 == y1 == x2 == y2 == 0:
break
A = x1 + y1 * 1j
B = x2 + y2 * 1j
C = x3 + y3 * 1j
a = abs(B - C)
b = abs(C - A)
c = abs(A - B)
s = (a + b + c) / 2
r = (s * (s - a) * (s - b) * (s - c)) ** 0.5 / s
D = (b * B + c * C) / (c + b)
BD = abs(D - B)
I = (BD * A + c * D) / (c + BD)
d = abs(A - I)
e = abs(B - I)
f = abs(C - I)
r1 = r / 2 / (s - a) * (s + d - r - e - f)
r2 = r / 2 / (s - b) * (s + e - r - d - f)
r3 = r / 2 / (s - c) * (s + f - r - d - e)
print(r1, r2, r3)
solve()
```
| 14,998 |
Provide a correct Python 3 solution for this coding contest problem.
The configuration of three circles packed inside a triangle such that each circle is tangent to the other two circles and to two of the edges of the triangle has been studied by many mathematicians for more than two centuries. Existence and uniqueness of such circles for an arbitrary triangle are easy to prove. Many methods of numerical calculation or geometric construction of such circles from an arbitrarily given triangle have been discovered. Today, such circles are called the Malfatti circles.
Figure 7 illustrates an example. The Malfatti circles of the triangle with the vertices (20, 80), (-40, -20) and (120, -20) are approximately
* the circle with the center (24.281677, 45.219486) and the radius 21.565935,
* the circle with the center (3.110950, 4.409005) and the radius 24.409005, and
* the circle with the center (54.556724, 7.107493) and the radius 27.107493.
Figure 8 illustrates another example. The Malfatti circles of the triangle with the vertices (20, -20), (120, -20) and (-40, 80) are approximately
* the circle with the center (25.629089, −10.057956) and the radius 9.942044,
* the circle with the center (53.225883, −0.849435) and the radius 19.150565, and
* the circle with the center (19.701191, 19.203466) and the radius 19.913790.
Your mission is to write a program to calculate the radii of the Malfatti circles of the given triangles.
<image>
Input
The input is a sequence of datasets. A dataset is a line containing six integers x1, y1 , x2 , y2, x3 and y3 in this order, separated by a space. The coordinates of the vertices of the given triangle are (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ), respectively. You can assume that the vertices form a triangle counterclockwise. You can also assume that the following two conditions hold.
* All of the coordinate values are greater than −1000 and less than 1000.
* None of the Malfatti circles of the triangle has a radius less than 0.1.
The end of the input is indicated by a line containing six zeros separated by a space.
Output
For each input dataset, three decimal fractions r1 , r2 and r3 should be printed in a line in this order separated by a space. The radii of the Malfatti circles nearest to the vertices with the coordinates (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ) should be r1 , r2 and r3 , respectively.
None of the output values may have an error greater than 0.0001. No extra character should appear in the output.
Example
Input
20 80 -40 -20 120 -20
20 -20 120 -20 -40 80
0 0 1 0 0 1
0 0 999 1 -999 1
897 -916 847 -972 890 -925
999 999 -999 -998 -998 -999
-999 -999 999 -999 0 731
-999 -999 999 -464 -464 999
979 -436 -955 -337 157 -439
0 0 0 0 0 0
Output
21.565935 24.409005 27.107493
9.942044 19.150565 19.913790
0.148847 0.207107 0.207107
0.125125 0.499750 0.499750
0.373458 0.383897 0.100456
0.706768 0.353509 0.353509
365.638023 365.638023 365.601038
378.524085 378.605339 378.605339
21.895803 22.052921 5.895714
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Problems 1301
Problem G: Malfatti Circles
"""
import math
def main():
while True:
x1, y1, x2, y2, x3, y3 = map(int,input().split())
if x1 == y1 == x2 == y2 == x3 == y3 == 0:
break
a = math.sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) ) # aの長さ
b = math.sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) ) # bの長さ
c = math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) # cの長さ
cosA = round((b*b + c*c - a*a),10) / round(2*b*c,10) # 余弦定理
cosB = round((a*a + c*c - b*b),10) / round(2*a*c,10) # 余弦定理
cosC = round((a*a + b*b - c*c),6) / round(2*a*b,10) # 余弦定理
A = math.degrees(math.acos(cosA)) # bcの角度
B = math.degrees(math.acos(cosB)) # acの角度
C = math.degrees(math.acos(cosC)) # abの角度
S = 1/2*a*c*math.sin(math.radians(B)) # 三角形の面積
r = 2*S / (a+b+c) # 内接円半径
tanA4 = round(math.tan(math.radians(A)/4),10) # tan(A/4)
tanB4 = round(math.tan(math.radians(B)/4),10) # tan(B/4)
tanC4 = round(math.tan(math.radians(C)/4),10) # tan(C/4)
r1 = ((1+tanB4)*(1+tanC4))/(2*(1+tanA4))*r
r2 = ((1+tanC4)*(1+tanA4))/(2*(1+tanB4))*r
r3 = ((1+tanA4)*(1+tanB4))/(2*(1+tanC4))*r
print(r1, r2, r3)
if __name__ == "__main__":
main()
```
| 14,999 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.