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 amount of money that Iroha will hand to the cashier. * * *
s103039320
Wrong Answer
p04045
The input is given from Standard Input in the following format: N K D_1 D_2 … D_K
n, k = input().split() o = map(int, input().split()) nums = map(int, list(n)) l = [] for v in nums: while v in o: v += 1 l.append(v) print("".join(map(str, l)))
Statement Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K. She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier.
[{"input": "1000 8\n 1 3 4 5 6 7 8 9", "output": "2000\n \n\nShe dislikes all digits except 0 and 2.\n\nThe smallest integer equal to or greater than N=1000 whose decimal notation\ncontains only 0 and 2, is 2000.\n\n* * *"}, {"input": "9999 1\n 0", "output": "9999"}]
Print the amount of money that Iroha will hand to the cashier. * * *
s433527446
Runtime Error
p04045
The input is given from Standard Input in the following format: N K D_1 D_2 … D_K
n,k = map(int, input().split()) li = [] i = 0 for i<k: li.append(int(input())) i += 1 else: Li = list(n) while not len(set(li)&set(Li)): n += 1 Li = list(n) else: print("".join(Li))
Statement Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K. She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier.
[{"input": "1000 8\n 1 3 4 5 6 7 8 9", "output": "2000\n \n\nShe dislikes all digits except 0 and 2.\n\nThe smallest integer equal to or greater than N=1000 whose decimal notation\ncontains only 0 and 2, is 2000.\n\n* * *"}, {"input": "9999 1\n 0", "output": "9999"}]
Print the amount of money that Iroha will hand to the cashier. * * *
s078372369
Runtime Error
p04045
The input is given from Standard Input in the following format: N K D_1 D_2 … D_K
n,k = map(int, input().split()) li = [] i = 0 for i<k: li.append(int(input())) i += 1 else: Li = list(str(n)) while li&Li: n += 1 Li = list(str(n)) else: print("".join(Li))
Statement Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K. She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier.
[{"input": "1000 8\n 1 3 4 5 6 7 8 9", "output": "2000\n \n\nShe dislikes all digits except 0 and 2.\n\nThe smallest integer equal to or greater than N=1000 whose decimal notation\ncontains only 0 and 2, is 2000.\n\n* * *"}, {"input": "9999 1\n 0", "output": "9999"}]
Print the amount of money that Iroha will hand to the cashier. * * *
s244751703
Runtime Error
p04045
The input is given from Standard Input in the following format: N K D_1 D_2 … D_K
n,k = map(int, input().split()) li = [] i = 0 for i<k: li.append(int(input())) i += 1 else: Li = list(n) while li&Li: n += 1 Li = list(n) else: print("".join(Li))
Statement Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K. She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier.
[{"input": "1000 8\n 1 3 4 5 6 7 8 9", "output": "2000\n \n\nShe dislikes all digits except 0 and 2.\n\nThe smallest integer equal to or greater than N=1000 whose decimal notation\ncontains only 0 and 2, is 2000.\n\n* * *"}, {"input": "9999 1\n 0", "output": "9999"}]
If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1. Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition. * * *
s396178569
Accepted
p02892
Input is given from Standard Input in the following format: N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
# -*- coding: utf-8 -*- import sys from collections import deque def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") sys.setrecursionlimit(10**9) INF = float("inf") MOD = 10**9 + 7 N = INT() G = [None] * N for i in range(N): G[i] = list(map(int, input())) nodes = [[] for i in range(N)] for i in range(N): for j in range(N): if G[i][j]: nodes[i].append(j) nodes[j].append(i) def bfs(start): visited = list2d(N, N, 0) group = [-1] * N que = deque() que.append((start, 1)) mx = 0 while que: u, cnt = que.popleft() if group[u] == -1: group[u] = cnt elif group[u] == cnt: continue else: return -1 mx = max(mx, cnt) for v in nodes[u]: if visited[u][v]: continue visited[u][v] = visited[v][u] = 1 que.append((v, cnt + 1)) return mx ans = -1 for i in range(N): ans = max(ans, bfs(i)) print(ans)
Statement Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S. If S_{i,j} is `1`, there is an edge connecting Vertex i and j; otherwise, there is no such edge. Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division. * Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.
[{"input": "2\n 01\n 10", "output": "2\n \n\nWe can put Vertex 1 in V_1 and Vertex 2 in V_2.\n\n* * *"}, {"input": "3\n 011\n 101\n 110", "output": "-1\n \n\n* * *"}, {"input": "6\n 010110\n 101001\n 010100\n 101000\n 100000\n 010000", "output": "4"}]
If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1. Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition. * * *
s942884397
Wrong Answer
p02892
Input is given from Standard Input in the following format: N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
s = str(input()) k = int(input()) if len(s) == 1: if k < 2: ans = 0 else: ans = k // 2 elif len(s) == 2: if s[0] == s[1]: ans = k else: ans = 0 else: if k == 1: ans = 0 bef = "" befchen = False trignum = 0 for i in range(len(s)): now = s[i] if bef == now: if befchen == False: ans += 1 befchen = True trignum = i if befchen == True: if trignum == i - 1: befchen = False bef = now elif k >= 2: ans = 0 bef = "" befchen = False trignum = 0 for i in range(len(s)): now = s[i] if bef == now: if befchen == False: # print(now) ans += 1 befchen = True trignum = i if befchen == True: if trignum == i - 1: befchen = False bef = now if s[0] == s[-1]: if befchen == False: ans += 1 ans *= k if s[0] == s[-1]: if befchen == False: ans -= 1 print(ans)
Statement Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S. If S_{i,j} is `1`, there is an edge connecting Vertex i and j; otherwise, there is no such edge. Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division. * Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.
[{"input": "2\n 01\n 10", "output": "2\n \n\nWe can put Vertex 1 in V_1 and Vertex 2 in V_2.\n\n* * *"}, {"input": "3\n 011\n 101\n 110", "output": "-1\n \n\n* * *"}, {"input": "6\n 010110\n 101001\n 010100\n 101000\n 100000\n 010000", "output": "4"}]
If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1. Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition. * * *
s523646164
Wrong Answer
p02892
Input is given from Standard Input in the following format: N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
# -*- coding: utf-8 -*- s = list(input()) k = int(input()) flag = False if s[0] == s[-1]: # print("flag") flag = True else: pass # sの中だけ考える count = 0 dp = "" for i in range(len(s)): if dp == s[i]: count += 1 dp = "" else: dp = s[i] ans = count * k if flag: try: # もし後ろ3つが同じなら # print("here") if len(set([s[-1], s[-2], s[-3]])) == 1: if len(set([s[0], s[1]])) == 1: pass if len(set([s[0], s[1], s[2]])) == 1: ans = ans + (k - 1) else: pass else: ans = ans + (k - 1) except IndexError: pass else: pass print(ans)
Statement Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S. If S_{i,j} is `1`, there is an edge connecting Vertex i and j; otherwise, there is no such edge. Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division. * Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.
[{"input": "2\n 01\n 10", "output": "2\n \n\nWe can put Vertex 1 in V_1 and Vertex 2 in V_2.\n\n* * *"}, {"input": "3\n 011\n 101\n 110", "output": "-1\n \n\n* * *"}, {"input": "6\n 010110\n 101001\n 010100\n 101000\n 100000\n 010000", "output": "4"}]
If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1. Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition. * * *
s069068163
Wrong Answer
p02892
Input is given from Standard Input in the following format: N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
N = int(input()) S = [input() for i in range(N)] l = [None] * N l[0] = 0 a = 0 while None in l: for i in range(1, N): for j in range(N): if S[i][j] == "1": if l[i] == None: if l[j] == 1: l[i] = 0 elif l[j] == 0: l[i] = 1 elif l[i] == l[j]: a = -1 break print(a)
Statement Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S. If S_{i,j} is `1`, there is an edge connecting Vertex i and j; otherwise, there is no such edge. Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division. * Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.
[{"input": "2\n 01\n 10", "output": "2\n \n\nWe can put Vertex 1 in V_1 and Vertex 2 in V_2.\n\n* * *"}, {"input": "3\n 011\n 101\n 110", "output": "-1\n \n\n* * *"}, {"input": "6\n 010110\n 101001\n 010100\n 101000\n 100000\n 010000", "output": "4"}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s831225466
Accepted
p02938
Input is given from Standard Input in the following format: L R
L, R = map(int, input().split()) mod = 10**9 + 7 DP_ini = [[[0 for i in range(2)] for j in range(2)] for k in range(64)] import copy ANS = 0 for begin in range(64): DP = copy.deepcopy(DP_ini) if L < (1 << (begin + 1)) and (1 << begin) <= R: if not (1 << begin <= L < 1 << (begin + 1)) and not ( 1 << begin <= R < 1 << (begin + 1) ): DP[begin][0][0] = 1 elif 1 << begin <= L < 1 << (begin + 1) and not ( 1 << begin <= R < 1 << (begin + 1) ): DP[begin][1][0] = 1 elif not (1 << begin <= L < 1 << (begin + 1)) and 1 << begin <= R < 1 << ( begin + 1 ): DP[begin][0][1] = 1 else: DP[begin][1][1] = 1 for keta in range(begin - 1, -1, -1): if L & (1 << keta) == 0 and R & (1 << keta) == 0: DP[keta][0][0] = ( DP[keta][0][0] + DP[keta + 1][0][0] * 3 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 1 + DP[keta + 1][1][1] * 0 ) DP[keta][0][1] = ( DP[keta][0][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 1 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) DP[keta][1][0] = ( DP[keta][1][0] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 2 + DP[keta + 1][1][1] * 0 ) DP[keta][1][1] = ( DP[keta][1][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 1 ) elif L & (1 << keta) != 0 and R & (1 << keta) == 0: DP[keta][0][0] = ( DP[keta][0][0] + DP[keta + 1][0][0] * 3 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) DP[keta][0][1] = ( DP[keta][0][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 1 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) DP[keta][1][0] = ( DP[keta][1][0] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 1 + DP[keta + 1][1][1] * 0 ) DP[keta][1][1] = ( DP[keta][1][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) elif L & (1 << keta) == 0 and R & (1 << keta) != 0: DP[keta][0][0] = ( DP[keta][0][0] + DP[keta + 1][0][0] * 3 + DP[keta + 1][0][1] * 1 + DP[keta + 1][1][0] * 1 + DP[keta + 1][1][1] * 0 ) DP[keta][0][1] = ( DP[keta][0][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 2 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 1 ) DP[keta][1][0] = ( DP[keta][1][0] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 2 + DP[keta + 1][1][1] * 1 ) DP[keta][1][1] = ( DP[keta][1][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 1 ) else: DP[keta][0][0] = ( DP[keta][0][0] + DP[keta + 1][0][0] * 3 + DP[keta + 1][0][1] * 1 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) DP[keta][0][1] = ( DP[keta][0][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 2 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 0 ) DP[keta][1][0] = ( DP[keta][1][0] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 1 + DP[keta + 1][1][1] * 0 ) DP[keta][1][1] = ( DP[keta][1][1] + DP[keta + 1][0][0] * 0 + DP[keta + 1][0][1] * 0 + DP[keta + 1][1][0] * 0 + DP[keta + 1][1][1] * 1 ) # print(begin,DP[0]) ANS += sum(DP[0][0]) + sum(DP[0][1]) print(ANS % mod)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s521100578
Runtime Error
p02938
Input is given from Standard Input in the following format: L R
import collections, bisect s = input() t = input() n = len(s) d = collections.defaultdict(list) for i, c in enumerate(s): d[c].append(i + 1) m = len(t) x = [0] * (m + 1) # 長さを記録 y = [0] * (m + 1) # 添え字を記録 for i in range(1, m + 1): c = t[i - 1] if not c in d: print(-1) exit() a = y[i - 1] ptr = bisect.bisect_right(d[c], y[i - 1]) if ptr == len(d[c]): b = d[c][0] else: b = d[c][ptr] y[i] = b if a < b: x[i] = x[i - 1] + b - a else: x[i] = x[i - 1] + n - a + b print(x[-1])
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s854245442
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
l, r = map(int, input().split()) m = 10**9 + 7 f = [1] for i in range(100): f += [f[-1] * (i + 1) % m] def comb(aa, bb, mm): return f[aa] * pow(f[bb], mm - 2, mm) * pow(f[aa - bb], mm - 2, mm) % mm s = bin(l)[2:] n = len(s) a = 0 b = 0 for i in range(1, n): if s[i] == "0": c = 2**b % m for j in range(n - i): a += comb(n - i, j, m) * c a %= m c *= 2 b += 1 s = bin(r)[2:] n = len(s) b = 0 for i in range(1, n): if s[i] == "1": c = 2 ** (n - b - 1) % m for j in range(n - i): a += comb(n - i, j, m) * c a %= m c //= 2 b += 1 for i in range(len(bin(l)[2:]) + 1, len(bin(r)[2:]) + 1): b = 1 for j in range(i): a += comb(i, j, m) * b a %= m b *= 2 print(a)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s256562007
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
# L, R = map(lambda x: bin(int(x))[2:], input().split()) L_, R_ = map(lambda x: int(x), input().split()) L, R = bin(L_)[2:], bin(R_)[2:] P = int(1e9 + 7) n_pattern = [0] * (len(R)) n_pattern[0] = 1 for i in range(1, len(R)): n_pattern[i] = (3 * n_pattern[i - 1]) % P ans = 0 for i in range(len(R)): ans = (ans + n_pattern[i]) % P # xがL未満をひく for i in range(len(L) - 1): ans = (ans - n_pattern[i]) % P tmp = 1 for i, l in enumerate(L): if i == 0: continue elif l == "0": tmp *= 2 elif l == "1": ans = (ans - tmp * 2 * n_pattern[len(L) - i - 1]) % P # y がR以降を引く tmp = 1 for i, r in enumerate(R): if i == 0: continue elif r == "1": tmp *= 2 elif r == "0": ans = (ans - tmp * 2 * n_pattern[len(R) - i - 1]) % P print(ans) def solve_naive(L, R): cnt = 0 keta = len(bin(R)[2:]) for x in range(1, pow(2, keta)): for y in range(x, pow(2, keta)): cnt += (y % x) == (x ^ y) print(cnt) for x in range(1, L): for y in range(x, pow(2, keta)): cnt -= (y % x) == (x ^ y) print(cnt) for x in range(1, pow(2, keta)): for y in range(R + 1, pow(2, keta)): if x > y: continue cnt -= (y % x) == (x ^ y) print(cnt) # solve_naive(L_, R_)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s132936723
Accepted
p02938
Input is given from Standard Input in the following format: L R
mod = 10**9 + 7 # xと最上位bitが同じでx以下のもののスコア足し上げ def countup(x, y): L1 = x.bit_length() x -= 1 << (L1 - 1) y -= 1 << (L1 - 1) L = x.bit_length() dp = [[[0, 0], [0, 0]] for _ in range(L + 1)] # 立ってるbitがlこのもの dp[0][0][0] = 1 for i in range(L): a = x & (1 << (L - 1 - i)) b = y & (1 << (L - 1 - i)) if a: if b: dp[i + 1][0][0] = dp[i][0][0] dp[i + 1][0][1] = 2 * dp[i][0][1] % mod dp[i + 1][1][0] = dp[i][1][0] dp[i + 1][1][1] = (3 * dp[i][1][1] + dp[i][0][1]) % mod else: dp[i + 1][0][0] = dp[i][0][0] dp[i + 1][0][1] = (dp[i][0][0] + 2 * dp[i][0][1]) % mod dp[i + 1][1][0] = (dp[i][0][0] + 2 * dp[i][1][0]) % mod dp[i + 1][1][1] = (3 * dp[i][1][1] + dp[i][0][1] + dp[i][1][0]) % mod else: if not b: dp[i + 1][0][0] = dp[i][0][0] dp[i + 1][0][1] = dp[i][0][1] dp[i + 1][1][0] = 2 * dp[i][1][0] % mod dp[i + 1][1][1] = (3 * dp[i][1][1] + dp[i][1][0]) % mod else: dp[i + 1][0][1] = dp[i][0][1] dp[i + 1][1][0] = dp[i][1][0] dp[i + 1][1][1] = 3 * dp[i][1][1] % mod return sum(dp[L][0]) + sum(dp[L][1]) % mod def main(): Left, Right = map(int, input().split()) ans = 0 while True: # print(bin(Right)) bl = Left.bit_length() br = Right.bit_length() if bl == br: ans = (ans + countup(Right, Left)) % mod break else: ans = (ans + countup(Right, (1 << (br - 1)))) % mod Right = (1 << (br - 1)) - 1 print(ans) if __name__ == "__main__": main()
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s965621267
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
mod = 10**9 + 7 def solve(N): n = N.bit_length() dp = [[0 for i in range(4)] for j in range(n)] dp[0][0] = 1 + (N % 2 == 1) dp[0][1] = 1 + 2 * (N % 2 == 1) dp[0][2] = 2 dp[0][3] = 3 for i in range(1, n): for j in range(4): if j == 0: if N >> i & 1 == 1: dp[i][j] = dp[i - 1][2] + dp[i - 1][1] else: dp[i][j] = dp[i - 1][0] elif j == 1: if N >> i & 1 == 1: dp[i][j] = dp[i - 1][3] + 2 * dp[i - 1][1] else: dp[i][j] = dp[i - 1][1] elif j == 2: dp[i][j] = dp[i - 1][2] + dp[i - 1][3] else: dp[i][j] = 3 * dp[i - 1][3] return dp[-1][0] - 1 def extra(N): if N == 0: return 0 n = N.bit_length() dp = [[0 for i in range(4)] for j in range(n)] dp[0][0] = 1 + (N % 2 == 1) dp[0][1] = 2 + (N % 2 == 1) dp[0][2] = 2 dp[0][3] = 3 for i in range(1, n): for j in range(4): if j == 0: if N >> i & 1 == 1: dp[i][j] = dp[i - 1][2] + dp[i - 1][1] else: dp[i][j] = dp[i - 1][0] elif j == 1: if N >> i & 1 == 1: dp[i][j] = 2 * dp[i - 1][3] + dp[i - 1][1] else: dp[i][j] = 2 * dp[i - 1][1] elif j == 2: dp[i][j] = dp[i - 1][2] + dp[i - 1][3] else: dp[i][j] = 3 * dp[i - 1][3] return dp[-1][0] - 1 L, R = map(int, input().split()) print((solve(R) - extra(L - 1)) % mod)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s203508065
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
L, R = [int(i) for i in input().split(" ")] if R >= 10**10: exit() X = Y = list(range(L, R + 1)) i = 0 for x in X: for y in Y[X.index(x) :]: if y % x == y ^ x: i += 1 print(i % (10**9 + 7))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s999637609
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
s = input().split() L, R = (int(i) for i in s) x = L - 1 y = L - 1 lt = [] rl = [] while x != R: x = x + 1 y = L - 1 while y != R: y = y + 1 lt.append((x, y)) for i in lt: if (i[1] % i[0]) is (i[1] ^ i[0]): rl.append(i) print(int(len(rl) % (10**9 + 7)))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s680668973
Accepted
p02938
Input is given from Standard Input in the following format: L R
L, R = map(int, input().split()) dp = [] for i in range(62): nowA = [] for i in range(2): nowB = [] for i in range(2): nowC = [] for i in range(2): nowC.append(0) nowB.append(nowC) nowA.append(nowB) dp.append(nowA) dp[0][0][0][0] = 1 # print (dp[3][0][0][0]) for i in range(61): i += 1 keta = 62 - i lb = (L >> (keta - 1)) % 2 rb = (R >> (keta - 1)) % 2 for j in range(2): for k in range(2): for m in range(2): pre = dp[i - 1][j][k][m] for x in range(2): for y in range(2): nj = j nk = k nm = m if x == 1 and y == 0: continue if x != y and m == 0: continue if j == 0 and lb == 1 and x == 0: continue if k == 0 and rb == 0 and y == 1: continue if j == 0 and lb == 0 and x == 1: nj = 1 if k == 0 and rb == 1 and y == 0: nk = 1 if m == 0 and x == 1 and y == 1: nm = 1 dp[i][nj][nk][nm] += dp[i - 1][j][k][m] dp[i][nj][nk][nm] %= 10**9 + 7 ans = 0 for j in range(2): for k in range(2): ans += dp[-1][j][k][1] print(ans % (10**9 + 7))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s397011412
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
l, r = map(int, input().split()) yi = l ^ r x = l sum = 0 while x <= r: y = x while y <= r: if yi == y // x: sum += 1 y += 1 x += 1 print(sum % (10**9 + 7))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s468922215
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
l, r = map(int, input().split()) import math ans = 0 inf = math.ceil(math.log(l, 2)) sup = math.floor(math.log(r + 1, 2)) - 1 for i in range(inf, sup + 1): ans += 3**i for i in range(l, 2**inf): n = bin(i).count("0") - 1 ans += 2**n for i in range(2 ** (sup + 1), r + 1): n = bin(i).count("1") - 1 ans += 2**n print(ans % int(10**9 + 7))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s513210631
Accepted
p02938
Input is given from Standard Input in the following format: L R
l, r = map(int, input().split()) lb = l.bit_length() rb = r.bit_length() ans = 0 mod = 10**9 + 7 def ignorel(r): ret = 0 cntr = 0 rb = r.bit_length() for i in range(rb - 1)[::-1]: if r & 1 << i: ret += 2**cntr * 3**i cntr += 1 ret += 2**cntr return ret % mod def ignorer(l): cntl = 0 ret = 0 lb = l.bit_length() for i in range(lb)[::-1]: if l & 1 << i == 0: ret += 2**cntl * 3**i cntl += 1 ret += 2**cntl return ret % mod for i in range(lb + 1, rb): ans += 3 ** (i - 1) ans %= mod if lb != rb: ans += ignorer(l) ans += ignorel(r) ans %= mod print(ans) else: def from0(x): if x == 0: return 1 elif x == 1: return 3 xb = x.bit_length() ret = 3 ** (xb - 1) + 2 * from0(x - (1 << (xb - 1))) return ret def calc(l, r): ret = 0 lb = l.bit_length() rb = r.bit_length() if l > r: return 0 if l == r: return 1 if l == 2 ** (lb - 1): return ignorel(r) elif r == 2**rb - 1: return ignorer(l) for i in range(lb)[::-1]: if l & 1 << i == 0 and r & 1 << i: ret += from0((1 << i) - 1 - (l & (1 << i) - 1)) ret += from0(r & ((1 << i) - 1)) ret += calc( (l & ((1 << i) - 1)) + (1 << i), (r & ((1 << i) - 1)) + (1 << i) ) break return ret % mod ans += calc(l, r) print(ans % mod)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s928173228
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
mod = 10**9 + 7 def solve(n): s = bin(n)[2:][::-1] m = len(s) X = [] for i in range(m)[::-1]: if s[i] == "1": X.append(i) res = 0 t = len(X) for i in range(t): res += pow(3, X[i], mod) * pow(2, i, mod) res %= mod res += pow(2, t, mod) - 1 res %= mod res *= (mod + 1) // 2 res %= mod return res L, R = map(int, input().split()) print(solve(R) - solve(L - 1))
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s768407808
Accepted
p02938
Input is given from Standard Input in the following format: L R
L, R = map(int, input().split()) mod = 10**9 + 7 l = "{:060b}".format(L) r = "{:060b}".format(R) memo = [[[[-1] * 2 for i in range(2)] for j in range(2)] for k in range(60)] def f(pos, flagL, flagR, flagM): if pos == 60: return 1 if memo[pos][flagL][flagR][flagM] != -1: return memo[pos][flagL][flagR][flagM] ret = 0 # (0, 0) if flagL or l[pos] == "0": ret += f(pos + 1, flagL, 1 if r[pos] == "1" else flagR, flagM) # (0, 1) if flagM and (flagL or l[pos] == "0") and (flagR or r[pos] == "1"): ret += f(pos + 1, flagL, flagR, flagM) # (1, 1) if flagR or r[pos] == "1": ret += f(pos + 1, 1 if l[pos] == "0" else flagL, flagR, 1) memo[pos][flagL][flagR][flagM] = ret % mod return ret % mod print(f(0, 0, 0, 0) % mod)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s834592097
Accepted
p02938
Input is given from Standard Input in the following format: L R
import sys sys.setrecursionlimit(2147483647) INF = float("inf") MOD = 10**9 + 7 input = lambda: sys.stdin.readline().rstrip() from itertools import product def resolve(): L, R = map(int, input().split()) L = map(int, bin(L)[2:].zfill(60)) R = map(int, bin(R)[2:].zfill(60)) dp = [[[0] * 2 for _ in range(2)] for _ in range(2)] dp[0][0][0] = 1 # dp[msb][mt][lt] for l, r in zip(L, R): ndp = [[[0] * 2 for _ in range(2)] for _ in range(2)] for msb, mt, lt, x, y in product(range(2), repeat=5): if (not msb) and (x != y): continue if (not mt) and l > x: continue if (not lt) and y > r: continue if x > y: continue ndp[max(msb, x * y == 1)][max(mt, l < x)][max(lt, y < r)] += dp[msb][mt][lt] ndp[max(msb, x * y == 1)][max(mt, l < x)][max(lt, y < r)] %= MOD dp = ndp print(sum(dp[msb][mt][lt] for msb, mt, lt in product(range(2), repeat=3)) % MOD) resolve()
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s480379837
Accepted
p02938
Input is given from Standard Input in the following format: L R
l, r = map(int, input().split()) M = len(bin(r)) - 2 L = format(l, "b").zfill(M) R = format(r, "b") mod = 10**9 + 7 dp = [[0 for l in range(70)] for i in range(5)] if L[0] == "1": dp[2][0] = 1 else: dp[3][0] = 1 dp[0][0] = 1 for i in range(1, M): if L[i] == "1" and R[i] == "1": dp[1][i] = (dp[0][i - 1] + dp[1][i - 1]) % mod dp[2][i] = (dp[2][i - 1]) % mod dp[3][i] = (2 * dp[3][i - 1]) % mod dp[4][i] = (dp[3][i - 1] + 3 * dp[4][i - 1]) % mod elif L[i] == "1" and R[i] == "0": dp[1][i] = dp[1][i - 1] + dp[0][i - 1] dp[3][i] = dp[3][i - 1] dp[4][i] = (dp[4][i - 1] * 3) % mod elif L[i] == "0" and R[i] == "1": dp[0][i] = dp[0][i - 1] dp[1][i] = (2 * dp[1][i - 1] + dp[2][i - 1]) % mod dp[2][i] = dp[2][i - 1] dp[3][i] = (dp[2][i - 1] + dp[3][i - 1] * 2) % mod dp[4][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[3][i - 1] + dp[4][i - 1] * 3) % mod else: dp[0][i] = dp[0][i - 1] dp[1][i] = (dp[1][i - 1] * 2) % mod dp[2][i] = dp[2][i - 1] dp[3][i] = (dp[3][i - 1]) % mod dp[4][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[4][i - 1] * 3) % mod ans = 0 for i in range(5): ans += dp[i][M - 1] ans %= mod print(ans)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s653245208
Wrong Answer
p02938
Input is given from Standard Input in the following format: L R
def count(lb, rb, MOD): assert lb[0] == "1" assert rb[0] == "1" assert len(lb) == len(rb) i = 0 for i, (lc, rc) in enumerate(zip(lb, rb)): if lc != rc: break else: return 1 dp = [1, 0, 1] for lc, rc in zip(lb[i + 1 :], rb[i + 1 :]): ndp = [0, 0, 0] if rc == "0": ndp[0] += dp[0] else: ndp[0] += dp[0] * 2 ndp[1] += dp[0] if lc == "0": ndp[0] += 1 ndp[2] += 1 if lc == "0": ndp[1] += dp[2] ndp[2] += dp[2] * 2 else: ndp[2] += dp[2] ndp[1] += dp[1] * 3 ndp[0] %= MOD ndp[1] %= MOD dp = ndp return sum(dp) + 1 l, r = map(int, input().split()) lb = bin(l)[2:] rb = bin(r)[2:] ld = len(lb) rd = len(rb) ans = 0 MOD = 10**9 + 7 for d in range(ld, rd + 1): tlb = lb if d == ld else "1" + "0" * (d - 1) trb = rb if d == rd else "1" * d ans = (ans + count(tlb, trb, MOD)) % MOD print(ans)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. * * *
s442473467
Accepted
p02938
Input is given from Standard Input in the following format: L R
MOD = 10**9 + 7 L, R = [int(item) for item in input().split()] L_blen = L.bit_length() R_blen = R.bit_length() # dp[y loose/tight][x loose/tight][index] dp = [[[0] * (R_blen + 1) for _ in range(2)] for _ in range(2)] for i in range(R_blen + 1): L_bit = L & (1 << R_blen - i) R_bit = R & (1 << R_blen - i) # Form R's MSB to L's LSB can be the initial bit curbit_idx = R_blen - i + 1 if curbit_idx <= R_blen and curbit_idx >= L_blen: dp[R_blen == curbit_idx][L_blen == curbit_idx][i] += 1 # R=0, L=0 if not R_bit and not L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i - 1] dp[0][0][i] += dp[0][1][i - 1] # y=0, x=0 dp[1][1][i] += dp[1][1][i - 1] dp[1][0][i] += dp[1][0][i - 1] dp[0][0][i] += dp[0][0][i - 1] dp[0][1][i] += dp[0][1][i - 1] # y=1, x=0 dp[0][1][i] += dp[0][1][i - 1] dp[0][0][i] += dp[0][0][i - 1] # R=1, L=0 if R_bit and not L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i - 1] dp[1][0][i] += dp[1][0][i - 1] dp[0][0][i] += dp[0][1][i - 1] dp[1][0][i] += dp[1][1][i - 1] # y=0, x=0 dp[0][0][i] += dp[0][0][i - 1] dp[0][0][i] += dp[1][0][i - 1] dp[0][1][i] += dp[0][1][i - 1] dp[0][1][i] += dp[1][1][i - 1] # y=1, x=0 dp[0][0][i] += dp[0][0][i - 1] dp[1][0][i] += dp[1][0][i - 1] dp[0][1][i] += dp[0][1][i - 1] dp[1][1][i] += dp[1][1][i - 1] # R=0, L=1 if not R_bit and L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i - 1] dp[0][1][i] += dp[0][1][i - 1] # y=0, x=0 dp[0][0][i] += dp[0][0][i - 1] dp[1][0][i] += dp[1][0][i - 1] # y=1, x=0 dp[0][0][i] += dp[0][0][i - 1] # R=1, L=1 if R_bit and L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i - 1] dp[1][0][i] += dp[1][0][i - 1] dp[0][1][i] += dp[0][1][i - 1] dp[1][1][i] += dp[1][1][i - 1] # y=0, x=0 dp[0][0][i] += dp[1][0][i - 1] dp[0][0][i] += dp[0][0][i - 1] # y=1, x=0 dp[1][0][i] += dp[1][0][i - 1] dp[0][0][i] += dp[0][0][i - 1] # Take MOD for i in range(2): for j in range(2): dp[i][j][i] %= MOD ans = 0 for i in range(2): for j in range(2): ans += dp[i][j][-1] ans %= MOD print(ans)
Statement Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)
[{"input": "2 3", "output": "3\n \n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).\n\n* * *"}, {"input": "10 100", "output": "604\n \n\n* * *"}, {"input": "1 1000000000000000000", "output": "68038601\n \n\nBe sure to compute the number modulo 10^9 + 7."}]
Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. * * *
s622907340
Wrong Answer
p03850
The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N
input() sm = de = 0 ans = 10**15 for s in input().split("-"): a = list(map(int, s.split("+"))) if sm: ans = min(ans, sum(a) + de) de += a[0] sm += sum(a) print(sm - 2 * ans)
Statement Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.
[{"input": "3\n 5 - 1 - 3", "output": "7\n \n\nThe maximum possible value is: 5 - (1 - 3) = 7.\n\n* * *"}, {"input": "5\n 1 - 2 + 3 - 4 + 5", "output": "5\n \n\nThe maximum possible value is: 1 - (2 + 3 - 4) + 5 = 5.\n\n* * *"}, {"input": "5\n 1 - 20 - 13 + 14 - 5", "output": "13\n \n\nThe maximum possible value is: 1 - (20 - (13 + 14) - 5) = 13."}]
Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. * * *
s675944755
Accepted
p03850
The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N
import sys input = sys.stdin.readline N = int(input()) terms = [[]] # マイナス区切り op = "+" for x in input().rstrip().split(): if x in "+-": op = x continue x = int(x) if op == "+": terms[-1].append(x) else: terms.append([x]) # 何をマイナスにするか min_minus = 10**18 first_sum = 0 for t in terms[1:]: x = first_sum + sum(t) if x < min_minus: min_minus = x first_sum += t[0] answer = sum(sum(t) for t in terms) if len(terms) > 1: answer -= 2 * min_minus print(answer)
Statement Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.
[{"input": "3\n 5 - 1 - 3", "output": "7\n \n\nThe maximum possible value is: 5 - (1 - 3) = 7.\n\n* * *"}, {"input": "5\n 1 - 2 + 3 - 4 + 5", "output": "5\n \n\nThe maximum possible value is: 1 - (2 + 3 - 4) + 5 = 5.\n\n* * *"}, {"input": "5\n 1 - 20 - 13 + 14 - 5", "output": "13\n \n\nThe maximum possible value is: 1 - (20 - (13 + 14) - 5) = 13."}]
Print the number of squares the bishop can reach. * * *
s899462847
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
a, b = [int(x) for x in input().split()] print((a * b + 1) // 2)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s826640293
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
HK = input().split(" ") H = int(HK[0]) K = int(HK[1]) if K % 2 == 1 and H % 2 == 1: print(round(H * K / 2) + 1) else: print(int(H * K / 2))
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s137113946
Accepted
p02742
Input is given from Standard Input in the following format: H \ W
import sys sys.setrecursionlimit(4100000) import math import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) def resolve(): # S = [x for x in sys.stdin.readline().split()][0] # 文字列 一つ # N = [int(x) for x in sys.stdin.readline().split()][0] # int 一つ H, W = [int(x) for x in sys.stdin.readline().split()] # 複数int # h_list = [int(x) for x in sys.stdin.readline().split()] # 複数int # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)] # 文字列grid # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)] # grid = [[int(x) for x in sys.stdin.readline().split()] # for _ in range(N)] # int grid logger.debug("{}".format([])) if H == 1 or W == 1: print(1) return a = (H // 2) * (W // 2) b = math.ceil(H / 2) * math.ceil(W / 2) print(a + b) if __name__ == "__main__": resolve() # AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す # python -m unittest template/template.py で実行できる # pypy3 -m unittest template/template.py で実行できる import sys from io import StringIO import unittest class TestClass(unittest.TestCase): def assertIO(self, input, output): stdout, stdin = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(input) resolve() sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, stdin self.assertEqual(out, output) def test_入力例_1(self): input = """4 5""" output = """10""" self.assertIO(input, output) def test_入力例_2(self): input = """7 3""" output = """11""" self.assertIO(input, output) def test_入力例_3(self): input = """1000000000 1000000000""" output = """500000000000000000""" self.assertIO(input, output)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s275900526
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
import glob # 問題ごとのディレクトリのトップからの相対パス REL_PATH = "ABC\\159\\B" # テスト用ファイル置き場のトップ TOP_PATH = "C:\\AtCoder" class Common: problem = [] index = 0 def __init__(self, rel_path): self.rel_path = rel_path def initialize(self, path): file = open(path) self.problem = file.readlines() self.index = 0 return def input_data(self): try: IS_TEST self.index += 1 return self.problem[self.index - 1] except NameError: return input() def resolve(self): pass def exec_resolve(self): try: IS_TEST for path in glob.glob(TOP_PATH + "\\" + self.rel_path + "/*.txt"): print("Test: " + path) self.initialize(path) self.resolve() print("\n\n") except NameError: self.resolve() class Solver(Common): def resolve(self): tmp = [int(i) for i in self.input_data().split()] H = tmp[0] W = tmp[1] count = int(H * W / 2) if H % 2 == 1 and W % 2 == 1: count += 1 print(str(count)) solver = Solver(REL_PATH) solver.exec_resolve()
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s614416215
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
h, w = map(int, input().split(" ")) a1 = 0 if (h == 1) or (w == 1): a1 = 0 elif ((h * w) % 2) == 1: a1 = ((h * w) // 2) + 1 elif ((h * w) % 2) == 0: a1 = (h * w) // 2 print(a1)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s902621096
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
h, w = tuple(map(int, input().split())) print((-w // 2) * (-h // 2) + (-(-w // 2) - 1) * (-(-h // 2) - 1))
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s342209614
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
i = list(map(int, input().split())) print(i[0] * i[1] / 2)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s018213705
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
(a, b) = [int(x) for x in input().split()] print((a + b + 1) // 2)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s109905390
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
H, W = map(int, input().split()) Ho = H // 2 He = H - Ho Wo = W // 2 We = W - Wo print(Ho * Wo + He * We)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s689010276
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
a, b = list(map(int, input("\nEnter the numbers : ").strip().split())) x = a * b if x % 2 != 0: x = x + 1 print(x // 2)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s509335557
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
HW = list(map(int, input().split())) H = HW[0] W = HW[1] if type(H) is int and type(W) is int: if 1 <= H and W <= 10**9 and 1 <= W: out = H * W if out % 2 == 0: out = out / 2 elif out == 1: out = 1 else: out = ((out - 1) / 2) + 1 print(int(out))
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s238537011
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
H, W = map(int, input().split()) H_0 = H % 2 W_0 = W % 2 H_1 = H // 2 W_1 = W // 2 x = 0 if (H_0 * W_0) == 1: x = (H * W + 1) / 2 else: x = H * W / 2 x = int(x) print(x)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s111669513
Runtime Error
p02742
Input is given from Standard Input in the following format: H \ W
print((int(input()) + 1) // 2 * (int(input()) + 1) // 2)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s815369694
Runtime Error
p02742
Input is given from Standard Input in the following format: H \ W
a = input() b = input() print(a / 2 * b)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s164604505
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
inp = list(map(int, input().split())) h = inp[0] w = inp[1] print((h // 2) * w)
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the number of squares the bishop can reach. * * *
s301834443
Wrong Answer
p02742
Input is given from Standard Input in the following format: H \ W
h, k = map(int, input().split()) if (h % 2 == 0) and (k % 2 == 0): print(h * k / 2) elif (h % 2 == 1) and (k % 2 == 0): print(h * k / 2) elif (h % 2 == 0) and (k % 2 == 1): print(h * k / 2) elif (h % 2 == 1) and (k % 2 == 1): print(int((h - 1) * (k - 1) / 2 + (h + k) / 2))
Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds: * r_1 + c_1 = r_2 + c_2 * r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: ![](https://img.atcoder.jp/panasonic2020/943f3f8428e6f8328924ff99681c932b.png)
[{"input": "4 5", "output": "10\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/b218e01560fe6e40e7d082ca57a64e6e.png)\n\n* * *"}, {"input": "7 3", "output": "11\n \n\nThe bishop can reach the cyan squares in the following figure:\n\n![](https://img.atcoder.jp/panasonic2020/088830f8a244d99a9f95d20bf9a8d336.png)\n\n* * *"}, {"input": "1000000000 1000000000", "output": "500000000000000000"}]
Print the maximum possible profit of Joisino's shop. * * *
s500299743
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
import sys sys.setrecursionlimit(1000000) # def input(): # return sys.stdin.readline()[:-1] """ n=int(input()) for i in range(n): a[i]=int(input()) a[i],b[i]=map(int,input().split()) a=[int(x) for x in input().split()] n,m=map(int,input.split()) from operator import itemgetter a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)] print(sorted(a)) # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る print(sorted(a, key=itemgetter(0))) # 0 番目の要素だけでソート print(sorted(a, key=itemgetter(0, 2))) # 0 番目と 2 番目の要素でソート print(sorted(a, key=lambda x: x[0] * x[2])) # 0 番目の要素 * 2 番目の要素でソート print(sorted(a, reverse=True)) # 降順にソート a.sort() # 破壊的にソート、sorted() よりも高速 try: # エラーキャッチ list index out of range for i in range(): k=b[i] except IndexError as e: print(i) """ test_data1 = """\ 1 1 1 0 1 0 0 0 1 0 1 3 4 5 6 7 8 9 -2 -3 4 -2 """ test_data2 = """\ 2 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 """ test_data3 = """\ """ td_num = 1 def GetTestData(index): if index == 1: return test_data1 if index == 2: return test_data2 if index == 3: return test_data3 if False: with open("../test.txt", mode="w") as f: f.write(GetTestData(td_num)) with open("../test.txt") as f: # Start Input code --------------------------------------- n = int(f.readline()) g = [[] for x in range(n)] p = [[] for x in range(n)] for i in range(n): g[i] = [int(x) for x in f.readline().split()] for i in range(n): p[i] = [int(x) for x in f.readline().split()] # End Input code --------------------------------------- else: for i in range(1): # Start Input code --------------------------------------- n = int(input()) g = [[] for x in range(n)] p = [[] for x in range(n)] for i in range(n): g[i] = [int(x) for x in input().split()] for i in range(n): p[i] = [int(x) for x in input().split()] # End Input code --------------------------------------- # print('n,g,p=',n,g,p) o = [0] * 10 ans = -(10**17) try: # エラーキャッチ list index out of range for i in range(1, 2**10): e = 0 for shop in range(n): cnt = 0 for t in range(10): o[t] = (i % (2 ** (t + 1))) // (2**t) cnt += g[shop][t] * o[t] e += p[shop][cnt] ans = max(ans, e) # if e==8: print(o,g[shop],cnt,e) print(ans) except IndexError as e: print(i, shop, cnt)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s915149947
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n): l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n): l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n): l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n): l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 # A def A(): n, a, b = LI() print(min(n * a, b)) return # B def B(): s = S() print(s.count("1")) return # C def C(): n = list(map(int, S())) k = 0 s = sum(n) for i in range(len(n)): k *= 10 k += n[i] if k % s == 0: print("Yes") else: print("No") return # D def D(): n = I() a = LI() ans = float("inf") for i in range(n): m = 0 while a[i] % 2 == 0: m += 1 a[i] //= 2 ans = min(m, ans) print(ans) return # E def E(): def dfs(n, k): if n == 10: l.append(k) else: dfs(n + 1, k + [0]) dfs(n + 1, k + [1]) n = I() f = LIR(n) p = LIR(n) l = [] dfs(0, []) ans = -float("inf") for k in l[1:]: m = 0 c = [0 for i in range(n)] for i in range(n): for j in range(10): c[i] += f[i][j] & k[j] m += p[i][c[i]] ans = max(m, ans) print(ans) return # F def F(): n, k = LI() a = LI() d = defaultdict(int) for i in a: d[i] += 1 d = list(d.values()) d.sort() ans = 0 for i in range(len(d) - k): ans += d[i] print(ans) return # G def G(): n, c = LI() ch = LIR(n) T = [[0 for i in range(100002)] for j in range(c + 1)] for s, t, cha in ch: T[cha - 1][s] += 1 T[cha - 1][t + 1] -= 1 for i in range(c): for j in range(100000): T[i][j + 1] += T[i][j] if T[i][j] >= 1: T[i][j] = 1 ans = 0 for i in range(100001): m = 0 for j in range(c + 1): m += T[j][i] ans = max(m, ans) print(ans) return # H def H(): n = I() a = LI() ma = a.index(max(a)) mi = a.index(min(a)) ans = [] if abs(a[ma]) > abs(a[mi]): ans.append([ma + 1, 1]) ans.append([ma + 1, 1]) a[0] += 2 * a[ma] for i in range(n - 1): ans.append([i + 1, i + 2]) ans.append([i + 1, i + 2]) a[i + 1] += 2 * a[i] else: ans.append([mi + 1, n]) ans.append([mi + 1, n]) a[-1] += 2 * a[mi] for i in range(n - 1)[::-1]: ans.append([i + 2, i + 1]) ans.append([i + 2, i + 1]) a[i] += 2 * a[i + 1] print(len(ans)) for i in ans: print(*i) return # I def I_(): n = I() print(1080 / (n - 1)) return # J def J(): h, w, n = LI() p = LIR(n) if n % 2 == 1: print(-1) quit() ans = [] for i in range(1, h + 1): c = 0 m = i / w for x, y in p: if m * x < y: c += 1 elif m * x == y: c = 0 break if c == n // 2: ans.append([w, i]) for i in range(1, w): c = 0 m = h / i for x, y in p: if m * x < y: c += 1 elif m * x == y: c = 0 break if c == n // 2: ans.append([i, h]) if len(ans) == 0: print(-1) quit() ans.sort() for x, y in ans: print("(" + str(x) + "," + str(y) + ")") return # K # L def L(): return # M def M(): return # Solve if __name__ == "__main__": E()
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s699640144
Wrong Answer
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
# -*- coding: utf-8 -*- ############# # Libraries # ############# import sys input = sys.stdin.readline import math # from math import gcd import bisect from collections import defaultdict from collections import deque from functools import lru_cache ############# # Constants # ############# MOD = 10**9 + 7 INF = float("inf") ############# # Functions # ############# ######INPUT###### def I(): return int(input().strip()) def S(): return input().strip() def IL(): return list(map(int, input().split())) def SL(): return list(map(str, input().split())) def ILs(n): return list(int(input()) for _ in range(n)) def SLs(n): return list(input().strip() for _ in range(n)) def ILL(n): return [list(map(int, input().split())) for _ in range(n)] def SLL(n): return [list(map(str, input().split())) for _ in range(n)] ######OUTPUT###### def P(arg): print(arg) return def Y(): print("Yes") return def N(): print("No") return def E(): exit() def PE(arg): print(arg) exit() def YE(): print("Yes") exit() def NE(): print("No") exit() #####Shorten##### def DD(arg): return defaultdict(arg) #####Inverse##### def inv(n): return pow(n, MOD - 2, MOD) ######Combination###### kaijo_memo = [] def kaijo(n): if len(kaijo_memo) > n: return kaijo_memo[n] if len(kaijo_memo) == 0: kaijo_memo.append(1) while len(kaijo_memo) <= n: kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD) return kaijo_memo[n] gyaku_kaijo_memo = [] def gyaku_kaijo(n): if len(gyaku_kaijo_memo) > n: return gyaku_kaijo_memo[n] if len(gyaku_kaijo_memo) == 0: gyaku_kaijo_memo.append(1) while len(gyaku_kaijo_memo) <= n: gyaku_kaijo_memo.append( gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo), MOD - 2, MOD) % MOD ) return gyaku_kaijo_memo[n] def nCr(n, r): if n == r: return 1 if n < r or r < 0: return 0 ret = 1 ret = ret * kaijo(n) % MOD ret = ret * gyaku_kaijo(r) % MOD ret = ret * gyaku_kaijo(n - r) % MOD return ret ######Factorization###### def factorization(n): arr = [] temp = n for i in range(2, int(-(-(n**0.5) // 1)) + 1): if temp % i == 0: cnt = 0 while temp % i == 0: cnt += 1 temp //= i arr.append([i, cnt]) if temp != 1: arr.append([temp, 1]) if arr == []: arr.append([n, 1]) return arr #####MakeDivisors###### def make_divisors(n): divisors = [] for i in range(1, int(n**0.5) + 1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n // i) return divisors #####GCD##### def gcd(a, b): while b: a, b = b, a % b return a #####LCM##### def lcm(a, b): return a * b // gcd(a, b) #####BitCount##### def count_bit(n): count = 0 while n: n &= n - 1 count += 1 return count #####ChangeBase##### def base_10_to_n(X, n): if X // n: return base_10_to_n(X // n, n) + [X % n] return [X % n] def base_n_to_10(X, n): return sum(int(str(X)[-i - 1]) * n**i for i in range(len(str(X)))) #####IntLog##### def int_log(n, a): count = 0 while n >= a: n //= a count += 1 return count ############# # Main Code # ############# N = I() F = ILL(N) P = ILL(N) for i in range(N): F[i] = base_n_to_10(sum(F[i][j] * 10**j for j in range(10)), 2) ans = 0 for b in range(1, 1 << 10): rieki = 0 for i in range(N): match = count_bit(F[i] & b) rieki += P[i][match] ans = max(ans, rieki) print(ans)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s121348504
Wrong Answer
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
import numpy as np
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s920671786
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
import copy N = int(input()) F = [[[0 for i in range(3)] for j in range(6)] for k in range(N + 1)] # print(F) for i in range(1, N + 1): line = [int(k) for k in input().split()] for j in range(1, 6): for k in range(1, 3): F[i][j][k] = line[2 * (j - 1) + (k - 1)] P = [[0 for i in range(11)] for j in range(N + 1)] for i in range(1, N + 1): line = [int(j) for j in input().split()] for k in range(11): P[i][k] = line[k] # print(P) # print(F) stack = [] stack.append([True]) stack.append([False]) rieki_max = float("inf") # print(stack) while 1: if len(stack) == 0: break kumiawase = stack.pop() if len(kumiawase) == 10: # 利益計算 if kumiawase.count(True) == 0: continue kaburi = [0 for i in range(N + 1)] for i in range(1, 6): for j in range(1, 3): if kumiawase[(i - 1) * 2 + j - 1] == False: continue for k in range(1, N + 1): if F[k][i][j] == 1: kaburi[k] += 1 rieki = 0 # print(kaburi) for i in range(0, N + 1): rieki += P[i][kaburi[i]] # print(kumiawase,kaburi,rieki) if rieki_max < rieki or rieki_max == float("inf"): rieki_max = rieki else: tmp_a = copy.deepcopy(kumiawase) tmp_a.append(True) stack.append(tmp_a) tmp_b = copy.deepcopy(kumiawase) tmp_b.append(False) stack.append(tmp_b) print(rieki_max)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s038336684
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
# https://atcoder.jp/contests/abc080/tasks/abc080_c # 各店の各曜日の各営業時間で営業するかどうか全探索 N = int(input()) # 店の数 F_list = [] for _ in range(N): f = list(map(int, input().split())) F_list.append(f) P_list = [] for _ in range(N): p = list(map(int, input().split())) P_list.append(p) # # 各曜日時間帯の最低限の店の開店数(おねーちゃんの店を考慮しない) # min_open_list = [] # for i in range(10): # count_open = 0 # for j in range(N): # if F_list[j][i] == 1: # count_open += 1 # min_open_list.append(count_open) # c個の店が営業中の時の、店iの利益 def benefit(i, c): return P_list[i][c] is_joisino_shop_open_list = [False] * 10 benefit_joisino_list = [] def dfs(j): if j >= 10: # 1つ以上の時間帯でお店を営業していること if True in is_joisino_shop_open_list: # joisinoおねえちゃんのお店の利益を計算 benefit_joisino = 0 for n in range(N): # 各お店ごとのjoisinoお姉ちゃんの利益を計算 count_n_open = 0 shop_n_list = F_list[n] for i in range(10): if shop_n_list[i] == 1 and is_joisino_shop_open_list[i] == True: count_n_open += 1 benefit_n_joisino = P_list[n][count_n_open] benefit_joisino += benefit_n_joisino benefit_joisino_list.append(benefit_joisino) return # joisinoお姉ちゃんのお店をあけるかあけないか全パターン試す # j番目の日時にjoisinoお姉ちゃんのお店をあけるとき is_joisino_shop_open_list[j] = True dfs(j + 1) # j番目の日時にjoisinoお姉ちゃんのお店をあけないとき is_joisino_shop_open_list[j] = False dfs(j + 1) dfs(0) max_benefit_joisino = max(benefit_joisino_list) print(max_benefit_joisino)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s687536127
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
import sys import itertools def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 n = int(readline()) f = [list(map(int, readline().split())) for _ in range(n)] p = [list(map(int, readline().split())) for _ in range(n)] mt = - (10 ** 10) for it in itertools.product([True, False], repeat=10): if sum(it) != 0: ls = [0] * n(n): ls[j] += 1 if f[j][i] == 1 and iv else 0 t = 0 for i in range(n): t += p[i][ls[i]] mt = max(mt, t) print(mt) if __name__ == '__main__': solve()
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s760191469
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
from collections import Counter,defaultdict,deque import sys import bisect import math import itertools import string import queue import copy import numpy as np # import scipy from itertools import permutations, combinations from heapq import heappop, heappush input = sys.stdin.readline sys.setrecursionlimit(10**8) mod = 10**9+7 def inp(): # n=1 return int(input()) def inpm(): # x=1,y=2 return map(int,input().split()) def inpl(): # a=[1,2,3,4,5,...,n] return list(map(int, input().split())) def inpls(): # a=['1','2','3',...,'n'] return list(input().split()) def inplm(n): # x=[] 複数行 return list(int(input()) for _ in range(n)) def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] return [list(map(int, input().split())) for _ in range(n)] def main(): n=inp() f=[] for _ in range(n): f.append(inpl()) p=[] for _ in range(n): p.append(inpl()) ans=-10**10 for i in range(1,11): for j in combinations(range(10),i): do = [0 for _ in range(10)] for k in j: do[k]=1 benefit = 0 for l in range(n): s=0 for u in range(10): s+=f[l][u]*do[u] benefit += p[l][s] ans=max(ans,benefit) print(ans) if __name__ == "__main__": main()from collections import Counter,defaultdict,deque import sys import bisect import math import itertools import string import queue import copy import numpy as np # import scipy from itertools import permutations, combinations from heapq import heappop, heappush input = sys.stdin.readline sys.setrecursionlimit(10**8) mod = 10**9+7 def inp(): # n=1 return int(input()) def inpm(): # x=1,y=2 return map(int,input().split()) def inpl(): # a=[1,2,3,4,5,...,n] return list(map(int, input().split())) def inpls(): # a=['1','2','3',...,'n'] return list(input().split()) def inplm(n): # x=[] 複数行 return list(int(input()) for _ in range(n)) def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] return [list(map(int, input().split())) for _ in range(n)] def main(): n=inp() f=[] for _ in range(n): f.append(inpl()) p=[] for _ in range(n): p.append(inpl()) ans=-10**10 for i in range(1,11): for j in combinations(range(10),i): do = [0 for _ in range(10)] for k in j: do[k]=1 benefit = 0 for l in range(n): s=0 for u in range(10): s+=f[l][u]*do[u] benefit += p[l][s] ans=max(ans,benefit) print(ans) if __name__ == "__main__": main()
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s123766396
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
n = int(input()) f = [list(map(int, input().split())) for i in range(n)] p = [list(map(int, input().split())) for j in range(n)] ans = -9999999999 for i in range(1, 1024): bf = 0 for j in range(n): cnt = 0 for k in range(10): if (i >> k) & 1 == 1 and f[j][k] == 1: cnt += 1 bf += p[j][cnt] ans =max(ans, bf)   print(ans)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s875214439
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
N=int(input()) F=[ [[0 for _ in range(2)] for _ in range(5)] for _ in range(N)] P= [[0 for _ in range(10)] for _ in range(N)] total= [[0 for _ in range(5)] for _ in range(2)] for i in range(N): tmp=list( map(int,input().split()) ) for j in range(5): for k in range(2): F[i][j][k]=tmp[j+k] for i in range(N): P[i]=list(map(int,input().split())) for i in range(N): tmp=0 for j in range(5): for k in range(2): tmp +=
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s643272828
Wrong Answer
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
N = int(input())
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s406725167
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
N = int(input()) F = [list(map(int, input().split())) for i in range(N)] P = [list(map(int, input().split())) for i in range(N)] l = [format(i, 'b').zfill(10) for i in range(1, 2 ** 10)] ans = [] for i in l: c = [0] * for j in range(10): if i[j] == '1': for k in range(N): if F[k][j] == 1: c[k] += 1 else: continue else: continue temp = 0 for j in range(N): temp += P[j][c[j]] ans.append(temp) print(max(ans))
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s471545888
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
N = int(input()) F = [] P = [] for i in range(N): f = list(map(int,input().split())) F.append(f) for i in range(N): p = list(map(int,input().split())) P.append(p) value = 0 ans = - (10 ** 9) def recur(list): con = len(list) #list = [0,0, ... ~ 0] ~ [1,1, ... ~ 1] if con == 10: #1がない = 出店していない場合は何も返さない if list.count(1) == 0: return value = 0 #インデックス番号とF[i] = 店の出店リストを取得 for n,shop in enumerate(F): con = 0 for j in range(10): #j日に自店が出店し、かつ他店nも出店するならば、カウントする if list[j] == 1 and shop[j] == 1: con += 1 #出店リストlistのパターンに応じた店nごとの利益を #同時出店日数conから算出し足し合わせる value += P[n][con] global ans   ans = max(ans, value) else: #出店しない recur(list + [0]) #出店する recur(list + [1]) recur([]) print(ans)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s790592609
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
#include<iostream> #include<vector> #include<bitset> #include<algorithm> #include<cmath> using namespace std; typedef vector< vector<int> > vv; int main(){ int n;cin >> n; vv f(n,vector<int>(10,0)); for(int i=0;i<n;i++)for(int j=0;j<10;j++)cin >> f[i][j]; vv p(n,vector<int>(11,0)); for(int i=0;i<n;i++)for(int j=0;j<=10;j++)cin >> p[i][j]; int ma=-1000000000; int bits=pow(2,10); for(int i=1;i<bits;i++){ bitset<10> bit(i); vector<int> v(n); for(int j=0;j<10;j++){ if(bit[j]){ for(int k=0;k<n;k++){ v[k]+=f[k][j]; } } } int ma_sub=0; for(int l=0;l<n;l++){ ma_sub+=p[l][v[l]]; } ma=max(ma,ma_sub); } cout << ma << endl; }
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s327097490
Runtime Error
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
N = int(input()) F = [list(map(int, input().split())) for _ in range(N)] P = [list(map(int, input().split())) for _ in range(N)] def dfs(i, A, ans): if i == 10: ans.append(A) return ans B = A[:] B.append(0) ans = dfs(i+1, B, ans) C = A[:] C.append(1) ans = dfs(i+1, C, ans) return ans def main() D = dfs(0, [], []) for ind, A in enumerate(D): if ind == 0: continue gain = 0 for i in range(N): c = 0 for day in range(10): c += F[i][day]*A[day] gain += P[i][c] if ind == 1: ans = gain continue ans = max(ans, gain) print(ans) if __name__ == "__main__": main()
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s969125709
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
import copy shop_num = int(input().rstrip()) # business hour shop_data = {} for i in range(shop_num): shop_data[i] = input().rstrip().split(" ") # benefit shop_benefit = {} for i in range(shop_num): shop_benefit[i] = input().rstrip().split(" ") patterns = [0, 1, 2, 3] targets = [] for pattern1 in patterns: list1 = [] if pattern1 == 1: list1.append(0) elif pattern1 == 2: list1.append(1) elif pattern1 == 3: list1.append(0) list1.append(1) for pattern2 in patterns: list2 = copy.deepcopy(list1) if pattern2 == 1: list2.append(2) elif pattern2 == 2: list2.append(3) elif pattern2 == 3: list2.append(2) list2.append(3) for pattern3 in patterns: list3 = copy.deepcopy(list2) if pattern3 == 1: list3.append(4) elif pattern3 == 2: list3.append(5) elif pattern3 == 3: list3.append(4) list3.append(5) for pattern4 in patterns: list4 = copy.deepcopy(list3) if pattern4 == 1: list4.append(6) elif pattern4 == 2: list4.append(7) elif pattern4 == 3: list4.append(6) list4.append(7) for pattern5 in patterns: list5 = copy.deepcopy(list4) if pattern5 == 1: list5.append(8) elif pattern5 == 2: list5.append(9) elif pattern5 == 3: list5.append(8) list5.append(9) targets.append(copy.deepcopy(list5)) ben_list = [] for target in targets: if len(target) <= 0: continue ben = 0 for key in shop_data: count = 0 for time in range(10): data = int(shop_data[key][time]) if time in target: if data == 1: count += 1 ben += int(shop_benefit[key][count]) ben_list.append(ben) print(max(ben_list))
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s253385851
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
n = int(input()) f = [] for _ in [0] * n: f += [list(map(int, input().split()))] p = [] for _ in [0] * n: p += [list(map(int, input().split()))] m = -10000001 * n for x in range(1, 1024): k = 0 s = format(x, "010b") for t in range(n): k += p[t][sum([int(s[i]) * f[t][i] for i in range(10)])] m = max(m, k) print(m)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the maximum possible profit of Joisino's shop. * * *
s027971099
Accepted
p03503
Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10}
count = int(input().rstrip()) shops = [input().split() for i in range(count)] gain = [input().split() for i in range(count)] max = -1000000000 for k in range(1, 1024): tmp = format(k, "b").zfill(10) tmp_sum = 0 for N in range(count): shop_count = 0 for l in range(10): if tmp[l] == shops[N][l] and tmp[l] == "1": shop_count += 1 tmp_sum += int(gain[N][shop_count]) if max < tmp_sum: max = tmp_sum print(max)
Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N. You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2. Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}. Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.
[{"input": "1\n 1 1 0 1 0 0 0 1 0 1\n 3 4 5 6 7 8 9 -2 -3 4 -2", "output": "8\n \n\nIf her shop is open only during the periods when Shop 1 is opened, the profit\nwill be 8, which is the maximum possible profit.\n\n* * *"}, {"input": "2\n 1 1 1 1 1 0 0 0 0 0\n 0 0 0 0 0 1 1 1 1 1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1", "output": "-2\n \n\nNote that a shop must be open during at least one period, and the profit may\nbe negative.\n\n* * *"}, {"input": "3\n 1 1 1 1 1 1 0 0 1 1\n 0 1 0 1 1 1 1 0 1 0\n 1 0 1 1 0 1 0 1 0 1\n -8 6 -2 -8 -8 4 8 7 -6 2 2\n -9 2 0 1 7 -5 0 -2 -6 5 5\n 6 -6 7 -9 6 -5 8 0 -9 -7 -7", "output": "23"}]
Print the minimum possible total cost. * * *
s600886553
Accepted
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
from itertools import accumulate from operator import itemgetter n, d = map(int, input().split()) aaa = list(map(int, input().split())) costs_l = [(-i * d + a, i) for i, a in enumerate(aaa)] costs_r = [(i * d + a, i) for i, a in enumerate(aaa)] costs_l = list(accumulate(costs_l, min)) costs_r = list(accumulate(reversed(costs_r), min)) costs_r.reverse() hubs = set(map(itemgetter(1), costs_l)) hubs.intersection_update(map(itemgetter(1), costs_r)) hubs.add(0) hubs.add(n - 1) hubs = sorted(hubs) ans = sum(aaa) - aaa[-1] s = hubs[0] for t in hubs[1:]: cls = costs_l[s][0] crt = costs_r[t][0] ans += crt - s * d ans += sum(min(cls + i * d, crt - i * d) for i in range(s + 1, t)) s = t print(ans)
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s084740074
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
import sys input = sys.stdin.readline N, D = map(int, input().split()) A = list(map(int, input().split())) # cost=abs(i-j)*D+A[i]+A[j] SUMDA = [D * i - A[-i] for i in range(1, N + 1)] SUMDA.reverse() MIN = SUMDA[-1] MININD = [None] * N MININD[N - 1] = N - 1 for i in range(N - 2, -1, -1): if SUMDA[i] < MIN: MIN = SUMDA[i] MININD[i] = i else: MININD[i] = MININD[i + 1] SUMA = sum(A) ANS = float("inf") for i in range(N - 2): cost = 0 k = MININD[i + 1] cost = D * (N - 1) + D * (N - 1 - abs(k - i)) + SUMA * 2 - A[i] - A[k] # print(cost) if ANS > cost: ANS = cost print(ANS)
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s901567296
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
def main(): import sys input = sys.stdin.readline class UnionFind: def __init__(self, n): self.n = n self.root = [-1] * (n + 1) self.rnk = [0] * (n + 1) def find_root(self, x): if self.root[x] < 0: return x else: self.root[x] = self.find_root(self.root[x]) return self.root[x] def unite(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return elif self.rnk[x] > self.rnk[y]: self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if self.rnk[x] == self.rnk[y]: self.rnk[y] += 1 def isSameGroup(self, x, y): return self.find_root(x) == self.find_root(y) def size(self, x): return -self.root[self.find_root(x)] N, D = map(int, input().split()) A = list(map(int, input().split())) A_plus = [0] * N A_minus = [0] * N for i in range(N): A_plus[i] = A[i] + D * i A_minus[i] = A[i] - D * i edge = [] m = float("inf") m_idx = 0 for i in range(N - 1): if A_minus[i] < m: m = A_minus[i] m_idx = i edge.append((m_idx + 1, i + 2, A_plus[i + 1] + A_minus[m_idx])) m = float("inf") m_idx = 0 for i in range(N - 1, 0, -1): if A_plus[i] < m: m = A_plus[i] m_idx = i edge.append((m_idx + 1, i, A_plus[m_idx] + A_minus[i - 1])) edge.sort(key=lambda x: x[2]) UF = UnionFind(N + 1) ans = 0 for u, v, cost in edge: if UF.isSameGroup(u, v): continue ans += cost UF.unite(u, v) print(ans) if __name__ == "__main__": main()
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s293945633
Accepted
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
# E N, D = map(int, input().split()) A_list = list(map(int, input().split())) # minimum spanning tree res = 0 # Prim based B_list = [0] * N for i in range(N): B_list[i] = A_list[i] + D * i C_list = [0] * N for i in range(N): C_list[i] = A_list[i] + D * (N - i) # cummin seen from left B_cummmin = [0] * N R = B_list[N - 1] BA = N - 1 for i in range(N - 1, -1, -1): if B_list[i] <= R: R = B_list[i] BA = i B_cummmin[i] = BA # cummin seen from right C_cummmin = [0] * N R = C_list[0] CA = 0 for i in range(N): if C_list[i] <= R: R = C_list[i] CA = i C_cummmin[i] = CA # start from 0 start = 0 while start < N - 1: end = B_cummmin[start + 1] target = C_cummmin[start] res += D * (end - target) + A_list[target] + A_list[end] for i in range(start + 1, end): ds = D * (i - target) + A_list[target] + A_list[i] de = D * (end - i) + A_list[end] + A_list[i] res += min(ds, de) start = end print(res)
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s187561487
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
# Union-Find ****0スタートなので注意!**** class UnionFind: # 作りたい要素数(ノードの数)nで初期化 # 使用するインスタンス変数の初期化 def __init__(self, n): self.n = n # root[x]<0ならそのノードが根かつその値が木の要素数 # rootノードでその木の要素数を記録する self.root = [-1] * ( n ) # 元々はn+1になっていたが、nにした(n+1なら1スタートで使えるかもしれないが、Count_Trees()がおかしくなるのでやめた) # 木をくっつける時にアンバランスにならないように調整する self.rnk = [0] * (n) # 元々はn+1になっていたが、nにした # ノードxのrootノードを見つける def Find_Root(self, x): if self.root[x] < 0: return x else: # ついでに経路圧縮(調べた辺を根に直接つなぎ直す)をかけておく self.root[x] = self.Find_Root(self.root[x]) return self.root[x] # 木の併合、入力は併合したい集合に属する各ノード def Unite(self, x, y): # 入力ノードのrootノードを見つける x = self.Find_Root(x) y = self.Find_Root(y) # すでに同じ木に属していた場合、何もしないで返す if x == y: return # 違う木に属していた場合rnkを見てくっつける方を決める elif self.rnk[x] > self.rnk[y]: self.root[x] += self.root[y] # 要素数を足す(根のrootは"-要素数") self.root[y] = x # ノードyの親をx(新しい集合の根)とする else: self.root[y] += self.root[x] # 要素数を足す self.root[x] = y # rnkが同じ(深さに差がない場合)は1増やす(根(rnk1)+繋げる集合のrnkということで、1増えることになる) if self.rnk[x] == self.rnk[y]: self.rnk[y] += 1 # xとyが同じグループに属するか判断 def isSameGroup(self, x, y): return self.Find_Root(x) == self.Find_Root(y) # ノードxが属する木のサイズを返す(根ノードの.root[idx]を-符号を外して返すだけ def Count(self, x): return -self.root[self.Find_Root(x)] # -符号を外す # 木の数(=集合の数)を返す(根の数=rootに-符号を持つものの数を返すだけ) def Count_Trees(self): treecnt = 0 for eachroot in self.root: if eachroot < 0: treecnt += 1 return treecnt
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s815245275
Runtime Error
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
###template### import sys def input(): return sys.stdin.readline().rstrip() from collections import defaultdict, Counter from itertools import product, groupby, count, permutations, combinations from math import pi, sqrt, ceil, floor from collections import deque from bisect import bisect, bisect_left, bisect_right from string import ascii_lowercase import heapq INF = float("inf") sys.setrecursionlimit(10**7) # 4近傍(右, 下, 左, 上) dy = [0, -1, 0, 1] dx = [1, 0, -1, 0] def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W def mi(): return map(int, input().split()) def ii(): return int(input()) ###template### N, D = mi() #As[i] = [各都市のコスト,各都市の番号] #0-indexed As = [[a, i] for i, a in enumerate(list(mi()))] #Union-Find ****0スタートなので注意!**** class UnionFind(): # 作りたい要素数(ノードの数)nで初期化 # 使用するインスタンス変数の初期化 def __init__(self, n): self.n = n # root[x]<0ならそのノードが根かつその値が木の要素数 # rootノードでその木の要素数を記録する self.root = [-1]*(n) #元々はn+1になっていたが、nにした(n+1なら1スタートで使えるかもしれないが、Count_Trees()がおかしくなるのでやめた) # 木をくっつける時にアンバランスにならないように調整する self.rnk = [0]*(n) #元々はn+1になっていたが、nにした # ノードxのrootノードを見つける def Find_Root(self, x): if(self.root[x] < 0): return x else: # ついでに経路圧縮(調べた辺を根に直接つなぎ直す)をかけておく self.root[x] = self.Find_Root(self.root[x]) return self.root[x] # 木の併合、入力は併合したい集合に属する各ノード def Unite(self, x, y): # 入力ノードのrootノードを見つける x = self.Find_Root(x) y = self.Find_Root(y) # すでに同じ木に属していた場合、何もしないで返す if(x == y): return # 違う木に属していた場合rnkを見てくっつける方を決める elif(self.rnk[x] > self.rnk[y]): self.root[x] += self.root[y] #要素数を足す(根のrootは"-要素数") self.root[y] = x #ノードyの親をx(新しい集合の根)とする else: self.root[y] += self.root[x] #要素数を足す self.root[x] = y # rnkが同じ(深さに差がない場合)は1増やす(根(rnk1)+繋げる集合のrnkということで、1増えることになる) if(self.rnk[x] == self.rnk[y]): self.rnk[y] += 1 # xとyが同じグループに属するか判断 def isSameGroup(self, x, y): return self.Find_Root(x) == self.Find_Root(y) # ノードxが属する木のサイズを返す(根ノードの.root[idx]を-符号を外して返すだけ def Count(self, x): return -self.root[self.Find_Root(x)] #-符号を外す #As_規模降順 = [各都市の規模, 各都市の番号(降順ソート前の番号)] from operator import itemgetter As_costdescending = sorted(As, key=itemgetter(0), reverse=True) #SegTreeはやめて、累積最小値を使う cummin_left = [0] * N cummin_right = [0] * N tmpmin = INF tmpminidx = -1 for i in range(0, N-1): #leftなので、0~N-2を更新 if (N-i)*D+As[i][0] <= tmpmin: tmpmin = (N-i)D+As[i][0] tmpminidx = i cummin_left[i] = tmpminidx tmpmin = INF tmpminidx = -1 for i in range(N-1, 0, -1): #rightなので、N-1~1を更新 # print(i+As[i][0]) if i*D+As[i][0] <= tmpmin: tmpmin = i*D+As[i][0] tmpminidx = i cummin_right[i] = tmpminidx #print(cummin_left) #print(cummin_right) #[(cost, idx, adjidx), ...] edge_candidates = [] for a, o_idx in As_costdescending: #o_idx:降順にする前のidx if o_idx == 0: #右だけを見る rightmin_oidx = cummin_right[1] edge_candidates.append((abs(o_idx-rightmin_oidx)*D+a+As[rightmin_oidx][0], o_idx, rightmin_oidx)) elif o_idx == N-1: #左だけを見る leftmin_oidx = cummin_left[N-2] edge_candidates.append((abs(o_idx-leftmin_oidx)*D+a+As[leftmin_oidx][0], o_idx, leftmin_oidx)) else: leftmin_oidx = cummin_left[o_idx-1] # print('+0:', o_idx) # print('+1:', o_idx+1, cummin_right[o_idx+1]) rightmin_oidx = cummin_right[o_idx+1] edge_candidates.append((abs(o_idx-leftmin_oidx)*D+a+As[leftmin_oidx][0], o_idx, leftmin_oidx)) edge_candidates.append((abs(o_idx-rightmin_oidx)*D+a+As[rightmin_oidx][0], o_idx, rightmin_oidx)) heapq.heapify(edge_candidates) #上でできた配列に対してクラスカル法を適用して最小全域木(のコスト総和)を求める #Union-Findを使ったクラスカル法 uf = UnionFind(N) ans = 0 while edge_candidates: c, idx, adjidx = heapq.heappop(edge_candidates) if not uf.isSameGroup(idx, adjidx): ans += c uf.Unite(idx, adjidx) print(ans)
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s678642802
Runtime Error
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
import numpy as np InputData1 = input() InputData2 = input() Lines = [data.split(" ") for data in [InputData1, InputData2]] InputNumbers = [int(numbers) for line in Lines for numbers in line] N = InputNumbers[0] D = InputNumbers[1] A = InputNumbers[2:] # print(N, D, A) def NetworkMatrix(Num, vector): matrix = np.empty((Num, Num)) for i in range(Num): ai = np.empty(Num) for j in range(Num): if i < j: aij = (j - i) * D + vector[i] + vector[j] else: aij = 0 ai[j] = aij matrix[i] = ai return matrix def Explore(Num, matrix): ResultMatrix = matrix for i in range(Num): # ResultVector[i] = min(matrix.T[i]) for j in range(Num): for k in range(Num): if i < j < k and min([matrix[i, j], matrix[j, k], matrix[i, k]]) != 0: if matrix[i, j] >= matrix[j, k] and matrix[i, j] >= matrix[i, k]: ResultMatrix[i, j] = 0 elif matrix[j, k] > matrix[i, j] and matrix[j, k] >= matrix[i, k]: ResultMatrix[j, k] = 0 elif matrix[i, k] > matrix[i, j] and matrix[i, k] > matrix[j, k]: ResultMatrix[i, k] = 0 if np.sum(ResultMatrix != matrix) != 0: ResultMatrix = Explore(Num, matrix) return ResultMatrix Matrix = NetworkMatrix(N, A) # print(Matrix) # Raveled = Matrix.ravel() # Answer = sum(sorted(Raveled)[:N-1]) AnswerMatrix = Explore(N, Matrix) # print(AnswerMatrix) print(int(np.sum(AnswerMatrix)))
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s888444538
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
import sys from collections import defaultdict as dd input = sys.stdin.readline N, D = map(int, input().split()) a = list(map(int, input().split())) dl = [0] * N dr = [0] * N base = 10**6 for i in range(N): dl[i] = ((N - i) * D + a[i]) * base + i dr[i] = (i * D + a[i]) * base + i e = dd(list) class SegTree: def segfunc(self, x, y): return min(x, y) def __init__(self, n, ide_ele, init_val): #####単位元###### self.ide_ele = ide_ele # num:n以上の最小の2のべき乗 self.num = 2 ** (n - 1).bit_length() self.seg = [self.ide_ele] * 2 * self.num # set_val for i in range(n): self.seg[i + self.num - 1] = init_val[i] # built for i in range(self.num - 2, -1, -1): self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) def update(self, k, x): k += self.num - 1 self.seg[k] = x while k + 1: k = (k - 1) // 2 self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) def query(self, p, q): if q <= p: return self.ide_ele p += self.num - 1 q += self.num - 2 res = self.ide_ele while q - p > 1: if p & 1 == 0: res = self.segfunc(res, self.seg[p]) if q & 1 == 1: res = self.segfunc(res, self.seg[q]) q -= 1 p = p // 2 q = (q - 1) // 2 if p == q: res = self.segfunc(res, self.seg[p]) else: res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q]) return res lseg = SegTree(N, pow(10, 20), dl) rseg = SegTree(N, pow(10, 20), dr) for i in range(N): l = lseg.query(0, i) r = rseg.query(i + 1, N) e[i].append((l % base, a[i] + l // base - D * (N - i))) e[l % base].append((i, a[i] + l // base - D * (N - i))) e[i].append((r % base, a[i] + r // base - D * i)) e[r % base].append((i, a[i] + r // base - D * i)) import heapq class prim: def __init__(self, n, e): self.e = e self.n = n def MSTcost(self): h = [] visited = [0] * (self.n + 1) ks = list(self.e.keys()) b = pow(10, 6) for edge in self.e[ks[0]]: heapq.heappush(h, edge[1] * b + edge[0]) res = 0 visited[ks[0]] = 1 while len(h): p = heapq.heappop(h) p0 = p // b p1 = p % b if visited[p1]: continue visited[p1] = 1 for q in self.e[p1]: if visited[q[0]]: continue heapq.heappush(h, q[1] * b + q[0]) res += p0 return res # print(e) pri = prim(N, e) print(pri.MSTcost())
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s346509869
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
def main(): N, D = tuple(map(int, input().split())) A = tuple(map(int, input().split())) A2 = [[A[i], i, 0] for i in range(N)] A2.sort() for i in range(N): A2[i][2] = i A2.sort(key=lambda x: x[1]) A2 = tuple(A2) rt = A[N - 1] Ll = [N - 1] k = N - 1 for i in range(N - 2, 0, -1): t = A[i] if rt > t - (k - i) * D: Ll.append(i) k = i rt = t rt = A[0] Lr = [0] k = 0 for i in range(1, N - 1): t = A[i] if rt > t + (k - i) * D: Lr.append(i) k = i rt = t Ll = tuple(Ll) Lr = tuple(Lr) L = [] lC = -1 rC = -1 for i in range(N - 1): if i >= Ll[lC]: lC -= 1 t = Ll[lC] if A2[i][2] > A2[t][2]: L.append((A[i] + A[t] + (t - i) * D, i, t)) k = N - 1 - i if k <= Lr[rC]: rC -= 1 t = Lr[rC] if A2[k][2] > A2[t][2]: L.append((A[k] + A[t] + (k - t) * D, t, k)) L.sort() T = [i for i in range(N)] C = 0 A = 0 for i in L: xR = i[1] xC = 0 yR = i[2] yC = 0 while T[xR] != xR: xC += 1 xR = T[xR] while T[yR] != yR: yC += 1 yR = T[yR] if xR != yR: if xC < yC: T[xR] = yR else: T[yR] = xR if xR != yR: C += 1 A += i[0] # print(i) if C == N - 1: break print(A) main()
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the minimum possible total cost. * * *
s649991183
Wrong Answer
p03153
Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N
# from typing import Callable def connecting_cities(N: int, D: int, A: list) -> int: edges = select_edges(N, D, A) edges = sorted(edges, key=lambda x: x[2], reverse=True) print(edges) uft = UnionFindTree(N) count = 0 while not len(edges) == 0: u, v, d = edges.pop() if not uft.same(u, v): uft.summarize(u, v) count += d return count def select_edges(N: int, D: int, A: list) -> list: INF = 1 << 32 st_l = SegmentTree(N, lambda x, y: x if x[0] < y[0] else y, (INF, -1)) st_r = SegmentTree(N, lambda x, y: x if x[0] < y[0] else y, (INF, -1)) indexes_A = sorted([i for i in range(N)], key=lambda x: A[x]) # edges = [] for i in indexes_A: la, li = st_l.range(0, i) if la < INF and li != -1: # edges.append( yield (li, i, abs(i - li) * D + A[i] + A[li]) # ) ra, ri = st_r.range(i, N) if ra < INF and ri != -1: # edges.append( yield (ri, i, abs(i - ri) * D + A[i] + A[ri]) # ) st_l.update(i, (A[i] + (N - i) * D, i)) st_r.update(i, (A[i] + i * D, i)) # return edges class SegmentTree: # def __init__(self, size: int, op: Callable[[any, any], any], init: any = 0): def __init__(self, size, op, init): """initialize SegmentTree :param size: Size of tree. This must be natural number. :param op: Operator which compares two numbers. This function return the representative value of two arguments. :param init: The initial value of element of tree. """ if size < 1: raise Exception("size must be greater than 0 (actual = %d)" % size) self.__treesize = 1 self.__init = init # tree size is the minimum number which is greater than or equal to # designed `size` and is power of 2. while self.__treesize < size: self.__treesize *= 2 self.__size = self.__treesize self.__treesize = self.__treesize * 2 - 1 self.__comp = op # initialize tree self.__tree = [init for _ in range(self.__treesize)] def range(self, l: int, r: int) -> int: """Returns the representative value in range [l, r) :param l: left value(include) :param r: right value(not include) :return: the representative value in range[l, r) """ return self.__range(l, r, 0, 0, self.__size) def __range(self, l: int, r: int, k: int, kl: int, kr: int) -> int: """ :param l: left value (include) :param r: right value (not include) :param k: node index :param kl: left value of node :param kr: right value of node """ if kr <= l or r <= kl: # no crossing return self.__init if l <= kl and kr <= r: # including whole k's range return self.__tree[k] vl = self.__range(l, r, 2 * k + 1, kl, (kl + kr) // 2) vr = self.__range(l, r, 2 * k + 2, (kl + kr) // 2, kr) if vl is None: return vr if vr is None: return vl return self.__comp(vl, vr) def update(self, index: int, val: int): """update value at self.__tree[index]'s value""" index += self.__size - 1 self.__tree[index] = val while index > 0: index = (index - 1) // 2 self.__tree[index] = self.__comp( self.__tree[2 * index + 1], self.__tree[2 * index + 2] ) def print(self): """for debug""" print(self.__tree) class UnionFindTree: def __init__(self, size: int): """UnionFindTreeを初期化します :param size: サイズ :return: UnionFindTree """ if size < 0: raise Exception("size must be greater than 0.") self.__parent = [-1] * size def summarize(self, a: int, b: int): """aを含む木とbを含む木をまとめます :param a: 木a :param b: 木b """ a = self.__root(a) b = self.__root(b) if a != b: self.__parent[b] = a def __root(self, a: int) -> int: """aの根を求めます :param a: 木の要素 :return: 根 """ if self.__parent[a] == -1: return a else: return self.__root(self.__parent[a]) def same(self, a: int, b: int) -> bool: """a と b が同じ木に属するかを判断します :param a: 木 :param b: 木 :return: a と b が同じ木なら True。そうでないなら False。 """ return self.__root(a) == self.__root(b) if __name__ == "__main__": N, D = [int(s) for s in input().split()] A = [int(s) for s in input().split()] ans = connecting_cities(N, D, A) print(ans)
Statement There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}. Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}. For Takahashi, find the minimum possible total cost to achieve the objective.
[{"input": "3 1\n 1 100 1", "output": "106\n \n\nThis cost can be achieved by, for example, building roads connecting City 1, 2\nand City 1, 3.\n\n* * *"}, {"input": "3 1000\n 1 100 1", "output": "2202\n \n\n* * *"}, {"input": "6 14\n 25 171 7 1 17 162", "output": "497\n \n\n* * *"}, {"input": "12 5\n 43 94 27 3 69 99 56 25 8 15 46 8", "output": "658"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s056053300
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
l, a, b, m = [int(i) for i in input().split()] cd = [] for i in range(18): cd.append(max(0, (10 ** (i + 1) - 1 - a) // b + 1)) if cd[-1] >= l: cd[-1] = l break cd_sum = 0 cd_2 = [] for i in cd: cd_2.append(i - cd_sum) cd_sum += i - cd_sum X = [0, a, 1] B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] for i in range(len(cd_2)): A = [[(10 ** (i + 1)), 0, 0], [1, 1, 0], [0, b, 1]] B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] n = cd_2[i] while n > 0: if n % 2 == 1: B = [ [ (B[0][0] * A[0][0] + B[0][1] * A[1][0] + B[0][2] * A[2][0]) % m, (B[0][0] * A[0][1] + B[0][1] * A[1][1] + B[0][2] * A[2][1]) % m, (B[0][0] * A[0][2] + B[0][1] * A[1][2] + B[0][2] * A[2][2]) % m, ], [ (B[1][0] * A[0][0] + B[1][1] * A[1][0] + B[1][2] * A[2][0]) % m, (B[1][0] * A[0][1] + B[1][1] * A[1][1] + B[1][2] * A[2][1]) % m, (B[1][0] * A[0][2] + B[1][1] * A[1][2] + B[1][2] * A[2][2]) % m, ], [ (B[2][0] * A[0][0] + B[2][1] * A[1][0] + B[2][2] * A[2][0]) % m, (B[2][0] * A[0][1] + B[2][1] * A[1][1] + B[2][2] * A[2][1]) % m, (B[2][0] * A[0][2] + B[2][1] * A[1][2] + B[2][2] * A[2][2]) % m, ], ] n -= 1 else: A = [ [ (A[0][0] * A[0][0] + A[0][1] * A[1][0] + A[0][2] * A[2][0]) % m, (A[0][0] * A[0][1] + A[0][1] * A[1][1] + A[0][2] * A[2][1]) % m, (A[0][0] * A[0][2] + A[0][1] * A[1][2] + A[0][2] * A[2][2]) % m, ], [ (A[1][0] * A[0][0] + A[1][1] * A[1][0] + A[1][2] * A[2][0]) % m, (A[1][0] * A[0][1] + A[1][1] * A[1][1] + A[1][2] * A[2][1]) % m, (A[1][0] * A[0][2] + A[1][1] * A[1][2] + A[1][2] * A[2][2]) % m, ], [ (A[2][0] * A[0][0] + A[2][1] * A[1][0] + A[2][2] * A[2][0]) % m, (A[2][0] * A[0][1] + A[2][1] * A[1][1] + A[2][2] * A[2][1]) % m, (A[2][0] * A[0][2] + A[2][1] * A[1][2] + A[2][2] * A[2][2]) % m, ], ] n //= 2 X[0] = (X[0] * B[0][0] + X[1] * B[1][0] + X[2] * B[2][0]) % m X[1] = (X[0] * B[0][1] + X[1] * B[1][1] + X[2] * B[2][1]) % m X[2] = (X[0] * B[0][2] + X[1] * B[1][2] + X[2] * B[2][2]) % m print(X[0] % m)
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s672418059
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
from operator import mul def getMatrixProduct(Ass, Bss, MOD): BssTr = [list(Bs) for Bs in zip(*Bss)] # 転置 ansss = [[sum(map(mul, As, Bs)) % MOD for Bs in BssTr] for As in Ass] return ansss def getMatrixPower(Ass, n, MOD): sizeA = len(Ass) ansss = [[0] * (sizeA) for _ in range(sizeA)] for i in range(sizeA): ansss[i][i] = 1 while n: if n & 1: ansss = getMatrixProduct(ansss, Ass, MOD) Ass = getMatrixProduct(Ass, Ass, MOD) n //= 2 return ansss L, A, B, M = map(int, input().split()) As = [[A % M], [A % M], [1]] iPrev = 0 d = 1 while True: i = (10**d - 1 - A) // B if i > L - 1: i = L - 1 num = i - iPrev if num > 0: Css = [[10**d, 1, B % M], [0, 1, B % M], [0, 0, 1]] PowCss = getMatrixPower(Css, num, M) As = getMatrixProduct(PowCss, As, M) if i == L - 1: break iPrev = max(0, i) d += 1 print(As[0][0])
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s627559319
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
def unitmat(n): return [[int(i == j) for i in range(n)] for j in range(n)] def matadd(A, B): h = len(A) w = len(A[0]) C = [[A[i][j] + B[i][j] for j in range(w)] for i in range(h)] return C def matmul(A, B): h = len(A) w = len(B[0]) l = len(B) C = [[0] * w for _ in range(h)] for i in range(h): for j in range(w): for k in range(l): C[i][j] += A[i][k] * B[k][j] for i in range(h): for j in range(w): C[i][j] %= mod return C def matpow(A, k): n = len(A) B = unitmat(n) while k: if k & 1: B = matmul(B, A) A = matmul(A, A) k >>= 1 return B l, a, b, mod = map(int, input().split()) X = [[0], [a], [1]] maxdigit = 18 Idx = [0] * (maxdigit + 1) for i in range(maxdigit + 1): if 10**i - 1 < a: continue Idx[i] = min((10**i - 1 - a) // b + 1, l) for i in range(1, maxdigit + 1): f = [[10**i, 1, 0], [0, 1, b], [0, 0, 1]] X = matmul(matpow(f, Idx[i] - Idx[i - 1]), X) print(X[0][0])
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s317920847
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
#!/usr/bin/env python3 MOD = None def matunit(n): return [[int(y == x) for x in range(n)] for y in range(n)] def matadd(a, b): assert len(a) == len(b) assert len(a[0]) == len(b[0]) h = len(a) w = len(a[0]) c = [[a[y][x] + b[y][x] for x in range(w)] for y in range(h)] return c def matmul(a, b): assert len(a[0]) == len(b) h = len(a) k = len(b) w = len(b[0]) c = [[0 for _ in range(w)] for _ in range(h)] for y in range(h): for z in range(k): for x in range(w): c[y][x] += a[y][z] * b[z][x] for y in range(h): for x in range(w): c[y][x] %= MOD return c def matpow(a, k): assert len(a) == len(a[0]) n = len(a) b = matunit(n) while k: if k & 1: b = matmul(b, a) a = matmul(a, a) k >>= 1 return b def binsearch(l, r, pred): # [l, r) assert l < r l -= 1 while r - l > 1: m = (l + r) // 2 if pred(m): r = m else: l = m return r def main(): l, a, b, m = map(int, input().split()) global MOD MOD = m x = [[0], [a], [1]] # (answer, s_i, 1) DIGITS = 20 d = [binsearch(0, l, lambda i: len(str(a + b * i)) >= d) for d in range(DIGITS)] for i in range(DIGITS - 1): l = d[i] r = d[i + 1] f = [[10**i, 1, 0], [0, 1, b], [0, 0, 1]] x = matmul(matpow(f, r - l), x) print(x[0][0] % m) if __name__ == "__main__": main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s185166102
Runtime Error
p03016
Input is given from Standard Input in the following format: L A B M
n, a, b, mod = map(int, input().split()) sum = 0 initial = len(str(a)) last = len(str(a + b * (n - 1))) dcnt = dict() for i in range(initial, last + 1): l = 0 r = n th = pow(10, i) while r - l > 1: k = (l + r) // 2 s = a + b * k if s < th: l = k else: r = k dcnt[i] = l + 1 - sum sum = l + 1 a = (a + b * (n - 1) % mod) % mod b = (mod - (b % mod)) % mod ret = 0 p = 0 cc = 0 for d in range(last, initial - 1, -1): cnt = dcnt[d] if d == last: cnt = cnt + 1 else: if d == initial: cnt = cnt - 1 w = pow(10, p, mod) r = pow(10, d, mod) m = mod * (r - 1) t = 0 if r == 0 else cnt if r == 1 else (pow(r, cnt, m) + m - 1) % m // (r - 1) q = w * t % mod t = ( 0 if r == 1 else (pow(r, cnt, m * (r - 1)) + m * (r - 1) - 1) % (m * (r - 1)) // (r - 1) ) u = ((cnt - 1) % m * pow(r, cnt, m) % m + 1 + m - t) % m v = 0 if r == 0 else cnt * (cnt - 1) // 2 if r == 1 else u % m // (r - 1) ret = (ret + (a + b * cc % mod) % mod * q % mod + v * w % mod * b % mod) % mod p = p + d * (cnt - 1) + d - 1 cc = cc + cnt print(ret)
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s602682300
Runtime Error
p03016
Input is given from Standard Input in the following format: L A B M
def d_lamp(H, W, Grid): num_left = [[0] * W for _ in range(H)] num_right = [[0] * W for _ in range(H)] num_down = [[0] * W for _ in range(H)] num_up = [[0] * W for _ in range(H)] for row in range(H): for col in range(W): if Grid[row][col] == ".": num_left[row][col] = 1 if col == 0 else (num_left[row][col - 1] + 1) for row in range(H): for col in range(W - 1, -1, -1): if Grid[row][col] == ".": num_right[row][col] = ( 1 if col == W - 1 else (num_right[row][col + 1] + 1) ) for col in range(W): for row in range(H): if Grid[row][col] == ".": num_up[row][col] = 1 if row == 0 else (num_up[row - 1][col] + 1) for col in range(W): for row in range(H - 1, -1, -1): if Grid[row][col] == ".": num_down[row][col] = 1 if row == H - 1 else (num_down[row + 1][col] + 1) ans = max( num_left[row][col] + num_right[row][col] + num_up[row][col] + num_down[row][col] - 3 for row in range(H) for col in range(W) ) return ans H, W = [int(i) for i in input().split()] Grid = [input() for _ in range(H)] print(d_lamp(H, W, Grid))
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s584046570
Runtime Error
p03016
Input is given from Standard Input in the following format: L A B M
from multiprocessing import Pool def calc_rem(arg): A, B, x = arg return A + B * x def main(): L, A, B, M = map(int, input().split(" ")) rems = [] with Pool(processes=5) as p: rems = p.map(calc_rem, zip([A] * L, [B] * L, range(L))) rem = 0 for i in range(L): si = rems[i] ndigs = len(str(si)) remsi = si % M rem = ((rem * (10**ndigs)) + remsi) % M print(rem) if __name__ == "__main__": main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s729477027
Wrong Answer
p03016
Input is given from Standard Input in the following format: L A B M
import sys from math import ceil from functools import lru_cache def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(10**6) @lru_cache(maxsize=1024) def power(n, mod): # 10 ** n % mod return pow(10, n, mod) @lru_cache(maxsize=1024) def f(L, d, mod): if L == 0: val = 0 elif L == 1: val = 1 elif L % 2 == 0: val = (f(L // 2, d, mod) * power(L * d // 2, mod) + f(L // 2, d, mod)) % mod elif L % 2 != 0: val = (f(L - 1, d, mod) * power(d, mod) + 1) % mod # print("f({}, {})={}".format(L, d, val)) return val @lru_cache(maxsize=1024) def g(L, d, B, mod): if L == 0: val = 0 elif L == 1: val = 0 elif L % 2 == 0: val = ( g(L // 2, d, B, mod) * power(L * d // 2, mod) + g(L // 2, d, B, mod) + (B * L // 2) * f(L // 2, d, mod) ) % mod elif L % 2 != 0: val = (g(L - 1, d, B, mod) * power(d, mod) + B * (L - 1)) % mod # print("g({}, {})={}".format(L, d, val)) return val def main(): L, A, B, mod = map(int, input().split()) """ 数列の桁が変わっていくのが面倒だが、桁数dの数列の要素の個数は (10^(d+1)未満の要素の個数) - (10^d未満の要素の個数) で簡単に求まるので、とりあえず桁数は同じで考えて良い このとき求めるのは \Sum_{i=0}^{L-1} (A+Bi) * 10^{d(L-i-1)} これは分解すれば f(L) = \Sum_{i=0}^{L-1} 10^{di} g(L) = \Sum_{i=0}^{L-1} Bi * 10^{d(L-i-1)} の線形和(= A * f(L) + g(L))として求まる。 前者については f(L + 1) = f(L) * 10^d + 1 f(L * 2) = f(L) * 10^{Ld} + f(L) が成り立つので二分法によりO(log^2 L)で求まる(f(18)->f(9)->f(8)->f(4)->f(2)->f(1)みたいに) 後者については g(L + 1) = g(L) * 10^d g(L * 2) = g(L) * 10^{Ld} + g(L) + (BL * f(L)) なのでやはり同様に二分法が可能でO(log^3 L)で計算可能。 """ # まずは桁数で区切る initial = [0] * 19 # initial[d] = (数列の項でd桁にはじめてなる項の値) number = [0] * 19 # number[d] = (数列の項でd桁である物が何個存在するか) for d in range(len(str(A)), 19): i = max(0, ceil((10 ** (d - 1) - A) / B)) initial[d] = A + B * i if (10**d - A) % B == 0: number[d] = (10**d - A) // B - i else: number[d] = (10**d - A) // B - i + 1 if number[d] == 0: initial[d] = 0 max_len = len(str(A + B * (L - 1))) exceeded_num = sum(number[: (max_len + 1)]) - L number[max_len] -= exceeded_num initial[(max_len + 1) :] = [0] * (18 - max_len) number[(max_len + 1) :] = [0] * (18 - max_len) # print("initial={}".format(initial)) # print("number={}".format(number)) # 各dについて上述の値を計算 ans = 0 digits = 0 for d in range(18, 0, -1): a = initial[d] l = number[d] val = (a * f(l, d, mod) + g(l, d, B, mod)) * power(digits, mod) val %= mod ans += val ans %= mod digits += d * l # print("d={}->val={}".format(d, val)) print(ans) if __name__ == "__main__": main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s101831956
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
L, A, B, mod = map(int, input().split()) def f(l): return max(0, (10**l - A - 1) // B + 1) def m(a, b): r = [[0] * len(b[0]) for i in range(len(a))] for i in range(len(a)): for k in range(len(b)): for j in range(len(b[0])): r[i][j] = (r[i][j] + a[i][k] * b[k][j]) % mod return r def p(a, n): r = [[0] * len(a) for i in range(len(a))] b = [] for i in range(len(a)): r[i][i] = 1 b.append(a[i][:]) l = n while l > 0: if l & 1: r = m(b, r) b = m(b, b) l >>= 1 return r X = [[0, 0, 1], [0, 0, A], [0, 0, 0]] Y, R, Z = 0, 1, 0 while L: Y = [[1, 0, 0], [B, 1, 0], [0, 1, pow(10, R, mod)]] Z = min(L, f(R) - f(R - 1)) L -= Z Y = p(Y, Z) X = m(Y, X) R += 1 print(X[2][2] % mod)
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s339672963
Runtime Error
p03016
Input is given from Standard Input in the following format: L A B M
l, a, b, m = input().split() l = int(l) a = int(a) b = int(b) m = int(m) list = [0] * l for i in range(l): list[i] = a + b * i list = [str(n) for n in list] mojiretu = " " for x in list: mojiretu += x mojiretu = int(mojiretu) print(mojiretu % m)
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s010604692
Runtime Error
p03016
Input is given from Standard Input in the following format: L A B M
L, A, B, M = 1, 1, 1, 1 def a(idx): return A + B * idx hosei = 1 last = 1 def f(keta): R = 10 ** (keta - 1) t = 0 global last if a(last) >= R: cant = -1 can = L - 1 while can - cant > 1: m = (can + cant) // 2 if a(m) >= R: can = m else: cant = m first = can n = last - first + 1 aa = a(last) % M r = R * 10 % M t1 = (r**n - r) // (r - 1) * B t2 = (aa - (n - 1) * B) * r**n t = aa - t1 - t2 t //= 1 - r global hosei t %= M t *= hosei t %= M hosei = hosei * 10 ** (keta * n) hosei %= M last = first - 1 return t def main(): global L global A global B global M L, A, B, M = map(int, input().split()) global last last = L - 1 ans = 0 ans += f(18) ans += f(17) ans += f(16) ans += f(15) ans += f(14) ans += f(13) ans += f(12) ans += f(11) ans += f(10) ans += f(9) ans += f(8) ans += f(7) ans += f(6) ans += f(5) ans += f(4) ans += f(3) ans += f(2) ans += f(1) ans %= M print(ans) main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s204784198
Wrong Answer
p03016
Input is given from Standard Input in the following format: L A B M
L, A, B, M = map(int, input().split()) f = [0 for i in range(20)] def doubling(n, m, mod): y = 1 base = n while m != 0: if m % 2 == 1: y *= base y %= mod base *= base base %= mod m //= 2 return y def factorization(n): if n == 1: return {1: 1} else: p = 2 tmp = n D = {} while n != 1: i = 0 while n % p == 0: i += 1 n //= p if i != 0: D[p] = i p += 1 if p * p > tmp and n != 1: D[n] = 1 break return D def Torshent(n): S = n dic = factorization(n) for i in dic: S //= i S *= i - 1 return S d = 1 for i in range(20): l, r = max(-1, (d - A - 1) // B), max(-1, (10 * d - A - 1) // B) if i == 0: f[i] = min(r, L - 1) - min(l, L - 1) else: f[i] = f[i - 1] + min(r, L - 1) - min(l, L - 1) d *= 10 N = 0 l, r = 0, 0 for i in range(20): r = f[i] N = N * doubling(10, (i + 1) * (r - l), M) prod = (A + B * l) * (doubling(10, (i + 1) * (r - l), M) - 1) prod *= doubling(doubling(10, (i + 1), M) - 1, Torshent(M) - 1, M) prod %= M N += prod N %= M d1 = doubling(10, (i + 1) * (r - l), M) d2 = doubling(10, (i + 1), M) prod = B * ((d1 - 1) - (r - l) * (d2 - 1)) prod *= doubling(d2 - 1, Torshent(M) - 1, M) prod *= doubling(d2 - 1, Torshent(M) - 1, M) prod %= M N += prod N %= M l = f[i] print(N)
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s778141515
Wrong Answer
p03016
Input is given from Standard Input in the following format: L A B M
import sys from io import StringIO import unittest import math class TestClass(unittest.TestCase): def assertIO(self, input, output): stdout, stdin = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(input) resolve() sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, stdin self.assertEqual(out, output) def test_入力例_1(self): input = """5 3 4 10007""" output = """5563""" self.assertIO(input, output) def test_入力例_2(self): input = """4 8 1 1000000""" output = """891011""" self.assertIO(input, output) def test_入力例_3(self): input = """107 10000000000007 1000000000000007 998244353""" output = """39122908""" self.assertIO(input, output) def resolve(): (L, A, B, M) = map(int, input().split()) st = (L - 1) * B + A beforemod = 1 modlist = [0, 1] mod = 0 for i in range(19): modlist.append(modlist[-1] * 10 % M) for i in range(L): mod = ((beforemod * (st % M)) % M + mod) % M beforemod = (modlist[math.floor(math.log10(st) + 1) + 1] * beforemod) % M st -= B print(mod) if __name__ == "__main__": unittest.main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the remainder when the integer obtained by concatenating the terms is divided by M. * * *
s924493426
Accepted
p03016
Input is given from Standard Input in the following format: L A B M
def main(): import sys input = sys.stdin.readline N, a0, d, mod = map(int, input().split()) def matmul(A, B): C = [[0] * len(B[0]) for _ in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(B)): C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod return C def matpow(A, p): n = len(A) B = [[0] * n for _ in range(n)] for i in range(n): B[i][i] = 1 while p > 0: if p & 1: B = matmul(B, A) A = matmul(A, A) p >>= 1 return B ans = [[a0 % mod], [a0 % mod], [d % mod]] ok_prev = 0 for i in range(len(str(a0)), 20): ok = N ng = -1 mid = (ok + ng) // 2 while ok - ng > 1: if len(str(a0 + d * mid)) >= i + 1: ok = mid else: ng = mid mid = (ok + ng) // 2 mat = [[(10**i) % mod, 1, 1], [0, 1, 1], [0, 0, 1]] ans = matmul(matpow(mat, ok - ok_prev - 1), ans) ok_prev = ok - 1 if ok == N: break print(ans[0][0]) if __name__ == "__main__": main()
Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?
[{"input": "5 3 4 10007", "output": "5563\n \n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod\n10007, that is, 5563.\n\n* * *"}, {"input": "4 8 1 1000000", "output": "891011\n \n\n* * *"}, {"input": "107 10000000000007 1000000000000007 998244353", "output": "39122908"}]
Print the number of prime numbers in the given list.
s679025067
Runtime Error
p02257
The first line contains an integer _N_ , the number of elements in the list. _N_ numbers are given in the following lines.
n = int(input()) a = [] for i in range(1, n - 1): a[i] = int(input())
Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of _N_ integers and prints the number of prime numbers in the list.
[{"input": "5\n 2\n 3\n 4\n 5\n 6", "output": "3"}, {"input": "11\n 7\n 8\n 9\n 10\n 11\n 12\n 13\n 14\n 15\n 16\n 17", "output": "4"}]
Print the number of prime numbers in the given list.
s709355078
Wrong Answer
p02257
The first line contains an integer _N_ , the number of elements in the list. _N_ numbers are given in the following lines.
def p(x): if x%2==0:return 1 i=3 while i<=x**.5: if x%i==0:return 0 i+=2 return 1 n=int(input()) print(sum([p(int(input()))for _ in range(n)]))
Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of _N_ integers and prints the number of prime numbers in the list.
[{"input": "5\n 2\n 3\n 4\n 5\n 6", "output": "3"}, {"input": "11\n 7\n 8\n 9\n 10\n 11\n 12\n 13\n 14\n 15\n 16\n 17", "output": "4"}]
Print the number of prime numbers in the given list.
s811110642
Accepted
p02257
The first line contains an integer _N_ , the number of elements in the list. _N_ numbers are given in the following lines.
print( sum( map( lambda x: 1 if x == 2 or pow(2, x - 1, x) == 1 else 0, [int(input()) for _ in range(int(input()))], ) ) )
Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of _N_ integers and prints the number of prime numbers in the list.
[{"input": "5\n 2\n 3\n 4\n 5\n 6", "output": "3"}, {"input": "11\n 7\n 8\n 9\n 10\n 11\n 12\n 13\n 14\n 15\n 16\n 17", "output": "4"}]
Print the total weight of the edges contained in a minimum spanning tree of the graph. * * *
s601335830
Wrong Answer
p03915
The input is given from Standard Input in the following format: N Q A_1 B_1 C_1 A_2 B_2 C_2 : A_Q B_Q C_Q
import sys input = sys.stdin.readline import heapq n, q = [int(v) for v in input().split()] parent_list = [i for i in range(n)] def root(x): compress_node = [] while parent_list[x] != x: compress_node.append(x) x = parent_list[x] for i in compress_node: parent_list[i] = x return x query_list = [] for i in range(q): a, b, c = [int(v) for v in input().split()] query_list.append([c, a, b]) query_list.sort() connect = 0 ans = 0 # while connect != n-1: for i in range(20): c, a, b = heapq.heappop(query_list) ra, rb = root(a), root(b) if ra != rb: parent_list[ra] = b connect += 1 ans += c heapq.heappush(query_list, [c + 1, b % n, (a + 1) % n]) print(ans)
Statement We have a graph with N vertices, numbered 0 through N-1. Edges are yet to be added. We will process Q queries to add edges. In the i-th (1≦i≦Q) query, three integers A_i, B_i and C_i will be given, and we will add infinitely many edges to the graph as follows: * The two vertices numbered A_i and B_i will be connected by an edge with a weight of C_i. * The two vertices numbered B_i and A_i+1 will be connected by an edge with a weight of C_i+1. * The two vertices numbered A_i+1 and B_i+1 will be connected by an edge with a weight of C_i+2. * The two vertices numbered B_i+1 and A_i+2 will be connected by an edge with a weight of C_i+3. * The two vertices numbered A_i+2 and B_i+2 will be connected by an edge with a weight of C_i+4. * The two vertices numbered B_i+2 and A_i+3 will be connected by an edge with a weight of C_i+5. * The two vertices numbered A_i+3 and B_i+3 will be connected by an edge with a weight of C_i+6. * ... Here, consider the indices of the vertices modulo N. For example, the vertice numbered N is the one numbered 0, and the vertice numbered 2N-1 is the one numbered N-1. The figure below shows the first seven edges added when N=16, A_i=7, B_i=14, C_i=1: ![](https://atcoder.jp/img/code- festival-2016-final/5b0258fb4255f846a4e10ce875362baf.png) After processing all the queries, find the total weight of the edges contained in a minimum spanning tree of the graph.
[{"input": "7 1\n 5 2 1", "output": "21\n \n\nThe figure below shows the minimum spanning tree of the graph:\n\n![](https://atcoder.jp/img/code-\nfestival-2016-final/f1a6c3cfd52c386e6da5c8c761a78521.png)\n\nNote that there can be multiple edges connecting the same pair of vertices.\n\n* * *"}, {"input": "2 1\n 0 0 1000000000", "output": "1000000001\n \n\nAlso note that there can be self-loops.\n\n* * *"}, {"input": "5 3\n 0 1 10\n 0 2 10\n 0 4 10", "output": "42"}]
Print the number of squares that satisfy both of the conditions. * * *
s311694535
Accepted
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
_, *a = map(int, open(0).read().split()) print(sum(b % 2 for b in a[::2]))
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the number of squares that satisfy both of the conditions. * * *
s436368949
Runtime Error
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
# 解答 def abc173_a(lines): N = int(lines[0]) values = map(int, lines[1].split(" ")) answer = 0 tmp = list() for value in values: tmp.append(value) print(f"tmp=[{tmp}]") print(f"tmp[0]=[{tmp[0]}]") for i in range(N): if i % 2 != 0: pass else: if tmp[i] % 2 != 0: answer += 1 return [answer] # 引数を取得 def get_input_lines(lines_count): lines = list() for _ in range(lines_count): lines.append(input()) return lines # テストデータ def get_testdata(pattern): if pattern == 1: lines_input = ["5", "1 3 4 5 7"] lines_export = [2] elif pattern == 2: lines_input = ["15", "13 76 46 15 50 98 93 77 31 43 84 90 6 24 14"] lines_export = [3] return lines_input, lines_export # 動作モード判別 def get_mode(): import sys args = sys.argv if len(args) == 1: mode = 0 else: mode = int(args[1]) return mode # 主処理 def main(): mode = get_mode() if mode == 0: lines_input = get_input_lines(1) else: lines_input, lines_export = get_testdata(mode) lines_result = abc173_a(lines_input) for line_result in lines_result: print(line_result) if mode > 0: print(f"lines_input=[{lines_input}]") print(f"lines_export=[{lines_export}]") print(f"lines_result=[{lines_result}]") if lines_result == lines_export: print("OK") else: print("NG") # 起動処理 if __name__ == "__main__": main()
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the number of squares that satisfy both of the conditions. * * *
s465740699
Wrong Answer
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
num_of_squares = int(input()) written_values = [i for i in input().split(" ")] flag = 0 for i in range(num_of_squares): if (i % 2 != 0) & (int(written_values[i]) % 2 != 0): flag += 1 print(flag)
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the number of squares that satisfy both of the conditions. * * *
s596280226
Wrong Answer
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
print(sum([int(i) % 2 for i in input().split()[::2]]))
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the number of squares that satisfy both of the conditions. * * *
s134231253
Accepted
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
n, *A = map(int, open(0).read().split()) print(len([a for i, a in enumerate(A, 1) if i % 2 == a % 2 == 1]))
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the number of squares that satisfy both of the conditions. * * *
s001196998
Wrong Answer
p02607
Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N
n = int(input()) N = list(map(int, input().split())) del N[::2] i = 0 ans = [i for i in N if i % 2 == 1] print(len(ans))
Statement We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd.
[{"input": "5\n 1 3 4 5 7", "output": "2\n \n\n * Two squares, Square 1 and 5, satisfy both of the conditions.\n * For Square 2 and 4, the assigned numbers are not odd.\n * For Square 3, the written integer is not odd.\n\n* * *"}, {"input": "15\n 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14", "output": "3"}]
Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print `-1` instead. * * *
s000039506
Runtime Error
p02579
Input is given from Standard Input in the following format: H W C_h C_w D_h D_w S_{11}\ldots S_{1W} \vdots S_{H1}\ldots S_{HW}
import sys sys.setrecursionlimit(10000) H,W=map(int,input().split()) Ch,Cw=map(int,input().split()) Dh,Dw=map(int,input().split()) maze=[] for i in range(H): l=list(input()) for j in range(len(l)): if l[j]=='.':l[j]=-1 if l[j]=='#':l[j]=-2 maze.append(l) def isIn(h,w): if h<0 or h>=H or w<0 or w >=W:return False return True def update(maze,h,w,value,l): global update_list vectors=[[-1,0],[1,0],[0,1],[0,-1]] maze[h][w]=value update_list=l u=[] for i in range(25): if not isIn(h+i//5-2,w+i%5-2):continue if maze[h+i//5-2][w+i%5-2]>maze[h][w] or maze[h+i//5-2][w+i%5-2]==-1: if [h+i//5-2,w+i%5-2] not in u: u.append([h+i//5-2,w+i%5-2]) maze[h+i//5-2][w+i%5-2]=value+1 for vector in vectors: if not isIn(h+vector[0],w+vector[1]):continue if maze[h+vector[0]][w+vector[1]]>maze[h][w] or maze[h+vector[0]][w+vector[1]]==-1: maze[h+vector[0]][w+vector[1]]=maze[h][w] update(maze,h+vector[0],w+vector[1],value,l) for h,w in u: if maze[h][w]==value+1 and not [h,w] in update_list: update_list.append([h,w]) return update_list already=0 def calc(update_list,value): for h,w in update_list: if maze[h][w]!=-1 and maze[h][w]<value:continue u=update(maze,h,w,value,[]) calc(u,value+1) update_list=update(maze,Ch-1,Cw-1,0,[]) calc(update_list,1) print(maze[Dh-1][Dw-1])
Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal. The square at the i-th row from the top and the j-th column from the left - (i,j) \- is a wall if S_{ij} is `#` and a road if S_{ij} is `.`. There is a magician in (C_h,C_w). He can do the following two kinds of moves: * Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in. * Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in. In either case, he cannot go out of the maze. At least how many times does he need to use the magic to reach (D_h, D_w)?
[{"input": "4 4\n 1 1\n 4 4\n ..#.\n ..#.\n .#..\n .#..", "output": "1\n \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n 1 4\n 4 1\n .##.\n ####\n ####\n .##.", "output": "-1\n \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n 2 2\n 3 3\n ....\n ....\n ....\n ....", "output": "0\n \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n 1 2\n 2 5\n #.###\n ####.\n #..##\n #..##", "output": "2"}]
Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print `-1` instead. * * *
s587963866
Wrong Answer
p02579
Input is given from Standard Input in the following format: H W C_h C_w D_h D_w S_{11}\ldots S_{1W} \vdots S_{H1}\ldots S_{HW}
from collections import deque class G: def __init__(self, c): self.c = c self.reached = False self.range = 0 H, W = [int(i) for i in input().split()] Ch, Cw = [int(i)-1 for i in input().split()] Dh, Dw = [int(i)-1 for i in input().split()] S = [['.' for j in range(W)] for i in range(H) ] for i in range(H): tmp = list(input()) for j in range(W): S[i][j] = G(tmp[j]) def neighbors(y, x): s = [] if x != 0: if S[y][x-1].reached == False and S[y][x-1].c == '.': s.append((y, x-1)) if x != W-1: if S[y][x+1].reached == False and S[y][x-1].c == '.': s.append((y, x+1)) if y != 0: if S[y-1][x].reached == False and S[y][x-1].c == '.': s.append((y-1, x)) if y != H-1: if S[y+1][x].reached == False and S[y][x-1].c == '.': s.append((y+1, x)) return s def jump(x, y, d): for i in range(max(x-2, 0), min(x+3, W)): for j in range(max(y-2, 0), min(y+3, H)): if S[j][i].reached == False and S[j][i].c == '.': d.append((j, i)) def DFS(S, d): while True: if len(d) == 0: break q = d.popleft() S[q[0]][q[1]].reached = True for each in neighbors(q[0], q[1]): S[each[0]][each[1]].reached = True if S[each[0]][each[1]].c == '.': d.append(each) counter = 0 d = deque([]) S[Ch][Cw].reached = True d.append((Ch, Cw)) flag = False while True: DFS(S, d) if S[Dh][Dw].reached: break counter += 1 for i in range(H): for j in range(W): if S[i][j].reached and S[i][j].c == '.': jump(j, i, d) if len(d) == 0: flag = True break if flag: print(-1) else: print(counter)
Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal. The square at the i-th row from the top and the j-th column from the left - (i,j) \- is a wall if S_{ij} is `#` and a road if S_{ij} is `.`. There is a magician in (C_h,C_w). He can do the following two kinds of moves: * Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in. * Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in. In either case, he cannot go out of the maze. At least how many times does he need to use the magic to reach (D_h, D_w)?
[{"input": "4 4\n 1 1\n 4 4\n ..#.\n ..#.\n .#..\n .#..", "output": "1\n \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n 1 4\n 4 1\n .##.\n ####\n ####\n .##.", "output": "-1\n \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n 2 2\n 3 3\n ....\n ....\n ....\n ....", "output": "0\n \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n 1 2\n 2 5\n #.###\n ####.\n #..##\n #..##", "output": "2"}]
Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print `-1` instead. * * *
s033664437
Wrong Answer
p02579
Input is given from Standard Input in the following format: H W C_h C_w D_h D_w S_{11}\ldots S_{1W} \vdots S_{H1}\ldots S_{HW}
def get_position(node): h = node // W + 1 w = node % W if w == 0: w = W return (h, w) H, W = list(map(int, input().split())) C_h, C_w = list(map(int, input().split())) D_h, D_w = list(map(int, input().split())) N = H * W # 壁のノード wall = [] for h in range(H): for w_idx, w in enumerate(list(input())): if w == "#": wall.append(W * h + w_idx + 1) # 道のノード path = [_ for _ in range(1, N + 1) if _ not in wall] # 隣接リスト ad = {} for n in range(N): ad[n + 1] = [] for n in range(N): n = n + 1 if n not in wall: up = n - W if up > 0 and up not in wall: ad[n].append(up) down = n + W if down <= N and down not in wall: ad[n].append(down) left = n - 1 if n % W != 1 and left not in wall and left > 0: ad[n].append(left) right = n + 1 if n % W != 0 and right not in wall: ad[n].append(right) from collections import deque color = {} for n in range(N): color[n + 1] = -1 depth = {} for n in range(N): depth[n + 1] = -1 group = {} # for n in range(N): # group[n+1] = -1 group_ = 0 for p in path: if color[p] != -1: continue elif color[p] == -1: start = p color[start] = 0 que = deque([start]) visit = deque([]) group_ += 1 group[group_] = [start] depth[start] = 0 while len(que) > 0: start = que[0] # print(start,que) for v in ad[start]: # print(v,color[v]) if color[v] == -1: que.append(v) color[v] = 0 depth[v] = depth[start] + 1 group[group_].append(v) color[start] = 1 visit.append(que.popleft()) group_conect = {} for g in group.keys(): group_conect[g] = set([]) break_flag = False for g_1 in range(1, group_ + 1): for g_2 in range(g_1 + 1, group_ + 1): for g1 in group[g_1]: for g2 in group[g_2]: y, x = get_position(g1) y_min = max(0, y - 2) y_max = min(W, y + 2) x_min = max(0, x - 2) x_max = min(H, x + 2) Y, X = get_position(g2) if y_min <= Y and Y <= y_max and x_min <= X and X <= x_max: group_conect[g_1].add(g_2) group_conect[g_2].add(g_1) break print(group_conect)
Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal. The square at the i-th row from the top and the j-th column from the left - (i,j) \- is a wall if S_{ij} is `#` and a road if S_{ij} is `.`. There is a magician in (C_h,C_w). He can do the following two kinds of moves: * Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in. * Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in. In either case, he cannot go out of the maze. At least how many times does he need to use the magic to reach (D_h, D_w)?
[{"input": "4 4\n 1 1\n 4 4\n ..#.\n ..#.\n .#..\n .#..", "output": "1\n \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n 1 4\n 4 1\n .##.\n ####\n ####\n .##.", "output": "-1\n \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n 2 2\n 3 3\n ....\n ....\n ....\n ....", "output": "0\n \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n 1 2\n 2 5\n #.###\n ####.\n #..##\n #..##", "output": "2"}]
Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print `-1` instead. * * *
s121037142
Wrong Answer
p02579
Input is given from Standard Input in the following format: H W C_h C_w D_h D_w S_{11}\ldots S_{1W} \vdots S_{H1}\ldots S_{HW}
from collections import deque def resolve(): drc4 = {(-1, 0), (0, 1), (1, 0), (0, -1)} def bfs(start, goal): sy, sx = start gy, gx = goal q = deque() q.append(start) ans[sy][sx] = 0 while q: sy, sx = q.popleft() # if sy == gy and sx == gx: # return print(ans[gy][gx]) for dy, dx in drc4: ny, nx = dy + sy, dx + sx # if G[ny][nx] == "@" or ans[ny][nx] != INF: # if G[ny][nx] != "#": # continue if G[ny][nx] == "." and ans[ny][nx] > ans[sy][sx]: ans[ny][nx] = ans[sy][sx] q.append((ny, nx)) for dy8 in range(-2, 3): for dx8 in range(-2, 3): if (dy8, dx) in drc4: continue ny8, nx8 = dy8 + sy, dx8 + sx if 1 <= ny8 <= H + 1 and 1 <= nx8 <= W + 1: # if G[ny8][nx8] == "#": # continue # if G[ny8][nx8] == "@" or ans[ny8][nx8] != INF: # continue if G[ny8][nx8] == "." and ans[ny8][nx8] > ans[sy][sx] + 1: ans[ny8][nx8] = ans[sy][sx] + 1 q.append((ny8, nx8)) # return print(-1) H, W = map(int, input().split()) sy, sx = map(int, input().split()) gy, gx = map(int, input().split()) G = [] G.append(["@"] * (W + 2)) G += [["@"] + list(input()) + ["@"] for _ in range(H)] G.append(["@"] * (W + 2)) INF = 10 ** 7 ans = [[INF] * (W + 2) for _ in range(H + 2)] bfs((sy, sx), (gy, gx)) print(ans[gy][gx] if ans[gy][gx] < INF else '-1') if __name__ == "__main__": resolve()
Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal. The square at the i-th row from the top and the j-th column from the left - (i,j) \- is a wall if S_{ij} is `#` and a road if S_{ij} is `.`. There is a magician in (C_h,C_w). He can do the following two kinds of moves: * Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in. * Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in. In either case, he cannot go out of the maze. At least how many times does he need to use the magic to reach (D_h, D_w)?
[{"input": "4 4\n 1 1\n 4 4\n ..#.\n ..#.\n .#..\n .#..", "output": "1\n \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n 1 4\n 4 1\n .##.\n ####\n ####\n .##.", "output": "-1\n \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n 2 2\n 3 3\n ....\n ....\n ....\n ....", "output": "0\n \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n 1 2\n 2 5\n #.###\n ####.\n #..##\n #..##", "output": "2"}]