output_description
stringlengths
15
956
submission_id
stringlengths
10
10
status
stringclasses
3 values
problem_id
stringlengths
6
6
input_description
stringlengths
9
2.55k
attempt
stringlengths
1
13.7k
problem_description
stringlengths
7
5.24k
samples
stringlengths
2
2.72k
Print the minimum integer m that satisfies the condition. * * *
s682425044
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) def func(n): if n%2 == 1: return 3n+1 else: return n//2 def prog(N): if N == 1: return s else: a=s for i in range(1,N): a = func(a) return a b=[] for i in range(1,10000): tmp = prog(i) if tmp in b: print(i) break else: b.append(tmp)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s460049007
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) def functionF(n): result = n/2 if n % 2 == 0 else 3 * n + 1 return int(result) def calc(s): array = [] for i in range(1,1000000): if i == 1: array.append(s) elif i > 1: array.append(functionF(array[i - 2])) return array def result(s): result = calc(s) for i in range(1, 1000000): # print('i',i) for k in range(i): # print('k',k) # print(result[i], result[k]) if result[i] == result[k]: print(i - 1) return
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s611698634
Runtime Error
p03146
Input is given from Standard Input in the following format: s
def next_num(n): if n%2: return 3*n+1 else: return n/2 s = int(input()) n = s for i in range(1000000): if n in [1,2,4]: print(i+4) break except: n = next_num(n)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s136662214
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) a = [s] def f(n): if n % 2 == 0: return(n//2) else: return(3*n+1) for i in range(1000001): if f(a[i]) in a: print(i+1) break a.append(f(a[i])
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s477704878
Runtime Error
p03146
Input is given from Standard Input in the following format: s
n = int(input()) cnt = 1 if n>=3: cnt = cnt + 1 elif n=2: cnt = cnt + 2 else: cnt = cnt + 3 while n != 1: if n % 2 == 0: n /= 2 else: n = 3*n+1 cnt += 1 print(cnt)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s634431207
Runtime Error
p03146
Input is given from Standard Input in the following format: s
def func(x): if(x % 2 == 0): return x / 2 else: return 3 * x + 1 s = int(input()) if(s == 1 || s == 2): print(4) exit() cnt = 1 while(s != 1): s = func(s) cnt += 1 print(cnt + 1)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s398493319
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) def func(n): if n%2 == 1: return 3n+1 else: return n/2 def prog(N): if N == 1: return s else: a=s for i in range(1,N): a = func(a) return a while prog(k)==prog(j):
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s818298626
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) ans = [s] while True: if s%2 ==0: s = s//2 else: s = 3s + 1 if s in ans: break else: ans.append(s)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s544074494
Runtime Error
p03146
Input is given from Standard Input in the following format: s
lists = input().split() a = int(lists[0]) b = int(lists[1]) x = int(lists[2]) * 2 answer = [] next = 0 for num in range(0, x): answer.append(a) answer.append(b) if len(answer) >= x: break a += 1 b -= 1 if a == b: if a + num < b - num: for b_num in range(b, a): answer.append(b_num) if len(answer) >= x: break elif a + num > b - num: for a_num in range(a, b): answer.append(a_num) if len(answer) >= x: break break answer.sort() for a in answer: print(a)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s777560221
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) def func(n): if n % 2 == 0: return (n/2) else: return (3n+1) a = list() a.append(s) for i in range(1000): a.append(func(a[i])) for j in range(1001): for k in range(1001): if (j != k and a[j] == a[k]): print(k) exit()
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s430811828
Wrong Answer
p03146
Input is given from Standard Input in the following format: s
# import sys x = int(input()) nums = [] ans = 1 while True: if x % 2 == 0: ans += 1 if x / 2 in nums: print(ans) # sys.exit() break nums.append(x / 2) x = x / 2 if x % 2 == 1: ans += 1 if x * 3 + 1 in nums: print(ans) # sys.exit() break nums.append(x * 3 + 1) x = x * 3 + 1
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s198567907
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) def func(n): if n%2 == 1: return 3n+1 else: return n//2 def prog(N): if N == 1: return s else: a=s for i in range(1,N): a = func(a) return a b=[] for i in range(1,1000001): tmp = prog(i) if tmp in b: print(i) break else: b.append(tmp)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s978894824
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) k=[s] i = 1 while 1: if k[-1]%2 == 0: x = int(k[-1]/2) else: x = int(3*k[-1]+1) if x in k: print(i+1) break else: k.append(x) i+=1
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s825492480
Runtime Error
p03146
Input is given from Standard Input in the following format: s
S = int(input()) A = [S] idx = 0 while True if A[idx] % 2 == 0: A.append(A[idx]//2) else: A.append(A[idx]*3+1) if A[idx+1] in A[:idx+1]: print(idx+1) break idx += 1
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s543668029
Runtime Error
p03146
Input is given from Standard Input in the following format: s
A = int(input()) p = A count = 0 if A = 1: count == 0 else: count =2 while A != 1: if A % 2 == 0: A = A // 2 count = count + 1 else: A = A * 3 + 1 count = count + 1 print(count)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s541523140
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) k=[s] i = 1 while 1: if k[-1]%2 == 0: x = int(k[-1]/2) else: x = 3*k[-1]+1 if x in k: print(i+1) break else: k.append(x) i+=1
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s921270411
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) n = 0 if s==1 or s == 2: print("4") elif s==3: print("9") if s >=4: while s % 2 == 0: s = s/2 n += 1 if s!=1 and s%2 == 1: s = 3*s+1 n+=1 if s == 1: print(n+2) break
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s258220578
Runtime Error
p03146
Input is given from Standard Input in the following format: s
a = int(input()) b = [a] c = 0 while: c += 1 if a % 2 == 0: a /= 2 if b.contain(a): print(c) exit() else: b.append(a) else: a = a * 3 + 1 if b.contain(a): print(c) exit() else: b.append(a)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s179078929
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = input() l = [] while flag=False: if tmp in l: flag = True else: if s % 2 == 0: tmp = s / 2 l.append(tmp) s = tmp else: tmp = 3*s +1 l.append(tmp) s = tmp l.sort() print(l[-2])
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s390336801
Runtime Error
p03146
Input is given from Standard Input in the following format: s
s = int(input()) l = [] while s not in l: l.aplend(s) if s % 2 == 0: s //= 2 else: s = 3 * s + 1 print(len(l)=1)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s385388853
Runtime Error
p03146
Input is given from Standard Input in the following format: s
a=input() cnt=1 while a!=1 if(a%2==0): a//=2 cnt+=1 else: a=3a+1 cnt+=1 print(cnt=+3)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s151321153
Runtime Error
p03146
Input is given from Standard Input in the following format: s
an = int(input()) numbers = [] while True: if an%2 == 1: an = 3*an+1 else: an = an//2 if an in numbers: print(len(numbers)+1) break else: numbers.append(an)
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s702227882
Accepted
p03146
Input is given from Standard Input in the following format: s
s = int(input()) ans_list = { 1: 4, 2: 4, 3: 9, 4: 4, 5: 7, 6: 10, 7: 18, 8: 5, 9: 21, 10: 8, 11: 16, 12: 11, 13: 11, 14: 19, 15: 19, 16: 6, 17: 14, 18: 22, 19: 22, 20: 9, 21: 9, 22: 17, 23: 17, 24: 12, 25: 25, 26: 12, 27: 113, 28: 20, 29: 20, 30: 20, 31: 108, 32: 7, 33: 28, 34: 15, 35: 15, 36: 23, 37: 23, 38: 23, 39: 36, 40: 10, 41: 111, 42: 10, 43: 31, 44: 18, 45: 18, 46: 18, 47: 106, 48: 13, 49: 26, 50: 26, 51: 26, 52: 13, 53: 13, 54: 114, 55: 114, 56: 21, 57: 34, 58: 21, 59: 34, 60: 21, 61: 21, 62: 109, 63: 109, 64: 8, 65: 29, 66: 29, 67: 29, 68: 16, 69: 16, 70: 16, 71: 104, 72: 24, 73: 117, 74: 24, 75: 16, 76: 24, 77: 24, 78: 37, 79: 37, 80: 11, 81: 24, 82: 112, 83: 112, 84: 11, 85: 11, 86: 32, 87: 32, 88: 19, 89: 32, 90: 19, 91: 94, 92: 19, 93: 19, 94: 107, 95: 107, 96: 14, 97: 120, 98: 27, 99: 27, 100: 27, } # トンデモナイクソコード print(ans_list[s])
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the minimum integer m that satisfies the condition. * * *
s647398141
Wrong Answer
p03146
Input is given from Standard Input in the following format: s
A = [ 0, 5, 5, 9, 5, 7, 10, 18, 5, 21, 8, 16, 11, 11, 19, 19, 6, 14, 22, 22, 9, 9, 17, 17, 12, 25, 12, 113, 20, 20, 20, 108, 7, 28, 15, 15, 23, 23, 23, 36, 10, 111, 10, 31, 18, 18, 18, 106, 13, 26, 26, 26, 13, 13, 114, 114, 21, 34, 21, 34, 21, 21, 109, 109, 8, 29, 29, 29, 16, 16, 16, 104, 24, 117, 24, 16, 24, 24, 37, 37, 11, 24, 112, 112, 11, 11, 32, 32, 19, 32, 19, 94, 19, 19, 107, 107, 14, 120, 27, 27, 27, ] print(A[int(input())])
Statement A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n).
[{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}]
Print the answer. * * *
s878922120
Wrong Answer
p03516
Input is given from Standard Input in the following format: N d_1 d_2 ... d_{N}
import sys import numpy as np import numba from numba import njit i8 = numba.int64 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 10**9 + 7 @njit((i8,), cache=True) def fact_table(N): inv = np.empty(N, np.int64) inv[0] = 0 inv[1] = 1 for n in range(2, N): q, r = divmod(MOD, n) inv[n] = inv[r] * (-q) % MOD fact = np.empty(N, np.int64) fact[0] = 1 for n in range(1, N): fact[n] = n * fact[n - 1] % MOD fact_inv = np.empty(N, np.int64) fact_inv[0] = 1 for n in range(1, N): fact_inv[n] = fact_inv[n - 1] * inv[n] % MOD return fact, fact_inv, inv @njit((i8[::1],), cache=True) def main(A): # key:サイクルに選んだ個数、(d-2)の和 # value:選んでいないもの:1/(d-1)!、選んだもの:1/(d-2)! の積の和 N = len(A) U = N + 10 dp = np.zeros((U, U), np.int64) dp[0, 0] = 1 fact, fact_inv, inv = fact_table(10**3) for d in A: newdp = np.zeros_like(dp) # 選ばない場合の遷移 newdp += dp * fact_inv[d - 1] % MOD # 選ぶ場合の遷移 if d >= 2: newdp[1:, d - 2 : U] += dp[:-1, 0 : U + 2 - d] * fact_inv[d - 2] % MOD dp = newdp % MOD ret = 0 for n in range(3, N + 1): for a in range(N + 1): x = a * fact[n - 1] % MOD * fact[N - n - 1] % MOD x = x * inv[2] % MOD * dp[n, a] % MOD ret += x return ret % MOD A = np.array(read().split(), np.int64)[1:] print(main(A))
Statement Snuke has come up with the following problem. > You are given a sequence d of length N. Find the number of the undirected > graphs with N vertices labeled 1,2,...,N satisfying the following > conditions, modulo 10^{9} + 7: > > * The graph is simple and connected. > * The degree of Vertex i is d_i. > **When 2 \leq N, 1 \leq d_i \leq N-1, {\rm Σ} d_i = 2(N-1),** it can be proved that the answer to the problem is \frac{(N-2)!}{(d_{1} -1)!(d_{2} - 1)! ... (d_{N}-1)!}. Snuke is wondering what the answer is **when 3 \leq N, 1 \leq d_i \leq N-1, { \rm Σ} d_i = 2N.** Solve the problem under this condition for him.
[{"input": "5\n 1 2 2 3 2", "output": "6\n \n\n * There are six graphs as shown below:\n\n![51367cdb21c64bfb07113b300921d52c.png](https://atcoder.jp/img/asaporo2/51367cdb21c64bfb07113b300921d52c.png)\n\n* * *"}, {"input": "16\n 2 1 3 1 2 1 4 1 1 2 1 1 3 2 4 3", "output": "555275958\n \n\n * Be sure to find the answer modulo 10^{9} + 7."}]
Print the minimum number of operations required. * * *
s531048958
Wrong Answer
p02891
Input is given from Standard Input in the following format: S K
a = list(input()) b = int(input()) c = len(a) sum = 0 for i in range(0, c - 1): if a[i] == a[i + 1]: a[i + 1] = "^" sum += 1 print(sum * b)
Statement Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
[{"input": "issii\n 2", "output": "4\n \n\nT is `issiiissii`. For example, we can rewrite it into `ispiqisyhi`, and now\nany two adjacent characters are different.\n\n* * *"}, {"input": "qq\n 81", "output": "81\n \n\n* * *"}, {"input": "cooooooooonteeeeeeeeeest\n 999993333", "output": "8999939997"}]
Print the minimum number of operations required. * * *
s126425725
Wrong Answer
p02891
Input is given from Standard Input in the following format: S K
s = input() d = [c for c in s] n = int(input()) l = len(d) count = 0 for i in range(l - 2): if i == 0 and d[0] == d[l - 1] and d[i] == d[i + 1]: d[0] = "1" count += 1 if d[i] == d[i + 1]: if d[i] != d[i + 2]: d[i] = "1" count += 1 elif d[i] == d[i + 2]: d[i + 1] = "1" count += 1 if l >= 3: if s[l - 2] == s[l - 1] and s[l - 3] != s[l - 2]: count += 1 count *= n elif s[l - 2] == s[l - 1] and s[l - 2] == s[l - 3]: count *= n count += n - 1 else: count *= n if l == 2 and s[0] == s[1]: count = n print(count)
Statement Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
[{"input": "issii\n 2", "output": "4\n \n\nT is `issiiissii`. For example, we can rewrite it into `ispiqisyhi`, and now\nany two adjacent characters are different.\n\n* * *"}, {"input": "qq\n 81", "output": "81\n \n\n* * *"}, {"input": "cooooooooonteeeeeeeeeest\n 999993333", "output": "8999939997"}]
Print the minimum number of operations required. * * *
s200027564
Accepted
p02891
Input is given from Standard Input in the following format: S K
# test def test(): param = [] val = ["issii", "2", "4"] param.append(val) # issii issii issii # x x x x x x val = ["issii", "3", "6"] param.append(val) # qq qq qq # x x x val = ["qq", "81", "81"] param.append(val) val = ["cooooooooonteeeeeeeeeest", "999993333", "8999939997"] param.append(val) # aaa aaa aaa aaa # _x_ x_x _x_ x_x val = ["aaa", "4", "6"] param.append(val) # aaaa aaaa aaaa aaaa # x x x x x x x x val = ["aaaa", "4", "8"] param.append(val) # aaaaa aaaaa aaaaa aaaaa # x x x x x x x x x x val = ["aaaaa", "4", "10"] param.append(val) # i i i i # x x val = ["i", "4", "2"] param.append(val) # isi isi isi isi # x x x val = ["isi", "4", "3"] param.append(val) # isssi isssi isssi # x x x x x val = ["isssi", "3", "5"] param.append(val) # isiii isiii isiii # x x x x x val = ["isssi", "3", "5"] param.append(val) # iissi iissi iissi # x x x x x x val = ["iissi", "3", "6"] param.append(val) print(param) for i in range(len(param)): ret = f(param[i][0], param[i][1]) if ret == param[i][2]: chk = "o" else: chk = "x" space = " " print( chk + space + param[i][0] + space + param[i][1] + space + param[i][2] + space + "test..." + ret ) def f(s, k): k = int(k) cnt = 0 ss = s for i in range(len(ss)): if i == 0: continue if ss[i - 1] == ss[i]: ss = ss[:i] + "\\" + ss[i + 1 :] cnt += 1 cnt = cnt * k if s.count(s[0]) == len(s): if len(s) % 2 == 1: cnt = cnt + (k // 2) else: cntchk1 = 0 cntchk2 = 0 if s[0] == s[len(s) - 1]: for i in range(len(s)): if s[len(s) - 1] == s[len(s) - 1 - i]: cntchk1 += 1 # else: # break if i < len(s) - 1 and s[0] == s[i + 1]: cntchk2 += 1 if cntchk1 % 2 == 0 and cntchk1 % 2 == 0: cnt = cnt + (k - 1) return str(cnt) ## Submit print(f(input(), input()))
Statement Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
[{"input": "issii\n 2", "output": "4\n \n\nT is `issiiissii`. For example, we can rewrite it into `ispiqisyhi`, and now\nany two adjacent characters are different.\n\n* * *"}, {"input": "qq\n 81", "output": "81\n \n\n* * *"}, {"input": "cooooooooonteeeeeeeeeest\n 999993333", "output": "8999939997"}]
Print the minimum number of operations required. * * *
s152718404
Wrong Answer
p02891
Input is given from Standard Input in the following format: S K
s = [s_i for s_i in input()] k = int(input()) c = a = 0 s_ = s[0] idx = 1 while idx < len(s): if s_ == s[idx]: c += 1 else: s_ = s[idx] c = 0 a += c % 2 idx += 1 a *= k if c != 0 and c % 2 == 0 and s[-1] == s[0]: a -= 1 print(a)
Statement Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
[{"input": "issii\n 2", "output": "4\n \n\nT is `issiiissii`. For example, we can rewrite it into `ispiqisyhi`, and now\nany two adjacent characters are different.\n\n* * *"}, {"input": "qq\n 81", "output": "81\n \n\n* * *"}, {"input": "cooooooooonteeeeeeeeeest\n 999993333", "output": "8999939997"}]
Print the minimum number of operations required. * * *
s668599120
Accepted
p02891
Input is given from Standard Input in the following format: S K
s = str(input()) k = int(input()) def main(s, k): separation = [] tmp = "" for x in range(len(s) - 1): tmp += s[x] if s[x] == s[x + 1]: if tmp == "": tmp += s[x] else: separation.append(tmp) tmp = "" tmp += s[-1] separation.append(tmp) total = 0 if len(separation) == 1: if len(separation[0]) == 1: return k // 2 else: return (len(separation[0]) * k) // 2 if (s[0] == s[-1]) and k > 1 and len(separation) > 1: a = len(separation[0]) // 2 b = len(separation[-1]) // 2 total += a total += b separation[0] += separation[-1] a = separation[0] c = (len(a) // 2) * (k - 1) total += c separation = separation[1:-1] # print(separation) for x in separation: total += (len(x) // 2) * k return total print(main(s, k))
Statement Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
[{"input": "issii\n 2", "output": "4\n \n\nT is `issiiissii`. For example, we can rewrite it into `ispiqisyhi`, and now\nany two adjacent characters are different.\n\n* * *"}, {"input": "qq\n 81", "output": "81\n \n\n* * *"}, {"input": "cooooooooonteeeeeeeeeest\n 999993333", "output": "8999939997"}]
Print the minimum cost to achieve connectivity. * * *
s018731243
Wrong Answer
p03783
The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N
import sys input = sys.stdin.readline from heapq import heappop, heappush """ f(x) = (一番上の長方形の左端がxに来るときのコストの最小値) を関数ごと更新していきたい 更新後をg(x)とする g(x) = |x-L| + min_{-width_1 \leq t\leq width_2} f(x+t), 前回の幅、今回の幅 常に、区間上で最小値を持ち傾きが1ずつ変わる凸な関数であることが維持される。(区間は1点かも) 傾きが変わる点の集合S_f = S_f_lower + S_f_upperを持っていく。 S_f_lower, S_upperは一斉に定数を足す:変化量のみ持つ """ N = int(input()) LR = [[int(x) for x in input().split()] for _ in range(N)] # initialize L, R = LR[0] S_lower = [-L] S_upper = [L] min_f = 0 add_lower = 0 add_upper = 0 prev_w = R - L push_L = lambda x: heappush(S_lower, -x) push_R = lambda x: heappush(S_upper, x) pop_L = lambda: -heappop(S_lower) pop_R = lambda: heappop(S_upper) for L, R in LR[1:]: w = R - L # 平行移動とのminをとるステップ add_lower -= w add_upper += prev_w # abs(x-L) を加えるステップ # abs は瞬間に2傾きが変わるので x = pop_L() + add_lower y = pop_R() + add_upper x, y, z, w = sorted([x, y, L, L]) push_L(x - add_lower) push_L(y - add_lower) push_R(z - add_upper) push_R(w - add_upper) min_f += z - y print(min_f)
Statement AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: ![](https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png) AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.
[{"input": "3\n 1 3\n 5 7\n 1 3", "output": "2\n \n\nThe second rectangle should be moved to the left by a distance of 2.\n\n* * *"}, {"input": "3\n 2 5\n 4 6\n 1 4", "output": "0\n \n\nThe rectangles are already connected, and thus no move is needed.\n\n* * *"}, {"input": "5\n 999999999 1000000000\n 1 2\n 314 315\n 500000 500001\n 999999999 1000000000", "output": "1999999680\n \n\n* * *"}, {"input": "5\n 123456 789012\n 123 456\n 12 345678901\n 123456 789012\n 1 23", "output": "246433\n \n\n* * *"}, {"input": "1\n 1 400", "output": "0"}]
Print the minimum cost to achieve connectivity. * * *
s573424593
Wrong Answer
p03783
The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N
n = int(input()) a = [list(map(int, input().split())) for i in range(n)] dp = [[float("inf")] * (500) for i in range(n + 1)] if n > 400: exit() for i in range(500): dp[0][i] = 0 for i in range(1, n + 1): m = a[i - 1][1] - a[i - 1][0] mm = a[i - 2][1] - a[i - 2][0] for j in range(405): for k in range(405): if (j <= k and k <= j + m) or (k <= j and j <= k + mm) or i == 1: dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(j - a[i - 1][0])) ans = float("inf") for i in range(500): ans = min(ans, dp[n][i]) print(ans)
Statement AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: ![](https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png) AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.
[{"input": "3\n 1 3\n 5 7\n 1 3", "output": "2\n \n\nThe second rectangle should be moved to the left by a distance of 2.\n\n* * *"}, {"input": "3\n 2 5\n 4 6\n 1 4", "output": "0\n \n\nThe rectangles are already connected, and thus no move is needed.\n\n* * *"}, {"input": "5\n 999999999 1000000000\n 1 2\n 314 315\n 500000 500001\n 999999999 1000000000", "output": "1999999680\n \n\n* * *"}, {"input": "5\n 123456 789012\n 123 456\n 12 345678901\n 123456 789012\n 1 23", "output": "246433\n \n\n* * *"}, {"input": "1\n 1 400", "output": "0"}]
Print the minimum cost to achieve connectivity. * * *
s947296661
Wrong Answer
p03783
The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N
# coding: utf-8 num_lec = int(input()) l_lec = [list(map(int, input().split())) for i in range(num_lec)] l_set = set([]) for lec in l_lec: l_set.add(lec[0]) l_set.add(lec[1]) all_dis = [] for num in l_set: l_dis = [] for lec in l_lec: if lec[0] <= num <= lec[1]: min_dis = 0 else: dis_0 = num - lec[0] if dis_0 < 0: dis_0 *= -1 dis_1 = num - lec[1] if dis_1 < 0: dis_1 *= -1 if dis_0 > dis_1: min_dis = dis_1 else: min_dis = dis_0 l_dis.append(min_dis) sum_dis = sum(l_dis) all_dis.append(sum_dis) ans = min(all_dis) print(ans)
Statement AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: ![](https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png) AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.
[{"input": "3\n 1 3\n 5 7\n 1 3", "output": "2\n \n\nThe second rectangle should be moved to the left by a distance of 2.\n\n* * *"}, {"input": "3\n 2 5\n 4 6\n 1 4", "output": "0\n \n\nThe rectangles are already connected, and thus no move is needed.\n\n* * *"}, {"input": "5\n 999999999 1000000000\n 1 2\n 314 315\n 500000 500001\n 999999999 1000000000", "output": "1999999680\n \n\n* * *"}, {"input": "5\n 123456 789012\n 123 456\n 12 345678901\n 123456 789012\n 1 23", "output": "246433\n \n\n* * *"}, {"input": "1\n 1 400", "output": "0"}]
Print the minimum cost to achieve connectivity. * * *
s407314822
Wrong Answer
p03783
The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N
n = int(input()) a = [list(map(int, input().split())) for i in range(n)] def keisan(x): ans = 0 for i, j in a: if x < i: ans += i - x elif x > j: ans += x - j return ans x0 = 0 x3 = 1 << 32 while x3 - x0 > 2: x1 = (x0 * 2 + x3) // 3 x2 = (x0 + x3 * 2) // 3 y1 = keisan(x1) y2 = keisan(x2) if y1 > y2: x0 = x1 else: x3 = x2 print(min(keisan(x0), keisan(x0 + 1), keisan(x0 + 2)))
Statement AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: ![](https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png) AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.
[{"input": "3\n 1 3\n 5 7\n 1 3", "output": "2\n \n\nThe second rectangle should be moved to the left by a distance of 2.\n\n* * *"}, {"input": "3\n 2 5\n 4 6\n 1 4", "output": "0\n \n\nThe rectangles are already connected, and thus no move is needed.\n\n* * *"}, {"input": "5\n 999999999 1000000000\n 1 2\n 314 315\n 500000 500001\n 999999999 1000000000", "output": "1999999680\n \n\n* * *"}, {"input": "5\n 123456 789012\n 123 456\n 12 345678901\n 123456 789012\n 1 23", "output": "246433\n \n\n* * *"}, {"input": "1\n 1 400", "output": "0"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s912710788
Accepted
p04046
The input is given from Standard Input in the following format: H W A B
inp = input().split(" ") H = int(inp[0]) W = int(inp[1]) A = int(inp[2]) B = int(inp[3]) mod = 10**9 + 7 fact = [1] fact_r = [1] for i in range(1, H + W - 1): fact.append(i * fact[i - 1] % mod) fact_r.append(pow(fact[i], 10**9 + 5, mod)) const = fact_r[H - A - 1] * fact_r[A - 1] % mod ans = sum( fact[H + i - A - 1] * fact_r[i] * fact_r[W - i - 1] * fact[W - i - 2 + A] % mod for i in range(B, W) ) print(ans * const % mod)
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s478605185
Accepted
p04046
The input is given from Standard Input in the following format: H W A B
def read_input(): h, w, a, b = map(int, input().split()) return h, w, a, b # xの階乗 mod 10**9 + 7 について、1~nまでの結果を辞書にして返す def factorial_table(n): result = {0: 1} curr = 1 acc = 1 modmax = (10**9) + 7 while curr <= n: acc *= curr acc %= modmax result[curr] = acc curr += 1 return result # xの逆元 mod 10**9 + 7を求める def reverse_mod(x): return power_n(x, 10**9 + 5) # xのn乗 mod 10**9 + 7を求める def power_n(x, n): r = 1 curr_a = x modmax = (10**9) + 7 while n: if 1 & n: r *= curr_a r %= modmax n = n >> 1 curr_a *= curr_a curr_a %= modmax return r def comb(x, y, factorial_dic): return ( factorial_dic[x] * reverse_mod(factorial_dic[y]) * reverse_mod(factorial_dic[x - y]) ) def path(s, e, factorial_dic, modmax): x = e[0] - s[0] y = e[1] - s[1] return comb(x + y, x, factorial_dic) % modmax def submit(): h, w, a, b = read_input() f_dic = factorial_table(h + w - 2) count = 0 modmax = 10**9 + 7 for i in range(h - a): count += path((0, 0), (i, b - 1), f_dic, modmax) * path( (i, b), (h - 1, w - 1), f_dic, modmax ) count %= modmax print(count) if __name__ == "__main__": submit()
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s794919852
Accepted
p04046
The input is given from Standard Input in the following format: H W A B
def d_iroha_and_a_grid(MOD=10**9 + 7): class Combination(object): """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb""" __slots__ = ["mod", "factorial", "inverse"] def __init__(self, max_n: int = 10**6, mod: int = 10**9 + 7): fac, inv = [1], [] fac_append, inv_append = fac.append, inv.append for i in range(1, max_n + 1): fac_append(fac[-1] * i % mod) inv_append(pow(fac[-1], mod - 2, mod)) for i in range(max_n, 0, -1): inv_append(inv[-1] * i % mod) self.mod, self.factorial, self.inverse = mod, fac, inv[::-1] def comb(self, n, r): if n < 0 or r < 0 or n < r: return 0 return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod H, W, A, B = [int(i) for i in input().split()] c = Combination(max_n=H + W) # 左上のマスの座標を(0,0)としたとき、B<=i<=W-1を満たすiについて、 # (0,0)→(H-A-1,i)への行き方 * (H-A-1,i)→(H-A,i)への行き方(1通り) * # (H-A,i)→(H-1,W-1)への行き方 の値の総和を取る(10**9+7で剰余を取る) ans = sum( [ c.comb(H - A - 1 + i, H - A - 1) * c.comb(A - 1 + W - 1 - i, A - 1) for i in range(B, W) ] ) return ans % MOD print(d_iroha_and_a_grid())
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s003447190
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
h, w, a, b = map(int, input().split())
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s417702275
Accepted
p04046
The input is given from Standard Input in the following format: H W A B
iH, iW, iA, iB = [int(x) for x in input().split()] iD = 10**9 + 7 # 法 iLBoxW = iB - 1 iLBoxH = iH - iA - 1 iRBoxW = iW - iB - 1 iRBoxH = iH - 1 # iMax = iH+iW-2 iMax = max(iLBoxW + iLBoxH, iRBoxW + iRBoxH) # nCr = n!/r!(n-r)! # 二分累乗法 iDを法として def fBiPow(iX, iN, iD): iY = 1 while iN > 0: if iN % 2 == 0: iX = iX * iX % iD iN = iN // 2 else: iY = iX * iY % iD iN = iN - 1 return iY # 階乗(iDを法とした)の配列 aM = [1] * (iMax + 1) for i in range(1, iMax + 1): aM[i] = aM[i - 1] * i % iD # 各階乗のiDを法とした逆元の配列 aInvM = [1] * (iMax + 1) aInvM[iMax] = fBiPow(aM[iMax], iD - 2, iD) for i in range(iMax, 0, -1): aInvM[i - 1] = aInvM[i] * i % iD iRet = 0 for iL in range(0, iLBoxH + 1): iRet += ( (aM[iL + iLBoxW] * aInvM[iL] * aInvM[iLBoxW]) % iD * (aM[iRBoxW + iRBoxH - iL] * aInvM[iRBoxW] * aInvM[iRBoxH - iL]) % iD ) iRet %= iD iRet %= iD print(iRet)
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s399167431
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
import math # Function to find modulo inverse of b. It returns # -1 when inverse doesn't # modInverse works for prime m def modInverse(b, m): g = math.gcd(b, m) if g != 1: # print("Inverse doesn't exist") return -1 else: # If b and m are relatively prime, # then modulo inverse is b^(m-2) mode m return pow(b, m - 2, m) # Function to compute a/b under modulo m def modDivide(a, b, m): a = a % m inv = modInverse(b, m) if inv == -1: print("Division not defined") else: return (inv * a) % m MOD = (10**9) + 7 H, W, A, B = list(map(int, input().split(" "))) DP = [] j = 1 for i in range(1, 200002): j *= i j %= MOD DP.append(j) # print(DP) def factorial(i): if i == 0: return 1 global DP # print(i) # print(i, DP[i-1]) # print(DP[i-1]) return DP[i - 1] def move2(H, W, A, B): numPaths = 0 h = H - A w = W - (W - B) + 1 a = A + 1 pttp = 0 for b in range(w, W + 1): # print(h, b, a, W-b+1) ttp = ( factorial(h + b - 2) * modInverse((factorial(h - 1) * factorial(b - 1)) % MOD, MOD) ) % MOD tpttp = ttp ttp -= pttp pttp = tpttp btp = ( factorial(a + (W - b + 1) - 2) * modInverse((factorial(a - 1) * factorial(W - b)) % MOD, MOD) ) % MOD # btp = factorial(a+(W-b+1)-2)//(factorial(a-1)*factorial(W-b)) numPaths += ttp * btp return numPaths ways = move2(H, W, A, B) print(ways % (10**9 + 7))
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s033577118
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
n, k = map(int, input().rstrip().split()) dislikes = list(map(int, input().rstrip().split())) n = str(n) len_n = len(n) separate_n = [int(s) for s in n] pay = separate_n.copy() up_nextlevel = False for i in range(len(separate_n) - 1, -1, -1): up_nextlevel = False if separate_n[i] in dislikes: for j in range(10): num = separate_n[i] + j if num > 9: num -= 10 up_nextlevel = True if not (num in dislikes): pay[i] = num break else: continue if up_nextlevel: for i in range(1, 10): if not (i in dislikes): print(i, end="") break for p in pay: print(p, end="")
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s696267913
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
import math const MOD = 1000000007 def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n - r)) def main(): H, W, A, B = map(int, input().split()) nr = 0 for i in range(1, N - A + 1): nr = nr + comb(i - 1, B + i - 2) * comb(m - b - 1, m + n - b - i - 1) % MOD print(nr % MOD) main()
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s585833246
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
h, w, a, b = [int(i) for i in input().split()] p = 10 ** 9 + 7 s = 0 fact = [1] for i in range(1, h+w-2): fact.append(fact[i-1] * i % p) rfact = [(h+w-2) % p] for i in range(h+w-1, 0, -1): rfact.append(rfact[-1] * i % p) rfact.reverse() def comb(n, r): return fact[n]//fact[n-r]//%pfact[r]%p for i in range(w-b): s += comb(h+w-a-2-i, w-1-i) * comb(a-1+i, a-1)%p print(s%p)
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s921265113
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
H,W,A,B=map(int,input().split()) M=H-A N=W-B import math def comb(n, r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) a=B-1 b=H+N-2 c=N-1 S=sum(comb(a+k,a)*(comb(b-i,c) for k in range(M)) print(S%(10**9+7))
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s863461232
Runtime Error
p04046
The input is given from Standard Input in the following format: H W A B
h, w, a, b = map(int, input().split()) mod = 10**9 + 7 nfact_l = [1 for i in range(h + w + 1)] def nfact_mod(n0, mod0): for i in range(1, len(nfact_l)): nfact_l[i] *= (nfact_l[i - 1] * i) % mod nfact_mod(h + w + 1, mod) # 二分累乗法 あとで使う def powfunc_mod(n0, r0, mod0): ans0 = 1 r0 = bin(r0)[:1:-1] for i in r0: if i == "1": ans0 *= n0 % mod0 n0 *= n0 % mod0 ans0 %= mod0 return ans0 nfact_inv_l = [1 for i in range(h + w + 1)] for i in range(len(nfact_inv_l)): nfact_inv_l[i] = powfunc_mod(nfact_l[i], mod - 2, mod) point1 = [0 for i in range(w - b)] for i in range(len(point1)): # ... = n!*r!^(p-2)*(n-r)!^(p-2) [mod] yoko = b + i tate = h - a - 1 n = yoko + tate r = tate point1[i] = (nfact_l[n] * nfact_inv_l[r] * nfact_inv_l[n - r]) % mod # point1から目的まで行く経路数 out = 0 for i in range(len(point1)): yoko = w - b - 1 + i tate = a - 1 n = yoko + tate r = tate out += ( (((point1[i] * nfact_l[n]) % mod) * nfact_inv_l[r] % mod) * nfact_inv_l[n - r] ) % mod print(out)
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s444641787
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
from collections import deque H, W, A, B = map(int, input().split()) # H,W,A,B=2,3,1,1 # H,W,A,B=10,7,3,4 array = [[0 for i in range(W)] for j in range(H)] # 進めないところ塗りつぶし+1埋め for i in range(H): for j in range(W): if i == 0 or j == 0: array[i][j] = 1 if H - A <= i and j < B: array[i][j] = -1 # array[3][6]=5 def search(k, m): # スタート地点 if k == 0 and m == 0: pass # 縦0横M elif k == 0: array[k][m] = array[k][m - 1] # 縦K横0 elif m == 0: array[k][m] = array[k - 1][m] # -1を含む計算 elif k - 1 == -1: array[k][m] = array[k][m - 1] elif m - 1 == -1: array[k][m] = array[k - 1][m] else: array[k][m] = array[k - 1][m] + array[k][m - 1] # Kが閾値を超えていないで if k + 1 < H: # -1出もなくて、上下に値がある場合 if array[k + 1][m] != -1: search(k + 1, m) if m + 1 < W: if array[k][m + 1] != -1: search(k, m + 1) # 升目の計算 QUE = deque() array[0][0] = 1 # search(0,0) for i in range(0, H): for j in range(0, W): if (0 < i and 0 < j) and array[i][j] != -1: if array[i - 1][j] >= 1 and array[i][j - 1] >= 1: array[i][j] = array[i - 1][j] + array[i][j - 1] elif array[i - 1][j] == -1: array[i][j] = array[i][j] + array[i][j - 1] elif array[i][j - 1] == -1: array[i][j] = array[i - 1][j] + array[i][j] # #iが閾値を超えていないで # if i+1<H : # #-1出もなくて、上下に値がある場合 # if array[i+1][j]!=-1 : # search(i+1,j) # if j+1<W : # if array[i][j+1]!=-1 : # search(i,j+1) # print(*array,sep="\n") print(array[H - 1][W - 1])
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s923937156
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
H, W, A, B = (int(i) for i in input().split()) # H, W, A, B = (2, 3, 1, 1) # 初期化 # W行 H列 mat = [[0 for x in range(W)] for y in range(H)] # AかつBに−1を代入 for y in range(H - A, H): for x in range(0, B): mat[y][x] = -1 # 歩ける数を代入 for y in range(H): for x in range(W): # 右端でも左端でもない if (x != W - 1) and (y != H - 1): # 右が空 if mat[y][x + 1] == 0: mat[y][x] += 1 # 下が空 if mat[y + 1][x] == 0: mat[y][x] += 1 # 右端のみ elif (x == W - 1) and (y != H - 1): # 下が空 if mat[y + 1][x] == 0: mat[y][x] += 1 # 下端のみ elif (x != W - 1) and (y == H - 1): # 右が空 if mat[y][x + 1] == 0: mat[y][x] += 1 # ゴールまでのパターン数を計算 cnt = 0 for y in range(H): for x in range(W): if mat[y][x] == 2: cnt += 1 pattern_num = 2**cnt print(pattern_num)
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s286488491
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
H, W, A, B = [int(n) for n in input().split(" ")] answers = [] for total in range(B, W): disp, div = 1, 1 for n in range(min(H - A, total + 1), total + (H - A)): if div >= max(H - A, total + 1): disp = (disp * n) % (10**9 + 7) else: disp = (disp * n) % (10**9 + 7) // div div += 1 answers.append(disp % (10**9 + 7)) for cnt, total in enumerate(range(B, W)): disp, div = 1, 1 for n in range(min(H - (H - A), W - total), (W - total - 1) + (H - (H - A))): if div >= max(H - (H - A), W - total): disp = (disp * n) % (10**9 + 7) else: disp = (disp * n) % (10**9 + 7) // div div += 1 answers[cnt] *= disp % (10**9 + 7) print(sum(answers) % (10**9 + 7))
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s978894714
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
H, W, A, B = [int(n) for n in input().split(" ")] answers = [] for total in range(B, W): disp = 1 for n in range(1, total + (H - A)): disp *= n for m in range(1, H - A): disp //= m for t in range(1, total + 1): disp //= t answers.append(disp) for cnt, total in enumerate(range(B, W)): disp = 1 for n in range(1, (W - total - 1) + (H - (H - A))): disp *= n for m in range(1, H - (H - A)): disp //= m for t in range(1, W - total): disp //= t answers[cnt] *= disp print(sum(answers))
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. * * *
s918505359
Wrong Answer
p04046
The input is given from Standard Input in the following format: H W A B
import math from itertools import combinations from collections import Counter def solve(H, W, A, B): ans = 0 R = 10**9 + 7 p1_y = math.factorial(H - 1 - A) d2_y = math.factorial(H - 1 - (H - A)) way1 = -1 way3 = -1 for i in range(0, W - B): p1 = (B + i, H - 1 - A) p2 = (B + i, H - A) e = (W - 1, H - 1) d2 = (e[0] - p2[0], e[1] - p2[1]) if way1 == -1: way1 = math.factorial(p1[0] + p1[1]) // math.factorial(p1[0]) // p1_y else: way1 *= p1[0] + p1[1] way1 //= p1[0] if way3 == -1: way3 = math.factorial(d2[0] + d2[1]) // math.factorial(d2[0]) // d2_y else: way3 *= d2[0] + 1 way3 //= d2[0] + d2[1] + 1 ans += way1 * way3 way1 %= R way3 %= R return ans % R if __name__ == "__main__": H, W, A, B = map(int, input().split(" ")) print(solve(H, W, A, B))
Statement We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7.
[{"input": "2 3 1 1", "output": "2\n \n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number\nof ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\".\n\n* * *"}, {"input": "10 7 3 4", "output": "3570\n \n\nThere are 12 forbidden cells.\n\n* * *"}, {"input": "100000 100000 99999 99999", "output": "1\n \n\n* * *"}, {"input": "100000 100000 44444 55555", "output": "738162020"}]
Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo 10^9 + 7. * * *
s251424943
Runtime Error
p03279
Input is given from Standard Input in the following format: N M x_1 x_2 ... x_N y_1 y_2 ... y_M
import bisect r_n, e_n = map(int, input().split()) robot = list(map(int, input().split())) exit = list(map(int, input().split())) dict = [] for r in robot: i = bisect.bisect(exit, r) if i == 0 or i == e_n: continue left = exit[i - 1] right = exit[i] dict.append([r - left, right - r]) dict.sort() def check(dict): if len(dict) <= 1: if len(dict) == 0: return 1 else: return 2 result = 0 left = dict[0][0] count = 1 while True: if count == len(dict) or dict[count][0] > left: break else: count += 1 result += check(dict[count:]) right = dict[0][1] new_dict = [] for d in dict: if d[1] > right: new_dict.append(d) result += check(new_dict) return result print(check(dict))
Statement There are N robots and M exits on a number line. The N + M coordinates of these are all integers and all distinct. For each i (1 \leq i \leq N), the coordinate of the i-th robot from the left is x_i. Also, for each j (1 \leq j \leq M), the coordinate of the j-th exit from the left is y_j. Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously: * Increment the coordinates of all the robots on the number line by 1. * Decrement the coordinates of all the robots on the number line by 1. Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit. Snuke will continue performing operations until all the robots disappear. When all the robots disappear, how many combinations of exits can be used by the robots? Find the count modulo 10^9 + 7. Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.
[{"input": "2 2\n 2 3\n 1 4", "output": "3\n \n\nThe i-th robot from the left will be called Robot i, and the j-th exit from\nthe left will be called Exit j. There are three possible combinations of exits\n(the exit used by Robot 1, the exit used by Robot 2) as follows:\n\n * (Exit 1, Exit 1)\n * (Exit 1, Exit 2)\n * (Exit 2, Exit 2)\n\n* * *"}, {"input": "3 4\n 2 5 10\n 1 3 7 13", "output": "8\n \n\nThe exit for each robot can be chosen independently, so there are 2^3 = 8\npossible combinations of exits.\n\n* * *"}, {"input": "4 1\n 1 2 4 5\n 3", "output": "1\n \n\nEvery robot uses Exit 1.\n\n* * *"}, {"input": "4 5\n 2 5 7 11\n 1 3 6 9 13", "output": "6\n \n\n* * *"}, {"input": "10 10\n 4 13 15 18 19 20 21 22 25 27\n 1 5 11 12 14 16 23 26 29 30", "output": "22"}]
For each dataset, output a line that contains the states of the cells at time _T_. The format of the output is as follows. _S_(0, _T_) _S_(1, _T_) ... _S_(_N_ − 1, _T_) Each state must be represented as an integer and the integers must be separated by a space.
s052473099
Runtime Error
p00906
The input is a sequence of datasets. Each dataset is formatted as follows. _N M A B C T_ _S_(0, 0) _S_(1, 0) ... _S_(_N_ − 1, 0) The first line of a dataset consists of six integers, namely _N_ , _M_ , _A_ , _B_ , _C_ and _T_. _N_ is the number of cells. _M_ is the modulus in the equation (1). _A_ , _B_ and _C_ are coefficients in the equation (1). Finally, _T_ is the time for which you should compute the states. You may assume that 0 < _N_ ≤ 50, 0 < _M_ ≤ 1000, 0 ≤ _A_ , _B_ , _C_ < _M_ and 0 ≤ _T_ ≤ 109. The second line consists of _N_ integers, each of which is non-negative and less than _M_. They represent the states of the cells at time zero. A line containing six zeros indicates the end of the input.
# ittan copy & paste def mul(A, B, N, M): result = [[0] * N for i in range(N)] for i in range(N): for j in range(N): tmp = 0 for k in range(N): tmp += A[i][k] * B[k][j] tmp %= M result[i][j] = tmp return result while 1: N, M, A, B, C, T = map(int, input().split()) if N == 0: break (*S,) = map(int, input().split()) P = [[0] * N for i in range(N)] for i in range(N): P[i][i] = B for i in range(N - 1): P[i + 1][i] = A P[i][i + 1] = C base = [[0] * N for i in range(N)] for i in range(N): base[i][i] = 1 while T: if T & 1: base = mul(base, P, N, M) P = mul(P, P, N, M) T >>= 1 U = [0] * N for i in range(N): tmp = 0 for j in range(N): tmp += base[i][j] * S[j] tmp %= M U[i] = tmp print(*U)
C: One-Dimensional Cellular Automaton There is a one-dimensional cellular automaton consisting of _N_ cells. Cells are numbered from 0 to _N_ − 1\. Each cell has a state represented as a non-negative integer less than _M_. The states of cells evolve through discrete time steps. We denote the state of the i-th cell at time t as _S_(_i_ , _t_). The state at time _t_ \+ 1 is defined by the equation _S_(_i_ , _t_ \+ 1) = (_A_ × _S_(_i_ − 1, _t_) + _B_ × _S_(_i_ , _t_) + _C_ × _S_(_i_ \+ 1, _t_)) mod _M_ , (1) where _A_ , _B_ and _C_ are non-negative integer constants. For _i_ < 0 or _N_ ≤ _i_ , we define _S_(_i_ , _t_) = 0. Given an automaton definition and initial states of cells, your mission is to write a program that computes the states of the cells at a specified time _T_.
[{"input": "4 1 3 2 0\n 0 1 2 0 1\n 5 7 1 3 2 1\n 0 1 2 0 1\n 5 13 1 3 2 11\n 0 1 2 0 1\n 5 5 2 0 1 100\n 0 1 2 0 1\n 6 6 0 2 3 1000\n 0 1 2 0 1 4\n 20 1000 0 2 3 1000000000\n 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1\n 30 2 1 0 1 1000000000\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n 30 2 1 1 1 1000000000\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n 30 5 2 3 1 1000000000\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0", "output": "1 2 0 1\n 2 0 0 4 3\n 2 12 10 9 11\n 3 0 4 2 1\n 0 4 2 0 4 4\n 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n 1 1 3 2 2 2 3 3 1 4 3 1 2 3 0 4 3 3 0 4 2 2 2 2 1 1 2 1 3 0"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s553300457
Wrong Answer
p03629
Input is given from Standard Input in the following format: A
# coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline # n, = map(int,readline().split()) s = input() def next_index(N, s): D = [-1] * 26 E = [None] * (N + 1) cA = ord("a") for i in range(N - 1, -1, -1): E[i + 1] = D[:] D[ord(s[i]) - cA] = i E[0] = D return E n = len(s) nxt = next_index(n, s) # print(nxt) dp = [0] * (n + 1) for i in range(n - 1, -1, -1): idx = max(nxt[i]) bad = min(nxt[i]) dp[i] = dp[idx] + 1 if bad != -1 else 0 # print(nxt[0]) # print(dp) k = dp[0] + 1 ans = [None] * k v = 0 for i in range(k): # print(v) if v == n: ans[-1] = 0 break for j in range(26): # print(nxt[v+1][j], dp[nxt[v+1][j]]) if nxt[v][j] == -1 or dp[nxt[v][j] + 1] < dp[v]: ans[i] = j v = nxt[v][j] + 1 break # print(ans) def f(x): return chr(x + ord("a")) a = "".join(map(f, ans)) print(a) """ x = [chr(ord("z")-i) for i in range(26)] x = "".join(x) print(x) """
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s319646817
Accepted
p03629
Input is given from Standard Input in the following format: A
from bisect import bisect_right, bisect_left A = [ord(t) - ord("a") for t in input()] n = len(A) alf = [[] for i in range(26)] for i, a in enumerate(A): alf[a].append(i) for i in range(26): alf[i].append(n + 1) first = [n] appeard = [0] * 26 cnt = 0 for i in range(n - 1, -1, -1): a = A[i] if 1 - appeard[a]: appeard[a] = 1 cnt += 1 if cnt == 26: first.append(i) cnt = 0 appeard = [0] * 26 def ntoa(x): return chr(ord("a") + x) first.reverse() ans = "" for i in range(26): if 1 - appeard[i]: ans += ntoa(i) if alf[i]: last = alf[i][0] break if len(first) == 1: print(ans) exit() for j in range(len(first) - 1): for i in range(26): nxt = alf[i][bisect_right(alf[i], last)] if nxt >= first[j + 1]: ans += ntoa(i) last = nxt break print(ans)
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s598221906
Accepted
p03629
Input is given from Standard Input in the following format: A
def main(): import sys input = sys.stdin.readline S = input().rstrip("\n") N = len(S) rank = [0] * N seen = [0] * 26 cnt = 0 r = 0 for i in range(N - 1, -1, -1): j = ord(S[i]) - 97 if not seen[j]: seen[j] = 1 cnt += 1 rank[i] = r if cnt == 26: r += 1 seen = [0] * 26 cnt = 0 ans = [] for i in range(26): if not seen[i]: ans.append(i + 97) break i0 = 0 while ord(S[i0]) != ans[0]: i0 += 1 if i0 == N: print(chr(ans[0])) exit() r = rank[i0] seen2 = [0] * 26 flg = 1 for i in range(i0 + 1, N): j = ord(S[i]) - 97 if flg: if rank[i] == r: seen2[j] = 1 else: for k in range(26): if not seen2[k]: ans.append(k + 97) break flg = 0 seen2 = [0] * 26 if j == k: r -= 1 flg = 1 else: if j != k: continue else: r -= 1 flg = 1 for k in range(26): if not seen2[k]: ans.append(k + 97) break print("".join(map(chr, ans))) if __name__ == "__main__": main()
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s721261204
Runtime Error
p03629
Input is given from Standard Input in the following format: A
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <cmath> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <stdlib.h> #include <stdio.h> #include <bitset> #include <cstring> #include <deque> #include <iomanip> #include <limits> #include <fstream> using namespace std; #define FOR(I,A,B) for(int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; //bc|abbbc|baca|aacb->最短は絶対4つ、最初の区間にない英単語を選ぶ、次の区間では一個前の区間で選んだ英単語の後ろにない最小の英単語を選ぶ //aaba int b[200005]; // ある区間でi以降(i含め)で出てきた文字の集合 int c[200005]; // ある区間でi以降の最小の文字 int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin>>s; for(int i=s.length()-1;i>=0;i--){ b[i]=b[i+1]|(1<<(s[i]-'a')); c[i]=c[i+1]; while(b[i]&(1<<c[i]))c[i]++; if(c[i]==26){ b[i]=c[i]=0; } } for(int i=0;i<s.length();++i){ char x=c[i]+'a'; cout<<x; while(i<s.length()&&x!=s[i])i++; } cout<<endl; return 0; }
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s254833764
Wrong Answer
p03629
Input is given from Standard Input in the following format: A
import sys from collections import deque, defaultdict import copy import bisect sys.setrecursionlimit(10**9) import math import heapq from itertools import product, permutations, combinations import fractions import sys def input(): return sys.stdin.readline().strip() alpha2num = lambda c: ord(c) - ord("a") def num2alpha(num): if num <= 26: return chr(96 + num) elif num % 26 == 0: return num2alpha(num // 26 - 1) + chr(122) else: return num2alpha(num // 26) + chr(96 + num % 26) A = input() loc_list = deque([]) loc_list_last = deque([]) num = 0 alpha_list = [-1] * 26 alpha_list_last = [-1] * 26 roop = 0 for i in range(len(A) - 1, -1, -1): alphanum = alpha2num(A[i]) if alpha_list[alphanum] == -1: num += 1 alpha_list_last[alphanum] = i alpha_list[alphanum] = i if num == 26: loc_list.appendleft(alpha_list) loc_list_last.appendleft(alpha_list_last) alpha_list = [-1] * 26 alpha_list_last = [-1] * 26 roop += 1 num = 0 loc_list.appendleft(alpha_list) loc_list_last.appendleft(alpha_list_last) ans = deque([]) # print(loc_list) for i in range(26): if loc_list[0][i] == -1: x = i ans.append(x) break if len(loc_list) > 1: mozi = x for n in range(1, len(loc_list)): loc = loc_list[n][mozi] # print(loc, mozi) for i in range(26): if loc_list_last[n][i] <= loc: ans.appendleft(i) mozi = i break # print(loc_list) ans2 = [] for i in range(len(ans)): ans2.append(num2alpha(ans[i] + 1)) print("".join(ans2))
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s809537854
Wrong Answer
p03629
Input is given from Standard Input in the following format: A
import sys input = sys.stdin.readline import numpy as np S = [ord(s) - ord("a") for s in input().rstrip()]
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s157107151
Accepted
p03629
Input is given from Standard Input in the following format: A
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): inf = 10**7 s = input() n = len(s) s += "".join(chr(i + 97) for i in range(26)) # print(s) # 次をその文字にした時の「長さ、位置」 size = [0] * 26 pos = [n + i for i in range(26)] # 解答復元のための移動先 nxt = [-1] * n # 後ろの文字から順に見る for i in range(n - 1, -1, -1): min_size = inf min_pos = -1 # 次をどの文字にすれば短くなるか調べる # 同じ長さの時は辞書順優先 for sz, p in zip(size, pos): if sz < min_size: min_size = sz min_pos = p code = ord(s[i]) - 97 size[code] = min_size + 1 pos[code] = i nxt[i] = min_pos # print(size) # print(pos) # print(nxt) # print() # 最短になる最初の文字を調べて、nxtの順にたどる min_size = inf min_pos = -1 for sz, p in zip(size, pos): if sz < min_size: min_size = sz min_pos = p i = min_pos ans = s[i] while i < n: i = nxt[i] ans += s[i] print(ans) main()
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s558504884
Wrong Answer
p03629
Input is given from Standard Input in the following format: A
from collections import defaultdict from itertools import product A = input() d = defaultdict(lambda: False) f3 = f2 = f1 = "." for a in A: d[a] = d[f1 + a] = d[f2 + f1 + a] = d[f3 + f2 + f1 + a] = True f3, f2, f1 = f2, f1, a abc = list(map(chr, range(97, 97 + 26))) ABC = [] for i in range(1, 5): ABC.extend(product(abc, repeat=i)) for c in ABC: c = "".join(c) if not d[c]: print(c) break
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s868856108
Accepted
p03629
Input is given from Standard Input in the following format: A
#!/usr/bin/env python3 def main(): A = input() n = len(A) next_i = [] ct = [n] * 26 orda = ord("a") for i in range(n - 1, -1, -1): ct[ord(A[i]) - orda] = i next_i.append(ct.copy()) next_i.reverse() dp = [0] * (n + 1) dp[n] = 1 j = -1 for i in range(n - 1, -1, -1): ct = next_i[i] if max(ct) < n: j = i break else: dp[i] = 1 if j == -1: ct = next_i[0] for c in range(26): if ct[c] == n: print(chr(orda + c)) return rt = [0] * n for i in range(j, -1, -1): ct = next_i[i] min_c = 0 min_v = dp[ct[0] + 1] for c in range(1, 26): v = dp[ct[c] + 1] if v < min_v: min_c = c min_v = v rt[i] = min_c dp[i] = min_v + 1 r = "" i = 0 while i < n: if dp[i] == 1: for c in range(26): if not chr(orda + c) in A[i:]: r += chr(orda + c) break break r += chr(orda + rt[i]) i = next_i[i][rt[i]] + 1 print(r) if __name__ == "__main__": main()
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s626736165
Accepted
p03629
Input is given from Standard Input in the following format: A
def main(): # import sys # readline = sys.stdin.readline # readlines = sys.stdin.readlines A = input() N = len(A) a = ord("a") INF = N na = [[INF] * 26 for _ in range(N + 1)] for i in range(N - 1, -1, -1): c = ord(A[i]) - a for j in range(26): na[i][j] = na[i + 1][j] na[i][c] = i dp = [INF] * (N + 1) dp[N] = 1 recon = [None] * (N + 1) for i in range(N - 1, -1, -1): for j in range(26): ni = na[i][j] if ni == N: if dp[i] > 1: dp[i] = 1 recon[i] = (chr(a + j), N) elif dp[i] > dp[ni + 1] + 1: dp[i] = dp[ni + 1] + 1 recon[i] = (chr(a + j), ni + 1) # k = # for j in range(26): # ni = na[0][j] # if dp[i] > dp[ni] + 1: i = 0 ans = [] while i < N: c, ni = recon[i] ans.append(c) i = ni print("".join(ans)) if __name__ == "__main__": main()
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A. * * *
s266098272
Runtime Error
p03629
Input is given from Standard Input in the following format: A
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from math import * import random def readln(): return list(map(int, (input().split(" ")))) def get(p, c): if c == 0: return "" for i in range(M): if F[p][i] == -1: return chr(i + ord("a")) if C[F[p][i]] < C[p]: return chr(i + ord("a")) + get(F[p][i], c - 1) A = "a" + input() N, M = len(A), 26 C = [0 for i in range(N)] F = [[-1 for j in range(M)] for i in range(N)] start = N - 1 for i in range(N): idx = N - i - 1 k = ord(A[idx]) - ord("a") if idx < N - 1: for j in range(M): F[idx][j] = F[idx + 1][j] F[idx][ord(A[idx + 1]) - ord("a")] = idx + 1 C[idx] = N for j in range(M): if F[idx][j] >= 0: value = C[F[idx][j]] else: value = 0 if C[idx] > value + 1: C[idx] = value + 1 ans = get(0, C[0]) print(ans)
Statement A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, `arc`, `artistic` and (an empty string) are all subsequences of `artistic`; `abc` and `ci` are not. You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
[{"input": "atcoderregularcontest", "output": "b\n \n\nThe string `atcoderregularcontest` contains `a` as a subsequence, but not `b`.\n\n* * *"}, {"input": "abcdefghijklmnopqrstuvwxyz", "output": "aa\n \n\n* * *"}, {"input": "frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn", "output": "aca"}]
Print mn (mod 1,000,000,007) in a line.
s577723951
Accepted
p02468
m n Two integers m and n are given in a line.
print(pow(*list(map(int, input().split())), 10**9 + 7))
Power For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
[{"input": "2 3", "output": "8"}, {"input": "5 8", "output": "390625"}]
Print mn (mod 1,000,000,007) in a line.
s818998852
Accepted
p02468
m n Two integers m and n are given in a line.
print(pow(*map(int, input().split()), 10**9 + 7))
Power For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
[{"input": "2 3", "output": "8"}, {"input": "5 8", "output": "390625"}]
Print mn (mod 1,000,000,007) in a line.
s444836514
Runtime Error
p02468
m n Two integers m and n are given in a line.
print(pow(*map(int, input().split()), int(1e9) + 7))
Power For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
[{"input": "2 3", "output": "8"}, {"input": "5 8", "output": "390625"}]
Print mn (mod 1,000,000,007) in a line.
s038696364
Runtime Error
p02468
m n Two integers m and n are given in a line.
print(pow(int(input()), int(input()), int(1e9 + 7)))
Power For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
[{"input": "2 3", "output": "8"}, {"input": "5 8", "output": "390625"}]
Print mn (mod 1,000,000,007) in a line.
s290500086
Accepted
p02468
m n Two integers m and n are given in a line.
a, x = map(int, input().split()) n = 1000000007 print(pow(a, x, n))
Power For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
[{"input": "2 3", "output": "8"}, {"input": "5 8", "output": "390625"}]
For each data set, print GCD and LCM separated by a single space in a line.
s860764679
Wrong Answer
p00005
Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.
a, b = map(int, input().split()) for i in range(1, a + 1): f = (b * i) % a lcm = b * i if f == 0: break for j in range(1, a + 1): if a % j == 0 and b % j == 0 and j * lcm == a * b: print(j, lcm)
GCD and LCM Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.
[{"input": "6\n 50000000 30000000", "output": "24\n 10000000 150000000"}]
For each data set, print GCD and LCM separated by a single space in a line.
s301140190
Accepted
p00005
Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.
def get_input(): while True: try: yield "".join(input()) except EOFError: break def calcGCD( a, b ): # ?????§??¬?´???°????±?????????????????????????????????????????????¨????????? if a > b: large = a small = b elif a < b: large = b small = a else: return a while True: if large == small: return large temp_small = large - small if temp_small < small: large = small small = temp_small else: large = temp_small small = small def calcLCM(a, b, gcd): # ????°???¬?????°????±?????????? lcm = a * b / gcd return lcm if __name__ == "__main__": array = list(get_input()) for i in range(len(array)): temp_a, temp_b = array[i].split() a, b = int(temp_a), int(temp_b) gcd = calcGCD(a, b) lcm = calcLCM(a, b, gcd) lcm = int(lcm) print("{} {}".format(gcd, lcm))
GCD and LCM Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.
[{"input": "6\n 50000000 30000000", "output": "24\n 10000000 150000000"}]
Print the minimum number of spells required. * * *
s644838089
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
Enter a title here Main.py Success 

 出力 入力 コメント 0 (0.03 sec) 4
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s603247482
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
#!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 # A def A(): return # B def B(): return # C def C(): n = I() s = S() for i in range(n >> 1): s[i], s[n - i - 1] = s[n - i - 1], s[i] d = defaultdict(lambda: 0) m = 1 << n for i in range(m): r = [] b = [] for j in range(n): if i & (1 << j): r.append(s[j]) else: b.append(s[j]) d[(tuple(r), tuple(b))] += 1 ans = 0 for i in range(m): r = [] b = [] for j in range(n): if i & (1 << j): r.append(s[j + n]) else: b.append(s[j + n]) ans += d[(tuple(r), tuple(b))] print(ans) return # D def D(): return # E def E(): return # F def F(): return # Solve if __name__ == "__main__": C()
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s763173829
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
import numpy as np if __name__ == "__main__": T = input().rstrip().split(" ") T = int(T[0]) for t in range(T): line = input().rstrip().split(" ") A = int(line[0]) B = int(line[1]) C = int(line[2]) D = int(line[3]) A_list = [] flag = True if B > A: print("No") continue if B > D: print("No") continue if C > B: print("Yes") continue while True: if A in A_list: flag = True break A_list.extend([A]) if A < B: flag = False break q, A = divmod(A, B) for i in range(q - 1): if A + B > C: break A += B A_list.extend([A + B]) if A <= C: A += D if flag: print("Yes") else: print("No")
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s727974694
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
N = input() data = input().split() bef = -1 count = 0 result = 0 for i in data: if bef != data[i]: if count > 1: result += int(count) / 2 count = 1 bef = data[i] else: count++ print(result)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s094623228
Accepted
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
n, a = open(0) print(sum(len(list(g)) // 2 for k, g in __import__("itertools").groupby(a.split())))
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s672435286
Accepted
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
# A N = input() A_list = list(map(int, input().split())) n = 0 for i in range(len(A_list) - 1): if A_list[i] == A_list[i + 1]: A_list[i + 1] += 10000 n += 1 print(n)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s931724018
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
n=int(input());a=list(map(int,input().split()));c,ans=1,0 for i in range(n-1) if a[i]==a[i+1]:c+=1 else: if c>=2:ans+=c//2 c=1 print(ans)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s080009901
Wrong Answer
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
a = int(input()) print(a // 2)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s479808829
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
from itertools import*;n,a=open(0);print(sum(len(list(g))
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s352744108
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
n=int(input()) a=list(map(int,input().split())) i=1 count=0 while i<n: if a[i]=a[i-1]: count+=1 i+=2 else: i+=1 print(count)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s898226816
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
n = int(input()) a = list(map(int,input().split())) i = 1 count = 0 while i < n: if a[i] = a[i-1]: count += 1 i += 2 else: i += 1 print(count)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s107798020
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
N = input() data = input().split() bef = -1 count = 0 result = 0 for i in range(len(data)): if bef != data[i]: if count > 1: result += int(count) / 2 count = 1 bef = data[i] else: count++ print(result)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s802587542
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
N = int(input()) colors = int(input()) count = 0 for i in range(1,N) if i % 2 == 0 :continue elif color[i-1] == color[i] or color[i] == color[i+1] color[i] = color[i-1] + color[i+1] color[i] = color[i] % 10000 count = count + 1 print(count)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s408720802
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
N = input() data = int(input().split()) bef = -1 count = 0 result = 0 for i in range(len(data)): if bef != data[i]: if count > 1: result += int(count) / 2 count = 1 bef = data[i] else: count++ print(result)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s014494418
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
a=int(input()) N=[0 for i in range(a)] b=input() for i in range(a): N[i]=int(b.aplit()) 2ans=0 2ans2=0 for i in range(a-1): if N[i]==N[i+1]: 2ans2+=1 else : if 2ans2%2==1 : 2ans2+=1 ans+=2ans2/2 2ans2=0 return 2ans
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s636875085
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
N = int(input()) data = [int(i) for i in input().split(" ")] count = 0 for i in range(1,N-1,1): if data[i-1] == data[i]: if i < N-2: data[i] = (data[i+1]+data[i+2])%10000 i=i+1 else: data[i] = data[i+1]%10000 count+=1 print(count)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s490904054
Accepted
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
from itertools import ( accumulate, permutations, combinations, combinations_with_replacement, groupby, product, ) # import math # import numpy as np # Pythonのみ! # from operator import xor # import re # from scipy.sparse.csgraph import connected_components # Pythonのみ! # ↑cf. https://note.nkmk.me/python-scipy-connected-components/ # from scipy.sparse import csr_matrix # import string import sys sys.setrecursionlimit(10**5 + 10) def input(): return sys.stdin.readline().strip() def resolve(): n = int(input()) A = list(map(int, input().split())) cnt = 0 gr = groupby(A) for key, group in gr: # groupはイテレータ。list(group)でリスト化 """ex. 0: [0, 0, 0] 1: [1, 1] 0: [0, 0, 0] 1: [1, 1] 0: [0] 1: [1] """ cnt += len(list(group)) // 2 print(cnt) resolve()
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s957291903
Accepted
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
# -*- coding: utf-8 -*- """ Created on Tue Feb 12 15:50:41 2019 @author: shinjisu """ # import numpy as np # import math def getInt(): return int(input()) def getIntList(): return [int(x) for x in input().split()] def zeros(n): return [0] * n def zeros2(n, m): return [zeros(m)] * n # obsoleted zeros((n, m))で代替 def getIntLines(n): return [int(input()) for i in range(n)] def getIntMat(n, m): # n行に渡って、1行にm個の整数 mat = zeros2(n, m) for i in range(n): mat[i] = getIntList() return mat ALPHABET = [chr(i + ord("a")) for i in range(26)] DIGIT = [chr(i + ord("0")) for i in range(10)] N1097 = 10**9 + 7 INF = 10**18 class Debug: def __init__(self): self.debug = True def off(self): self.debug = False def dmp(self, x, cmt=""): if self.debug: if cmt != "": print(cmt, ": ", end="") print(x) return x def prob(): d = Debug() d.off() N = getInt() A = getIntList() d.dmp(A) magicCount = 0 i = 0 while i < N - 1: cont = 1 while A[i] == A[i + cont]: cont += 1 if i + cont == N: break d.dmp(cont, "cont") magicCount += cont // 2 d.dmp(magicCount, "magicCount") i += cont return magicCount ans = prob() if ans is None: pass else: print(ans) # elif ans[0] == 'col': # for elm in ans[1]: # print(elm)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s320642436
Accepted
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
a = int(input()) N = [0 for i in range(a)] b = input() # print(b) N = b.split() for i in range(a): N[i] = int(N[i]) # print(N) a2ans = 0 a2ans2 = 0 for i in range(a - 1): if N[i] == N[i + 1]: a2ans2 += 1 # print(a2ans2) else: if a2ans2 % 2 == 1: a2ans2 += 1 a2ans += a2ans2 / 2 a2ans2 = 0 if a2ans2 % 2 == 1: a2ans2 += 1 # print(a2ans2) a2ans += a2ans2 / 2 a2ans2 = 0 print(int(a2ans))
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the minimum number of spells required. * * *
s431133827
Runtime Error
p03296
Input is given from Standard Input in the following format: N a_1 a_2 ... a_N
T = int(input()) a = [] for i in range(T): a.append(list(map(int, input().split()))) for r in a: s, b, c, d = r[0], r[1], r[2], r[3] mod_list = [] if (s < b) or (b > d): print("No") break if c > b: print("Yes") break while True: s = s % b if s > c: print("No") break s += d if s in mod_list: print("Yes") break mod_list.append(s)
Statement Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
[{"input": "5\n 1 1 2 2 2", "output": "2\n \n\nFor example, if we change the color of the second slime from the left to 4,\nand the color of the fourth slime to 5, the colors of the slimes will be 1, 4,\n2, 5, 2, which satisfy the condition.\n\n* * *"}, {"input": "3\n 1 2 1", "output": "0\n \n\nAlthough the colors of the first and third slimes are the same, they are not\nadjacent, so no spell is required.\n\n* * *"}, {"input": "5\n 1 1 1 1 1", "output": "2\n \n\nFor example, if we change the colors of the second and fourth slimes from the\nleft to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the\ncondition.\n\n* * *"}, {"input": "14\n 1 2 2 3 3 3 4 4 4 4 1 2 3 4", "output": "4"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s842545322
Runtime Error
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
from itertools import combinations from bisect import bisect_left INF = float("INF") N = int(input()) points = [] scores = [] for i in range(N): point = list(map(int, input().split())) points.append(point + [i]) scores.append(max(abs(point[0]), abs(point[1])) * point[2]) for i in range(N + 1): MIN = INF for j in range(i + 1): for P in combinations(points, N - j): X = [0] tmp_scores = scores[:] for p in points: if not p in P: X.append(p[0]) tmp_scores[p[3]] = 0 X.sort() X += [100000] for p in P: index = bisect_left(X, p[0]) tmp_scores[p[3]] = min( tmp_scores[p[3]], min(p[0] - X[index], X[index + 1] - p[0]) * p[2] ) for Q in combinations(P, i - j): Y = [0] for p in points: if p in Q: Y.append(p[1]) tmp_scores[p[3]] = 0 Y.sort() Y += [100000] for p in Q: index = bisect_left(Y, p[0]) tmp_scores[p[3]] = min( tmp_scores[p[3]], min(p[1] - Y[index], Y[index + 1] - p[1]) * p[2], ) MIN = min(MIN, sum(tmp_scores)) print(MIN)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s692091192
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
import itertools G = [] MAX = 10**9 + 1 def calcS(R_x, R_y): S = 0 for g in G: near_r = MAX x, y, p = g for r in R_x: if near_r == 0: break near_r = min(near_r, abs(x - r)) for r in R_y: if near_r == 0: break near_r = min(near_r, abs(y - r)) S += p * near_r return S def resolve(): global G n = int(input()) G = [] # 道路の建設予定地 pre_x = set() pre_y = set() # pre[i] = [x or y, index] pre = [] for _ in range(n): x, y, p = list(map(int, input().split())) if x not in pre_x: pre_x.add(x) pre.append(["x", x]) if y not in pre_y: pre_y.add(y) pre.append(["y", y]) G.append([x, y, p]) for i in range(n + 1): if i == 0: print(calcS([0], [0])) continue min_S = MAX # i本通路を建設する for R in itertools.combinations(pre, i): R_x = [0] R_y = [0] for r in R: if r[0] == "x": R_x.append(r[1]) else: R_y.append(r[1]) min_S = min(min_S, calcS(R_x, R_y)) print(min_S) resolve()
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s003867046
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
from itertools import combinations def main(): def search(k): MIN = float("inf") for cx in range(k + 1): cy = k - cx for LX in combinations(xset, cx): for LY in combinations(yset, cy): if len(LX) == 0: MIN = min( MIN, sum( p * min(abs(x), min(abs(y - ly) for ly in LY)) for (x, y), p in zip(X, P) ), ) elif len(LY) == 0: MIN = min( MIN, sum( p * min(min(abs(x - lx) for lx in LX), abs(y)) for (x, y), p in zip(X, P) ), ) else: MIN = min( MIN, sum( p * min( min(abs(x - lx) for lx in LX), min(abs(y - ly) for ly in LY), ) for (x, y), p in zip(X, P) ), ) return MIN n = int(input()) X, P, M = [], [], [] xset, yset = set(), set() for _ in range(n): x, y, p = map(int, input().split()) if x != 0 and y != 0: X.append([x, y]) P.append(p) M.append(min(abs(x), abs(y))) xset.add(x) yset.add(y) LIM = min(len(xset), len(yset)) ans = sum(m * p for m, p in zip(M, P)) print(ans) for k in range(1, n + 1): if k >= LIM: print(0) else: print(search(k)) if __name__ == "__main__": main()
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s605454750
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
def ab(x): if x < 0: return (-1) * x else: return x n = int(input()) x = [] y = [] p = [] for i in range(n): X, Y, P = map(int, input().split()) x.append(X) y.append(Y) p.append(P) rec = [0] * n s = 0 for i in range(n): rec[i] = min(ab(x[i]), ab(y[i])) * p[i] s += rec[i] print(s) memory_x = [] memory_y = [] ans = float("inf") ans_x = float("inf") ans_y = float("inf") for i in range(n): ans_x = ans ans_y = ans for j in range(-10000, 10001): memo = 0 if not (j in memory_x): for k in range(n): if rec[k] > ab(x[k] - j) * p[k]: memo += ab(x[k] - j) * p[k] else: memo += rec[k] if memo < ans_x: ans_x = memo X = j for j in range(-10000, 10001): memo = 0 if not (j in memory_y): for k in range(n): if rec[k] > ab(y[k] - j) * p[k]: memo += ab(y[k] - j) * p[k] else: memo += rec[k] if memo < ans_y: ans_y = memo Y = j if ans_x >= ans_y: memory_y.append(Y) ans = ans_y for k in range(n): if rec[k] > ab(y[k] - Y) * p[k]: rec[k] = ab(y[k] - Y) * p[k] else: memory_x.append(X) ans = ans_x for k in range(n): if rec[k] > ab(x[k] - X) * p[k]: rec[k] = ab(x[k] - X) * p[k] print(ans)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s577274782
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
N = int(input()) L = [] X = {} Y = {} LX = [0] LY = [0] for i in range(N): x, y, p = map(int, input().split()) L.append([x, y, p]) if x not in X: X[x] = p else: X[x] += p if y not in Y: Y[y] = p else: Y[y] += p # print(X,Y) ans = [] cnt = 0 for i in range(N): cnt += min(abs(L[i][0]), abs(L[i][1])) * L[i][2] ans.append(cnt) # print(ans) for i in range(N): Xmax = 0 Xkey = 0 for k, v in X.items(): if Xmax < v: Xmax = v Xkey = k Ymax = 0 Ykey = 0 for k, v in Y.items(): if Ymax < v: Ymax = v Ykey = k if Xmax > Ymax: LX.append(Xkey) for q in range(N): if L[q][0] == Xkey: L[q] = [0, 0, 0] else: LY.append(Ykey) for q in range(N): if L[q][1] == Ykey: L[q] = [0, 0, 0] X = {} Y = {} for q in range(N): if L[q][0] not in X: X[L[q][0]] = L[q][2] else: X[L[q][0]] += L[q][2] if L[q][1] not in Y: Y[L[q][1]] = L[q][2] else: Y[L[q][1]] += L[q][2] cnt = 0 for l in range(N): mlen = 10**20 for x in range(len(LX)): if mlen > abs(LX[x] - L[l][0]): mlen = abs(LX[x] - L[l][0]) for y in range(len(LY)): if mlen > abs(LY[y] - L[l][1]): mlen = abs(LY[y] - L[l][1]) cnt += mlen * L[l][2] ans.append(cnt) for i in ans: print(i)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s407853966
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
n = int(input()) x = [None] * n y = [None] * n p = [None] * n distance = 0 lines = [0] * n linesx = [0] * n linesy = [0] * n linestotal = [0] * n for i in range(n): x[i], y[i], p[i] = list(map(int, input().split())) for j in range(n): lines[j] = min(abs(x[j]), abs(y[j])) * p[j] distance += lines[j] print(distance) for i in range(n - 1): tempx = 0 tempy = 0 for k in range(i - 1): tx = abs(x[i] - x[k]) * p[i] ty = abs(y[i] - y[k]) * p[i] if tx < lines[k]: tempx += lines[k] - tx if ty < lines[k]: tempy += lines[k] - ty for j in range(i + 1, n): tempx += abs(x[i] - x[j]) * p[i] tempy += abs(y[i] - y[j]) * p[i] # print(j) if x[i] == x[j]: linesx[i] += lines[j] linesx[j] += lines[i] if y[i] == y[j]: linesy[i] += lines[j] linesy[j] += lines[i] linesx[i] += tempx linesy[i] += tempy for i in range(n): linestotal[i] = max(linesx[i], linesy[i]) # print(linesx) # print(linesy) linestotal.sort(reverse=True) for i in range(n): if distance > linestotal[i]: distance -= linestotal[i] else: distance = 0 print(distance)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s860860852
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
def solve(X, Y, P, N): S = [-1 for i in range(N + 1)] T = [[0 for j in range(N + 1)] for i in range(N + 1)] U = [i for i in range(1, N + 1)] + [-i for i in range(1, N + 1)] for K in range(0, N + 1): if K == 0: s = 0 for i in range(1, N + 1): t = min(abs(X[i]), abs(Y[i])) * P[i] s += t T[0][i] = t S[0] = s elif S[K - 1] == 0: S[K] = 0 else: u = float("inf") min_K = 0 for data in U: if data < 0: s = 0 for i in range(1, N + 1): t = min(T[K - 1][i], abs(X[i] - X[-data])) * P[i] s += t T[K][i] = min(t, T[K][i]) if s < u: u = s min_K = K else: s = 0 for i in range(1, N + 1): t = min(T[K - 1][i], abs(Y[i] - Y[data])) * P[i] s += t T[K][i] = min(t, T[K][i]) if s < u: u = s min_K = K S[K] = u U.remove(min_K) return S N = int(input()) X = [0] Y = [0] P = [0] for i in range(N): x, y, p = map(int, input().split()) X.append(x) Y.append(y) P.append(p) print(solve(X, Y, P, N))
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s821507978
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
import copy N = int(input()) List = [list(map(int, input().split())) for i in range(N)] x_no_list = [] y_no_list = [] x_no_list.append(0) y_no_list.append(0) l2 = copy.copy(x_no_list) l3 = copy.copy(y_no_list) def min_x(t): s = abs(t - x_no_list[0]) for i in range(len(x_no_list)): if s > abs(t - x_no_list[i]): s = abs(t - x_no_list[i]) return s def min_y(t): s = abs(t - y_no_list[0]) for i in range(len(y_no_list)): if s > abs(t - y_no_list[i]): s = abs(t - y_no_list[i]) return s def soukyori(List): s = 0 for i in range(0, len(List)): s += min(min_x(List[i][0]), min_y(List[i][1])) * List[i][2] return s print(soukyori(List)) p = 1 goal = soukyori(List) while goal != 0: x_no_list.append(List[0][0]) sub = soukyori(List) sub1 = 0 sub2 = 100 for i in range(1, N): x_no_list.pop(-1) x_no_list.append(List[i][0]) if sub > soukyori(List): sub = soukyori(List) sub1 = i x_no_list.pop(-1) for i in range(0, N): y_no_list.append(List[0][1]) if sub > soukyori(List): sub = soukyori(List) sub2 = i sub1 = 100 y_no_list.pop(-1) if sub1 == 100: y_no_list.append(List[sub2][1]) else: x_no_list.append(List[sub1][0]) print(soukyori(List)) p = +1 goal = soukyori(List) q = N + 1 - p while q != 1: print(0) q -= 1
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s549833270
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
n = int(input()) xyp = [list(map(int, input().split())) for _ in range(n)] ans = [10**20] * (n + 1) ans[0] = sum(min(abs(x), abs(y)) * p for x, y, p in xyp) ans[-1] = 0 xx = [[]] yy = [[]] for bitt in range(1, 1 << n): xs = [(10**20, 0)] ys = [(10**20, 0)] for i in range(n): if bitt & (1 << i): xs.append((xyp[i][0], i)) ys.append((xyp[i][1], i)) xs.sort() ys.sort() xx.append(xs) yy.append(ys) for trit in range(1): z = 0 xbit = 0 ybit = 0 for i in range(n): if trit % 3 == 0: z += 1 if trit % 3 == 1: xbit += 1 << i if trit % 3 == 2: ybit += 1 << i trit //= 3 anss = [min(abs(x), abs(y)) * p for x, y, p in xyp] if xbit: xs = xx[xbit] i = 0 notx = [1] * n for x, j in xx[-xbit - 1][:-1]: while abs(xs[i][0] - x) > abs(xs[i + 1][0] - x): i += 1 anss[j] = min(anss[j], abs(xs[i][0] - x) * xyp[j][2]) notx[j] = 0 for j in range(n): if notx[j]: anss[j] = 0 if ybit: ys = yy[ybit] i = 0 noty = [1] * n for y, j in yy[-ybit - 1][:-1]: while abs(ys[i][0] - y) > abs(ys[i + 1][0] - y): i += 1 anss[j] = min(anss[j], abs(ys[i][0] - y) * xyp[j][2]) noty[j] = 0 for j in range(n): if noty[j]: anss[j] = 0 ans[n - z] = min(ans[n - z], sum(anss)) print(*ans)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]
Print the answer in N+1 lines. The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1. * * *
s837917266
Wrong Answer
p02604
Input is given from Standard Input in the following format: N X_1 Y_1 P_1 X_2 Y_2 P_2 : : : X_N Y_N P_N
from itertools import product n = 15 x = [] y = [] p = [] for i in range(1, n + 1): x.append(i + 10000) y.append(i + 10000) p.append(i) yokomi = [0] * (1 << n) tatemi = [0] * (1 << n) for i in range(1 << n): yoko = [10000] tate = [10000] for j in range(n): if (i >> j) & 1 == 1: yoko.append(y[j]) tate.append(x[j]) for j in range(n): pp = 10**9 qq = 10**9 for e in yoko: if pp > abs(e - y[j]): pp = abs(e - y[j]) for e in tate: if qq > abs(e - x[j]): qq = min(qq, abs(e - x[j])) yokomi[i] += pp * p[j] tatemi[i] += qq * p[j] def iter_p_adic(p, n): tmp = [range(p)] * n return product(*tmp) res = [10**20] * (n + 1) iterator = iter_p_adic(3, n) for idxs in iterator: c = 0 xn = 0 yn = 0 for i in range(n): if idxs[i] == 1: yn += 1 << i c += 1 elif idxs[i] == 2: xn += 1 << i c += 1 tmp = 0 tmp += min(tatemi[xn], yokomi[yn]) if c < n + 1 and res[c] > tmp: res[c] = tmp for e in res: print(e)
Statement New AtCoder City has an infinite grid of streets, as follows: * At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point. * A straight street, which we will call _East-West Main Street_ , runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane. * A straight street, which we will call _North-South Main Street_ , runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane. * There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane. There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas. **The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.** M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets. Let the _walking distance_ of each citizen be the distance from his/her residential area to the nearest railroad. M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized. For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?
[{"input": "3\n 1 2 300\n 3 3 600\n 1 4 800", "output": "2900\n 900\n 0\n 0\n \n\nWhen K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1,\n3, and 1, respectively, to reach a railroad. \nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3\n\\times 600 + 1 \\times 800 = 2900.\n\nWhen K = 1, if we build a railroad along the street corresponding to the line\ny = 4 in the coordinate plane, the walking distances of the citizens of Area\n1, 2, and 3 become 1, 1, and 0, respectively. \nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900. \nWe have many other options for where we build the railroad, but none of them\nmakes S less than 900.\n\nWhen K = 2, if we build a railroad along the street corresponding to the lines\nx = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad\nwith the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.\n\nThe figure below shows the optimal way to build railroads for the cases K = 0,\n1, 2. \nThe street painted blue represents the roads along which we build railroads.\n\n![](https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png)\n\n* * *"}, {"input": "5\n 3 5 400\n 5 3 700\n 5 5 1000\n 5 7 700\n 7 5 400", "output": "13800\n 1600\n 0\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the cases K = 1,\n2.\n\n![](https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png)\n\n* * *"}, {"input": "6\n 2 5 1000\n 5 2 1100\n 5 5 1700\n -2 -5 900\n -5 -2 600\n -5 -5 2200", "output": "26700\n 13900\n 3200\n 1200\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 3.\n\n![](https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png)\n\n* * *"}, {"input": "8\n 2 2 286017\n 3 1 262355\n 2 -2 213815\n 1 -3 224435\n -2 -2 136860\n -3 -1 239338\n -2 2 217647\n -1 3 141903", "output": "2576709\n 1569381\n 868031\n 605676\n 366338\n 141903\n 0\n 0\n 0\n \n\nThe figure below shows the optimal way to build railroads for the case K = 4.\n\n![](https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png)"}]