submission_id
string
problem_id
string
status
string
code
string
input
string
output
string
problem_description
string
s938174787
p04049
Runtime Error
import sys from collections import deque, defaultdict def bfs(links, s, limit): not_reachable = [True] * len(links) q = deque([(0, s, -1)]) while q: cost, v, p = q.popleft() if cost > limit: break not_reachable[v] = False cost += 1 q.extend((cost, u, v) for u in links[v] if u != p) return not_reachable def solve(n, k, ab): if k == 1: return n - 2 links = [set() for _ in range(n)] for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 links[a].add(b) links[b].add(a) ans = n thr = k // 2 if k % 2 == 0: ans = min(sum(bfs(links, v, thr) for v in range(n))) else: dists = [bfs(links, v, thr) for v in range(n)] for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 ans = min(ans, sum(d1 and d2 for d1, d2 in zip(dists[a], dists[b]))) return ans n, k, *ab = map(int, sys.stdin.buffer.read().split()) print(solve(n, k, ab))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s369512498
p04049
Runtime Error
#C n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for i in range(n-1): a,b = [int(j) for j in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) def dfs(u, cost): if dist[u]>=0: #if searched already return dist[u] = cost e = edges[u] for v in e: dfs(v, cost+1) ans = n #when k is even, we should think about each node if k%2==0: for u in range(n): dist = [-1]*n #if -1, it means not visited yet dfs(u,0) cnt = 0 for v in dist: if v<=(k//2): cnt += 1 ans = min(ans, n-cnt) #when k is even, we should think about each edge else: for u,e in enumerate(edges): for v in e: cnt = 0 dist = [-1]*n #if -1, it means not visited yet dist[v] = 0 dfs(u,0) for v in dist: if v<=(k//2): cnt += 1 dist = [-1]*n #if -1, it means not visited yet dist[u] = 0 dfs(v,0) for v in dist: if v<=(k//2): cnt += 1 cnt -= 2 ans = min(ans, n-cnt) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s434611661
p04049
Runtime Error
#C n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for i in range(n-1): a,b = [int(j) for j in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) ans = n #when k is even, we should think about each node if k%2==0: def dfs(u, cost): if dist[u]>=0: #if searched already return dist[u] = cost e = edges[u] for v in e: dfs(v, cost+1) for u in range(n): dist = [-1]*n #if -1, it means not visited yet dfs(u,0) cnt = 0 for v in dist: if v>(k//2): cnt += 1 ans = min(ans, cnt) print(ans) #when k is even, we should think about each edge else: def dfs(u, cost): if dist[u]>=0: #if searched already return dist[u] = cost e = edges[u] for v in e: dfs(v, cost+1) for u,e in enumerate(edges): for v in e: cnt = 0 dist = [-1]*n #if -1, it means not visited yet dist[v] = 0 dfs(u,0) for v in dist: if v>(k//2): cnt += 1 dist = [-1]*n #if -1, it means not visited yet dist[u] = 0 dfs(v,0) for v in dist: if v>(k//2): cnt += 1 ans = min(ans, cnt) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s296170347
p04049
Runtime Error
n, k = map(int, input().split()) tree = [[] for i in range(n)] for _ in range(n - 1): a, b = sorted(map(int, input().split())) tree[a - 1].append(b - 1) tree[b - 1].append(a - 1) def search(tree, pos, past=None, depth=0): """ 子孫ノードを探索して、最深のノードとその深さを返す """ # 子ノードを探索する ress = [] # 子ノードの探索結果 for next_node in tree[pos]: # next_nodeが親なら探索しない if next_node == past: continue res = search(tree, next_node, pos, depth + 1) ress.append(res) ress.sort(reverse=True) # 子ノードがいないならばこのノードとその深さを返す if len(ress) == 0: ress = [[depth, pos]] return ress[0] def countChildren(tree, pos): """ ノードposを根ノードとした場合の 深さ毎のノードを数える """ def dfs(tree, pos, child_list=[], past=None, depth=0): child_list += [0] * (depth + 1 - len(child_list)) child_list[depth] += 1 # 子ノードを探索する for next_node in tree[pos]: # next_nodeが親なら探索しない if next_node == past: continue dfs(tree, next_node, child_list, pos, depth + 1) child_list = [] dfs(tree, pos, child_list) return child_list[:] # 最遠頂点対 u,vと直径diameterを求める _, u = search(tree, 0) diameter, v = search(tree, u) # u,vを頂点ノードとしたときの深さ毎のノードを数える u_list = countChildren(tree, u) v_list = countChildren(tree, v) # 縮める木の直径の長さ clip_len = max(diameter - k, 0) u_ans = sum(u_list[-clip_len:]) v_ans = sum(v_list[-clip_len:]) print(min(u_ans, v_ans))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s989027159
p04049
Runtime Error
n, k = map(int, input().split()) tree = [[] for i in range(n)] for _ in range(n - 1): a, b = sorted(map(int, input().split())) tree[a - 1].append(b - 1) tree[b - 1].append(a - 1) def search(tree, pos, past=None, depth=0): """ 子孫ノードを探索して、最深のノードとその深さを返す """ # 子ノードを探索する ress = [] # 子ノードの探索結果 for next_node in tree[pos]: # next_nodeが親なら探索しない if next_node == past: continue res = search(tree, next_node, pos, depth + 1) ress.append(res) ress.sort(reverse=True) # 子ノードがいないならばこのノードとその深さを返す if len(ress) == 0: ress = [[depth, pos]] return ress[0] def countChildren(tree, pos): """ ノードposを根ノードとした場合の 深さ毎のノードを数える """ def dfs(tree, pos, child_list=[], past=None, depth=0): child_list += [0] * (depth + 1 - len(child_list)) child_list[depth] += 1 # 子ノードを探索する for next_node in tree[pos]: # next_nodeが親なら探索しない if next_node == past: continue dfs(tree, next_node, child_list, pos, depth + 1) child_list = [] dfs(tree, pos, child_list) return child_list[:] # 最遠頂点対 u,vと直径diameterを求める _, u = search(tree, 0) diameter, v = search(tree, u) # u,vを頂点ノードとしたときの深さ毎のノードを数える u_list = countChildren(tree, u) v_list = countChildren(tree, v) removed_nodes = 0 while diameter > k: if u_list[-1] < v_list[-1]: removed_nodes += u_list[-1] u_list.pop() else: removed_nodes += v_list[-1] v_list.pop() diameter -= 1 print(removed_nodes)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s406256226
p04049
Runtime Error
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n,k = map(int, input().split()) G = [[] for i in range(n)] edges = [] for i in range(n-1): a,b = map(int, input().split()) a,b = a-1,b-1 G[a].append(b) G[b].append(a) edges.append((a,b)) cnt = [0] def search(cur, prev, depth, k): if depth > k: cnt[0] += 1 for to in G[cur]: if to != prev: search(to, cur, depth+1, k) ans = float("inf") # 点中心 if k % 2 == 0: for i in range(n): cnt = [0] search(i, -1, 0, k//2) ans = min(ans, cnt[0]) # 辺中心 else: for a, b in edges: cnt = [0] search(a, b, 0, k//2) search(b, a, 0, k//2) ans = min(ans, cnt[0]) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s510777512
p04049
Runtime Error
#include <stdio.h> #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #include <chrono> #include <random> #include <time.h> #include <fstream> #define ll long long #define rep2(i,a,b) for(ll i=a;i<=b;++i) #define rep(i,n) for(ll i=0;i<n;i++) #define rep3(i,a,b) for(ll i=a;i>=b;i--) #define pii pair<int,int> #define pll pair<ll,ll> #define pq priority_queue<int> #define pqg priority_queue<int,vector<int>,greater<int>> #define pb push_back #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define endl "\n" #define all(c) begin(c),end(c) using namespace std; int in() {int x;scanf("%d",&x);return x;} ll lin() {ll x;scanf("%lld",&x);return x;} void print(vec v){for(auto e:v)cout<<e<<" ";cout<<endl;} void print(vecll v){for(auto e:v)cout<<e<<" ";cout<<endl;} void print(map<int,int> mp){for(auto e:mp)cout<<e.first<<" "<<e.second<<endl;cout<<endl;} #define INF 1e9+7 #define LLINF 1e18+7 #define N 250000 ll MOD=1e9+7; vector<vec> G; int n,k; int dfs(int e,int x,int t); int calc(int x){ int res=INF; for(auto e:G[x]){ res=min(n-1-dfs(e,x,1),res); } return res; } int dfs(int e,int x,int t){ int res=1; if(t>k)return 0; for(auto s:G[e]){ if(s==x)continue; res+=dfs(s,e,t+1); } return res; } main(){ cin>>n>>k; G.resize(n+1); rep(i,n-1){ int a=in(),b=in(); G[a].pb(b);G[b].pb(a); } int ans=INF; rep2(i,1,n){ ans=min(ans,calc(i)); } cout<<ans<<endl; }
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s076769013
p04049
Runtime Error
n,k = map(int,input().split()) INF = float("inf") edges = [[] for i in range(n)] ega = [] for i in range(n-1): a,b = map(int,input().split()) a = a-1 b = b-1 edges[a].append(b) edges[b].append(a) ega.append([a,b]) def dfs(v): d= [0] * n q=[v] visited = [0] * n while q: v=q.pop() visited[v] = 1 visited.append(v) for i in edges[v]: if not visited[i]: d[i] = d[v] + 1 q.append(i) return d mi = INF table = [] for i in range(n): table.append(dfs(i)) if k%2 == 0: for i in range(n): disi = table[i] c = 0 for m in range(n): if disi[m] > k/2: c += 1 if mi>c: mi = c print(mi) else: for edge in ega: c = 0 if table[edge[0]] + table[edge[1]] > k-1: c += 1 if mi>c: mi = c print(mi)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s963926242
p04049
Runtime Error
n,k = map(int,input().split()) import numpy as np INF = float("inf") edges = [[] for i in range(n)] ega = [] for i in range(n-1): a,b = map(int,input().split()) a = a-1 b = b-1 edges[a].append(b) edges[b].append(a) ega.append([a,b]) def dfs(v): visited = [] INF = float("inf") d=[INF for i in range(n)] q=[] d[v]=0 q.append(v) while len(q): v=q.pop() visited.append(v) for i in edges[v]: if i not in visited: d[i] = d[v] + 1 q.append(i) else: continue return d mi = INF table = [] for i in range(n): table.append(dfs(i)) if k%2 == 0: for i in range(n): disi = table[i] c = 0 for m in range(n): if disi[m] > k/2: c += 1 if mi>c: mi = c print(mi) else: for edge in ega: c = 0 if table[edge[0]] + table[edge[1]] > k-1: c += 1 if mi>c: mi = c print(mi)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s215625013
p04049
Runtime Error
N, K = map(int, input().split()) G = [[] for i in range(N)] for i in range(N - 1): a, b = map(int, input().split()) G[a - 1].append(b - 1) G[b - 1].append(a - 1) def dfs(u, par, d): global tmp if d > K // 2: tmp += 1 for w in G[u]: if w == par: continue dfs(w, u, d + 1) num = float("inf") for i in range(N): tmp = 0 dfs(i, -1, 0) num = min(num, tmp) print(num)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s609326818
p04049
Runtime Error
#import sys #input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) def f(x,y,c): global tmp c += 1 for e in edge[x]: if e == y: continue if c > d: tmp +=1 f(e,x,c) #sys.setrecursionlimit(4100000) if k % 2 == 0: d = k//2 ass = 2001 for i in range(n): tmp = 0 f(i,-1,0) ass = min(ass,tmp) print(ass) else: d = (k-1)//2 ass = 2001 for e1 in alledge: tmp = 0 f(e1[0],e1[1],0) f(e1[1],e1[0],0) ass = min(ass,tmp) print(ass)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s532382604
p04049
Runtime Error
import sys sys.setrecursionlimit(5000000) input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s931452563
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: if c > d and e >= 0 and e<= n-1: used[e] = 1 if len(edge[e])>=2: f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s238312663
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x): #c +=1 for e in edge[x]: #if e != y: #if c > d: # used[e] = 1 f(e) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0]) f(e1[1]) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s587581130
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y): #c +=1 for e in edge[x]: if e != y: #if c > d: # used[e] = 1 f(e,x) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1]) f(e1[1],e1[0]) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s300255193
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: if c > d and e >= 0 and e<= n-1: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) #f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s691044328
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 if len(edge)-1 >=x and x >= 0: for e in edge[x]: if e != y: if c > d and e >= 0 and e<= n-1: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s299004138
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 if len(edge)-1 >=x and x >= 0: for e in edge[x]: if e != y: if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: nag = [0]*n used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s584456806
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: #if c > d: #used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: nag = [0]*n used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s310044627
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) def g(x,y): for e in edge[x]: if e == y: continue nag[e] = nag[x]+1 if nag[e] > d: used[e] = 1 g(e,x) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: nag = [0]*n used = [0]*n g(e1[0],e1[1]) g(e1[1],e1[0]) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s621226825
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s726216232
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: if k == 1: print(n-2) exit() d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s811438918
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: print(0) exit() d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s626659322
p04049
Runtime Error
#import sys #input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s397303827
p04049
Runtime Error
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s098360855
p04049
Runtime Error
n, k = map(int, input().split()) import sys sys.setrecursionlimit(2000) l = [[] for i in range(n)] for i in range(n-1): a, b = map(int, input().split()) l[a-1].append(b-1) l[b-1].append(a-1) def first_search(first, depth): record = 0 for i in l[first]: temp = search(first, i, depth-1) if record < temp: record = temp return record + 1 def search(before, now, depth): if depth <= 0: return 1 else: ans = 1 for i in l[now]: if before != i: ans += search(now, i, depth-1) return ans ret = 0 if k%2 == 0: for i in range(n): temp = search(-1, i, k//2) if temp > ret: ret = temp else: for i in range(n): temp = first_search(i, k) if temp > ret: ret = temp print(n-ret)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s651912799
p04049
Runtime Error
n, k = map(int, input().split()) import sys sys.setrecursionlimit(2000) l = [[] for i in range(n)] for i in range(n-1): a, b = map(int, input().split()) l[a-1].append(b-1) l[b-1].append(a-1) def first_search(first, depth): record = 0 for i in l[first]: temp = search(first, i, depth-1) if record < temp: record = temp return record + 1 def search(before, now, depth): if depth <= 0: return 1 else: ans = 1 for i in l[now]: if before != i: ans += search(now, i, depth-1) return ans ret = 0 for i in range(n): temp = first_search(i, k) if temp > ret: ret = temp print(n-ret)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s070727505
p04049
Runtime Error
n, k = map(int, input().split()) import sys sys.setrecursionlimit(2000) l = [[] for i in range(n)] for i in range(n-1): a, b = map(int, input().split()) l[a-1].append(b-1) l[b-1].append(a-1) def first_search(first, depth): record = 0 for i in l[first]: temp = search(first, i, depth-1) if record < temp: record = temp return record + 1 def search(before, now, depth): if depth == 0: return 1 else: ans = 1 for i in l[now]: if before != i: ans += search(now, i, depth-1) return ans ret = 0 for i in range(n): temp = first_search(i, k) if temp > ret: ret = temp if ret > n: print(0) else: print(n-ret)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s799452240
p04049
Runtime Error
n, k = map(int, input().split()) l = [[] for i in range(n)] for i in range(n-1): a, b = map(int, input().split()) l[a-1].append(b-1) l[b-1].append(a-1) l2 = [0 for i in range(n)] def first_search(first, depth): record = 0 for i in l[first]: temp = search(first, i, depth-1) if record < temp: record = temp return record+1 def search(before, now, depth): if depth == 0: return 1 else: ans = 1 for i in l[now]: if before != i: ans += search(now, i, depth-1) return ans ret = 0 for i in range(n): temp = first_search(i, k) if temp > ret: ret = temp if ret > n: print(0) else: print(n-ret)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s284151588
p04049
Runtime Error
import sys stdin = sys.stdin def li(): return map(int, stdin.readline().split()) def li_(): return map(lambda x: int(x)-1, stdin.readline().split()) def lf(): return map(float, stdin.readline().split()) def ls(): return stdin.readline().split() def ns(): return stdin.readline().rstrip() def lc(): return list(ns()) def ni(): return int(stdin.readline()) def nf(): return float(stdin.readline()) from collections import deque from collections import defaultdict def bfs(graph:list, start:int) -> list: # 未探索のノードは距離null node = len(graph) dist = [None]*node # 始点ノードの距離を0とし、bfsのためのqueueを作成 dist[start] = 0 que = deque([(0,start)]) # 未探索のノードをqueueに入れる # kより大きいの距離のものを数える while que: cost, cur_node = que.popleft() for nex_cost, nex_node in graph[cur_node]: if dist[nex_node] is not None: continue else: dist[nex_node] = dist[cur_node] + nex_cost que.append((dist[nex_node], nex_node)) return dist # 入力, グラフ作成 n,k = li() adj_list = [[(1,i+1)] for i in range(1999)] adj_list.append([]) edges = [] for i in range(1,2000): adj_list[i].append((1,i-1)) edges.append((i-1,i)) for i in range(2000): d = bfs(adj_list, i) start = t.clock() ans = n # kが奇数の時 if k%2: for a,b in edges: dist1 = bfs(adj_list, a) dist2 = bfs(adj_list, b) ans = min(ans, sum([min(d1,d2) > (k-1)//2 for d1,d2 in zip(dist1, dist2)])) # kが偶数の時 else: for st in range(n): dist = bfs(adj_list, st) ans = min(ans, sum([d > k//2 for d in dist])) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s304854364
p04049
Runtime Error
import numpy as np N, K = map(int, input().split()) E = np.zeros((N, N), dtype=int) # Elist = [] for e in range(N-1): i, o = map(int, input().split()) # Elist.append((i-1, o-1)) E[i-1][o-1] = 1 E[o-1][i-1] = 1 # print("N, K=", N, K) # for e in Elist: # print(e[0], e[1]) I = np.eye(N, dtype=int) # print("I=", I) ie = I for k in range(K): ie = ie + np.matmul(I, E) # print("I + IE =", ie) exit(0) # withinK = np.count_nonzero(ie, axis=0) withinK = np.zeros(N, dtype=int) for i in range(N): for j in range(N): if ie[i][j] > 0: withinK[i] += 1 # print("withinK=", withinK) maxBall = np.amax(withinK) # print("maxBall=", maxBall) solution = N - maxBall print(solution)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s669543599
p04049
Runtime Error
import numpy as np N, K = map(int, input().split()) E = np.zeros((N, N), dtype=int) Elist = [] for e in range(N-1): i, o = map(int, input().split()) Elist.append((i-1, o-1)) E[i-1][o-1] = 1 E[o-1][i-1] = 1 # print("N, K=", N, K) # for e in Elist: # print(e[0], e[1]) I = np.eye(N, dtype=int) # print("I=", I) ie = I for k in range(K): ie = ie + np.matmul(I, E) # print("I + IE =", ie) # withinK = np.count_nonzero(ie, axis=0) withinK = np.zeros(N, dtype=int) for i in range(N): for j in range(N): if ie[i][j] > 0: withinK[i] += 1 # print("withinK=", withinK) maxBall = np.amax(withinK) # print("maxBall=", maxBall) solution = N - maxBall print(solution)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s624676471
p04049
Runtime Error
import numpy as np N, K = map(int, input().split()) E = np.zeros((N, N), dtype=int) Elist = [] for e in range(N-1): i, o = map(int, input().split()) Elist.append((i-1, o-1)) E[i-1][o-1] = 1 E[o-1][i-1] = 1 # print("N, K=", N, K) # for e in Elist: # print(e[0], e[1]) I = np.eye(N, dtype=int) # print("I=", I) ie = I for k in range(K): ie = ie + np.matmul(I, E) # print("I + IE =", ie) withinK = np.count_nonzero(ie, axis=0) # for i in range(N): # withinK[i] = np.count_nonzero(ie[i]) # print("withinK=", withinK) maxBall = np.amax(withinK) # print("maxBall=", maxBall) solution = N - maxBall print(solution)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s423454121
p04049
Runtime Error
from collections import deque N, K = map(int, input().split()) T = [[] for i in range(N)] E = [] for i in range(N-1): a, b = map(int, input().split()) a, b = a-1, b-1 T[a].append(b) T[b].append(a) E.append((a, b)) def bfs(n): visited = [False] * N dist = [0] * N queue = deque([n]) while queue: node = queue.pop() if visited[node]: continue visited[node] = True for n in T[node]: if not visited[n]: dist[n] = dist[node] + 1 queue.appendleft(n) return dist dist = [] for i in range(N): dist.append(bfs(i)) ans = float('inf') if K % 2 == 0: # 全ての頂点について全探索 for i in range(N): ans = min(ans, len(d for d in dist[i] if d > K / 2)) else: # 全ての辺について全探索 for a, b in E: adist = [(1 if min(d1, d2) > (K-1) / 2 else 0) for d1, d2 in zip(dist[a], dist[b])] ans = min(ans, sum(adist)) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s889802136
p04049
Runtime Error
from collections import deque N, K = map(int, input().split()) T = [[] for i in range(N)] E = [] for i in range(N-1): a, b = map(int, input().split()) a, b = a-1, b-1 T[a].append(b) T[b].append(a) E.append((a, b)) def bfs(n): visited = [False] * N dist = [0] * N queue = deque([n]) while queue: node = queue.pop() if visited[node]: continue visited[node] = True for n in T[node]: if not visited[n]: dist[n] = dist[node] + 1 queue.appendleft(n) return dist dist = [] for i in range(N): dist.append(bfs(i)) ans = float('inf') if K % 2 == 0: # 全ての頂点について全探索 for i in range(N): ans = min(ans, len(list(filter(lambda x: K / 2 < x, dist[i])))) else: # 全ての辺について全探索 for a, b in E: dist = [min(d1, d2) for d1, d2 in zip(dist[a], dist[b])] ans = min(ans, len(list(filter(lambda x: (K-1) / 2 < x, dist)))) print(ans)
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s566373062
p04049
Runtime Error
o=lambda:map(int,raw_input().split());r=range(n);a=10**5 n,k=o() e=[[]for _ in [0]*n] for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1] def U(x,f): l[f]=1;q=[(x,0)] while len(q): v,c=q.pop(0) if c>k/2:break l[v]=1 for w in e[v]: if not l[w]:q+=[(w,c+1)] return n-l.count(1) if k%2: for i in r: for j in e[i]: if i<j:l=[0]*n;U(i,j);a=min(a,U(j,i)) else: for i in r:l=[0]*n;a=min(a,U(i,-1)) print a
6 2 1 2 3 2 4 2 1 6 5 6
2
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
s287463219
p04050
Wrong Answer
from sys import exit n, m = map(int, input().split()) a = list(map(int, input().split())) odd = [] even = [] for x in a: if x%2 == 0: even.append(x) else: odd.append(x) if n%2 == 0: if len(odd) > 2: print("Impossible") exit() if len(odd) == 2: a = [odd[0]] + even + [odd[1]] b = [odd[0]+1] + even + [odd[1]-1] if b[-1] == 0: b.pop() else: a = even b = [1] + even b[-1] -= 1 if b[-1] == 0: b.pop() else: if len(odd) > 1: print("Impossible") exit() if m == 1: b = [1, n-1] else: a = odd + even b = [odd[0]+1] + even b[-1] -= 1 if b[-1] == 0: b.pop() print(*a) print(len(b)) print(*b)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s104198375
p04050
Wrong Answer
from sys import exit n, m = map(int, input().split()) a = list(map(int, input().split())) odd = [] even = [] for x in a: if x%2 == 0: even.append(x) else: odd.append(x) if n%2 == 0: if len(odd) > 2: print("Impossible") exit() if len(odd) == 2: a = [odd[0]] + even + [odd[1]] b = [odd[0]+1] + even + [odd[1]-1] if b[-1] == 0: b.pop() else: a = even b = [1] + even b[-1] -= 1 if b[-1] == 0: b.pop() else: if len(odd) > 1: print("Impossible") exit() a = odd + even b = [odd[0]+1] + even b[-1] -= 1 if b[-1] == 0: b.pop() print(*a) print(len(b)) print(*b)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s607817470
p04050
Wrong Answer
import sys def solve(n, m, arr): odds = [] evens = [] for a in arr: if a % 2 == 0: evens.append(a) else: odds.append(a) if len(odds) > 2: print('Impossible') return if len(odds) == 2: o1, o2 = odds aaa = [o1] + evens + [o2] bbb = [2] * (o1 // 2 + 1) bbb += evens bbb += [2] * (o2 // 2) elif len(odds) == 1: if evens: aaa = odds + evens bbb = list(reversed(aaa)) elif odds[0] == 1: aaa = odds bbb = odds else: aaa = odds bbb = [odds[0] // 2, odds[0] // 2 + 1] else: aaa = evens bbb = [1] + aaa[:-1] + [aaa[-1] - 1] print(*aaa) print(len(bbb)) print(*bbb) n, m, *aaa = map(int, sys.stdin.buffer.read().split()) solve(n, m, aaa)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s701292968
p04050
Wrong Answer
import sys def solve(n, m, arr): odds = [] evens = [] for a in arr: if a % 2 == 0: evens.append(a) else: odds.append(a) if len(odds) > 2: print('Impossible') return if len(odds) == 2: o1, o2 = odds aaa = [o1] + evens + [o2] bbb = [2] * (o1 // 2 + 1) bbb += evens bbb += [2] * (o2 // 2) elif len(odds) == 1: aaa = odds + evens bbb = list(reversed(aaa)) else: aaa = evens bbb = [1] + aaa[:-1] + [aaa[-1] - 1] print(*aaa) print(len(bbb)) print(*bbb) n, m, *aaa = map(int, sys.stdin.buffer.read().split()) solve(n, m, aaa)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s498481701
p04050
Wrong Answer
n,m=map(int,input().split()) a=list(map(int,input().split())) b=[] o=[] for i in a: if i%2:o.append(i) else:b.append(i) if len(o)>2:print("Impossible");exit() if len(o)==1:b=o+b elif len(o)==2:b=[o[0]]+b+[o[1]] ans=[] for i in range(len(b)): if i==0:ans.append(b[i]-1) elif i==len(b)-1:ans.append(b[i]+1) else:ans.append(b[i]) anss=[] for i in ans: if i!=0:anss.append(i) if sum(anss)!=n:anss.append(n-sum(anss)) print(*a) print(len(anss)) print(*anss)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s799170240
p04050
Wrong Answer
n,m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] breakFlag = False for i in range(1,m-1): if a[i]%2==1: if a[0]%2==1: if a[len(a)-1]%2==1: print("Impossible") breakFlag = True break else: a[i],a[len(a)-1] = a[len(a)-1],a[i] else: a[i],a[0] = a[0],a[i] if breakFlag==False: ans = ''.join([str(s)+" " for s in a]) print(ans[:-1]) if m==1: if len(a)==1: if a[0]>1: a[0]-=1 a.append(1) print(m+1) else: print(m) else: print(m-1) a.pop() a[0]+=1 else: print(m) a[0]+=1 a[len(a)-1] -= 1 ans = ''.join([str(s)+" " for s in a]) print(ans[:-1])
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s252919422
p04050
Wrong Answer
n,m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] breakFlag = False for i in range(m): if a[i]%2==1: if a[0]%2==1: if a[len(a)-1]%2==1: print("Impossible") breakFlag = True break else: a[i],a[len(a)-1] = a[len(a)-1],a[i] else: a[i],a[0] = a[0],a[i] ans = ''.join([str(s)+" " for s in a]) print(ans[:-1]) a[0]-=2 print(m) ans = ''.join([str(s+1)+" " for s in a]) print(ans[:-1])
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s257283179
p04050
Wrong Answer
import sys n, m = map(int, input().split(' ')) A = list(map(int, input().split(' '))) A_odds = [x for x in A if x % 2 == 0] A_evens = [x for x in A if x % 2 == 1] a = [] b = [] if len(A_odds) > 2: print('Impossible') sys.exit(); if len(A_odds) != 0: a.append(A_odds[0]) a += A_evens if len(A_odds) == 2: a.append(A_odds[1]) if len(a) == 1: if a[0] == 1: b.append(1) else: b.append(a[0] - 1) b.append(1) else: if a[0] == 1: b.append(2) b += a[1:-1] if a[-1] != 1: b.append(a[-1] - 1) else: b.append(a[0] - 1) b += a[1:-1] b.append(a[-1] + 1) else: a += A_evens b.append(a[0] - 1) b += a[1:-1] b.append(a[-1] + 1) print(' '.join(map(str,a))) print(str(len(b))) print(' '.join(map(str,b)))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s499016448
p04050
Wrong Answer
n, m = map(int, raw_input().split()) a = map(int,raw_input().split()) cntodd = 0 for x in a:cntodd += x % 2 if cntodd > 2: print "Impossible" exit() evens = [x for x in a if x % 2 == 0] odds = [x for x in a if x % 2 == 1] if len(a) == 1: print a[0] print 2 print a[0]-1,1 exit() if cntodd == 2: a = [odds[0]] + evens + [odds[1]] b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 1: a = [odds[0]] + evens b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 0: a = evens b = a[::] b[0] += 2 b[-1] -= 2 if b[-1] == 0: b = b[:-1] print " ".join(map(str, a)) print len(b) print " ".join(map(str, b))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s931854329
p04050
Wrong Answer
n, m = map(int, raw_input().split()) a = map(int,raw_input().split()) cntodd = 0 for x in a:cntodd += x % 2 if cntodd > 2: print "Impossible" exit() evens = [x for x in a if x % 2 == 0] odds = [x for x in a if x % 2 == 1] if cntodd == 2: a = odds[0] + evens + odds[1] b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 1: a = odds[0] + evens b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 0: a = evens b = a[::] b[0] += 2 b[-1] -= 2 if b[-1] == 0: b = b[:-1] print " ".join(map(str, a)) print len(b) print " ".join(map(str, b))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s743351234
p04050
Accepted
from sys import exit n, m = map(int, input().split()) a = list(map(int, input().split())) odd = [] even = [] for x in a: if x%2 == 0: even.append(x) else: odd.append(x) if n%2 == 0: if len(odd) > 2: print("Impossible") exit() if len(odd) == 2: a = [odd[0]] + even + [odd[1]] b = [odd[0]+1] + even + [odd[1]-1] if b[-1] == 0: b.pop() else: a = even b = [1] + even b[-1] -= 1 if b[-1] == 0: b.pop() else: if len(odd) > 1: print("Impossible") exit() if m == 1: b = [1, n-1] else: a = odd + even b = [odd[0]+1] + even b[-1] -= 1 if b[-1] == 0: b.pop() print(*a) print(len(b)) print(*b)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s965943795
p04050
Accepted
import sys def solve(n, m, arr): odds = [] evens = [] for a in arr: if a % 2 == 0: evens.append(a) else: odds.append(a) if len(odds) > 2: print('Impossible') return if len(odds) == 2: o1, o2 = odds aaa = [o1] + evens + [o2] bbb = [2] * (o1 // 2 + 1) bbb += evens bbb += [2] * (o2 // 2) elif len(odds) == 1: if odds[0] == 1: aaa = odds + evens bbb = evens + odds elif evens: aaa = odds + evens bbb = [odds[0] - 1] + evens[:-1] + [evens[-1] + 1] else: aaa = odds bbb = [odds[0] // 2, odds[0] // 2 + 1] else: aaa = evens bbb = [1] + evens[:-1] + [evens[-1] - 1] print(*aaa) print(len(bbb)) print(*bbb) n, m, *aaa = map(int, sys.stdin.buffer.read().split()) solve(n, m, aaa)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s571339864
p04050
Accepted
n,m=map(int,input().split()) a=list(map(int,input().split())) b=[] o=[] for i in a: if i%2:o.append(i) else:b.append(i) if len(o)>2:print("Impossible");exit() if len(o)==1:b=o+b elif len(o)==2:b=[o[0]]+b+[o[1]] ans=[] for i in range(len(b)): if i==0:ans.append(b[i]-1) elif i==len(b)-1:ans.append(b[i]+1) else:ans.append(b[i]) anss=[] for i in ans: if i!=0:anss.append(i) if sum(anss)!=n:anss.append(n-sum(anss)) print(*b) print(len(anss)) print(*anss)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s943528127
p04050
Accepted
n,m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] breakFlag = False for i in range(1,m-1): if a[i]%2==1: if a[0]%2==1: if a[len(a)-1]%2==1: print("Impossible") breakFlag = True break else: a[i],a[len(a)-1] = a[len(a)-1],a[i] else: a[i],a[0] = a[0],a[i] if breakFlag==False: ans = ''.join([str(s)+" " for s in a]) print(ans[:-1]) if m==1: if a[0]>1: a[0]-=1 a.append(1) print(m+1) else: print(m) else: if a[len(a)-1]==1: print(m-1) a.pop() a[0]+=1 else: print(m) a[0]+=1 a[len(a)-1] -= 1 ans = ''.join([str(s)+" " for s in a]) print(ans[:-1])
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s065079131
p04050
Accepted
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 7) N,M = map(int,readline().split()) A = [int(x) for x in readline().split()] OD = [x for x in A if x&1] EV = [x for x in A if not x&1] if len(OD) >= 3: print('Impossible') exit() if len(A) == 1: if N == 1: B = [1] else: B = [1,N-1] elif len(OD) == 0: B = A.copy() B[0] -= 1 B[-1] += 1 elif len(OD) == 1: A = OD + EV B = A.copy() B[0] += 1 B[-1] -= 1 else: A = [OD[0]] + EV + [OD[-1]] B = A.copy() B[0] += 1 B[-1] -= 1 B = [x for x in B if x > 0] print(*A) print(len(B)) print(*B)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s952339729
p04050
Accepted
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ agc001 D """ n, m = map(int, input().split()) ali = list(map(int, input().split())) if m == 1 and ali[0] == 1: print(1) print(1) print(1) exit() if m == 1: print(ali[0]) print(2) print(ali[0]-1, 1) exit() flag = 0 c = [] for a in ali: if a % 2 == 1: if flag == 0: lm = a flag += 1 elif flag == 1: rm = a flag += 1 elif flag == 2: print('Impossible') exit() else: c.append(a) if flag == 0: lm = c[0] del c[0] rm = c.pop() if flag == 1: rm = c.pop() b = [lm+1] + c d = [lm] + c + [rm] if rm > 1: b.append(rm-1) print(" ".join(map(str, d))) print(len(b)) print(" ".join(map(str, b)))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s213815438
p04050
Accepted
#!/usr/bin/env python3 def solve(n, m, a): odd = [] even = [] for a_i in a: if a_i % 2 == 0: even += [ a_i ] else: odd += [ a_i ] if len(odd) >= 3: return None a, b = [], [] if odd: x = odd.pop() a += [ x ] b += [ x - 1 ] else: x = even.pop() a += [ x ] b += [ x - 1 ] a += even b += even if odd: x = odd.pop() a += [ x ] b += [ x + 1 ] else: b += [ 1 ] return a, b n, m = map(int, input().split()) a = list(map(int, input().split())) it = solve(n, m, a) if it is None: print('Impossible') else: a, b = it b = list(filter(lambda b_i: b_i, b)) print(*a) print(len(b)) print(*b)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s637050993
p04050
Accepted
S, N = map(int, input().split()) A = list(map(int, input().split())) O = [a for a in A if a % 2 == 1] E = [a for a in A if a % 2 == 0] if len(O) > 2: print("Impossible") else: A = O[:min(len(O), 1)] + E + O[1:] B = A[:] + ([0] if N == 1 else []) B[0] -= 1 B[-1] += 1 if B[0] == 0: B = B[1:] print(*A, sep=" ") print(len(B)) print(*B, sep=" ")
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s709919279
p04050
Accepted
S, N = map(int, input().split()) A = list(map(int, input().split())) O = [a for a in A if a % 2 == 1] E = [a for a in A if a % 2 == 0] if len(O) > 2: print("Impossible") else: A = O[:min(len(O), 1)] + E + O[min(1, len(O)):] B = A[:] + ([0] if len(A) == 1 else []) B[0] -= 1 B[-1] += 1 if B[0] == 0: B = B[1:] print(" ".join(str(a) for a in A)) print(len(B)) print(" ".join(str(b) for b in B))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s934501506
p04050
Accepted
n, m = map(int, raw_input().split()) a = map(int,raw_input().split()) cntodd = 0 for x in a:cntodd += x % 2 if cntodd > 2: print "Impossible" exit() evens = [x for x in a if x % 2 == 0] odds = [x for x in a if x % 2 == 1] if len(a) == 1: if a[0] == 1: print 1 print 1 print 1 exit() print a[0] print 2 print a[0]-1,1 exit() if cntodd == 2: a = [odds[0]] + evens + [odds[1]] b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 1: a = [odds[0]] + evens b = a[::] b[0] += 1 b[-1] -= 1 if cntodd == 0: a = evens b = a[::] b[0] += 1 b[-1] -= 1 if b[-1] == 0: b = b[:-1] print " ".join(map(str, a)) print len(b) print " ".join(map(str, b))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s678667268
p04050
Accepted
from sys import stdout def main(): n, m = map(int, raw_input().split()) a = map(int, raw_input().split()) if m == 1: print a[0] if n == 1: print 1 print 1 else: print 2 print 1, n - 1 return if sum(x % 2 for x in a) > 2: print "Impossible" return a.sort(key=lambda x: x%2) a = a[-1:] + a[:-1] t = a[:] t[0] += 1 t[-1] -= 1 if not t[-1]: t.pop() stdout.write(' '.join(map(str, a))) stdout.write('\n') stdout.write(str(len(t)) + '\n') stdout.write(' '.join(map(str, t))) stdout.write('\n') main()
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s960682356
p04050
Runtime Error
n,m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] breakFlag = False for i in range(1,m-1): if a[i]%2==1: if a[0]%2==1: if a[len(a)-1]%2==1: print("Impossible") breakFlag = True break else: a[i],a[len(a)-1] = a[len(a)-1],a[i] else: a[i],a[0] = a[0],a[i] if breakFlag==False: ans = ''.join([str(s)+" " for s in a]) print(ans[:-1]) if a[len(a)-1]==1: print(m-1) a.pop() a[0]+=1 else: print(m) a[0]+=1 a[len(a)-1] -= 1 ans = ''.join([str(s)+" " for s in a]) print(ans[:-1])
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s347349727
p04050
Runtime Error
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 7) N,M = map(int,readline().split()) A = [int(x) for x in readline().split()] OD = [x for x in A if x&1] EV = [x for x in A if not x&1] if len(OD) >= 3: print('Impossible') exit() if len(A) == 1: if N == 1: B = [1] else: B = [1,N-1] elif len(OD) == 0: B = A.copy() B[0] -= 1 B[-1] += 1 elif len(OD) == 1: A = OD + EV B = A.copy() B[0] += 1 B[-1] -= 1 else: A = OD[0] + EV + OD[-1] B = A.copy() B[0] += 1 B[-1] -= 1 B = [x for x in B if x > 0] print(*A) print(len(B)) print(*B)
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s747140087
p04050
Runtime Error
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ agc001 D """ n, m = map(int, input().split()) ali = list(map(int, input().split())) if m == 1: print(ali[0]) print(2) print(ali[0]-1, 1) exit() flag = 0 c = [] for a in ali: if a % 2 == 1: if flag == 0: lm = a flag += 1 elif flag == 1: rm = a flag += 1 elif flag == 2: print('Impossible') exit() else: c.append(a) if flag == 1: rm = c.pop() b = [lm+1] + c d = [lm] + c + [rm] if rm > 1: b.append(rm-1) print(" ".join(map(str, d))) print(len(b)) print(" ".join(map(str, b)))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s950372139
p04050
Runtime Error
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ agc001 D """ n, m = map(int, input().split()) ali = list(map(int, input().split())) if m == 1: print(ali[0]) print(1) print(ali[0]-1, 1) exit() flag = 0 c = [] for a in ali: if a & 1: if flag == 0: lm = a flag += 1 elif flag == 1: rm = a flag += 1 elif flag == 2: print('Impossible') exit() else: c.append(a) if flag == 1: rm = c.pop() b = [lm+1] + c d = [lm] + c + [rm] if rm > 1: b.append(rm-1) print(" ".join(map(str, d))) print(len(b)) print(" ".join(map(str, b)))
3 2 2 1
1 2 1 3
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
s507846696
p04051
Wrong Answer
from scipy.special import comb MOD = 1000000007 #n = 3 #a = [1,1,2] #b = [1,1,1] n = int(input()) a = [0] * n b = [0] * n for i in range(n): a[i], b[i] = map(int, input().split()) count = 0 for i in range(n-1): M = a[i] + b[i] for j in range(i+1, n): N = M + a[j] + b[j] # print(i+1, j+1, comb(a[i] + b[i] + a[j] + b[j], a[i] + a[j], True)) count += comb(N, M, True) % MOD count = count % MOD print(count)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s857122274
p04051
Wrong Answer
import sys from collections import defaultdict import numpy as np def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs n = int(sys.stdin.buffer.readline()) mp = map(int, sys.stdin.buffer.read().split()) ab = list(zip(mp, mp)) atob = defaultdict(list) max_a, max_b = 0, 0 for a, b in ab: atob[a].append(b) max_a = max(max_a, a) max_b = max(max_b, b) MOD = 10 ** 9 + 7 dp = np.zeros(max_b * 2 + 1, dtype=np.int64) ans = 0 for i in range(-max_a, max_a + 1): if i < 0 and -i in atob: for j in atob[-i]: dp[-j + max_b] += 1 dp = np.add.accumulate(dp) % MOD if i > 0 and i in atob: for j in atob[i]: ans = (ans + dp[j + max_b]) % MOD facts, invs = prepare(2 * (max_a + max_b), MOD) ans = int(ans) for a, b in ab: ans = (ans - facts[2 * (a + b)] * invs[2 * a] * invs[2 * b]) % MOD print(ans // 2)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s357060639
p04051
Wrong Answer
U = 10**6 MOD = 10**9+7 fact = [1]*(U+1) fact_inv = [1]*(U+1) for i in range(1,U+1): fact[i] = (fact[i-1]*i)%MOD fact_inv[U] = pow(fact[U],MOD-2,MOD) for i in range(U,0,-1): fact_inv[i-1] = (fact_inv[i]*i)%MOD def comb(n,k): if k < 0 or k > n: return 0 z = fact[n] z *= fact_inv[k] z %= MOD z *= fact_inv[n-k] z %= MOD return z n = int(input()) ab = [ list(map(int, input().split())) for _ in range(n) ] ans = 0 for i in range(n-1): for j in range(i+1, n): ai, bi = ab[i] aj, bj = ab[j] ans += comb(ai+bi+aj+bj, ai+aj) print(ans)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s185569170
p04051
Wrong Answer
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10 ** 9 + 7 N = int(readline()) m = map(int,read().split()) AB = zip(m,m) D = 2000 U = 10 ** 4 fact = [1] * U for n in range(1,U): fact[n] = fact[n-1] * n % MOD fact_inv = [1] * U fact_inv[-1] = pow(fact[-1],MOD-2,MOD) for n in range(U-1,0,-1): fact_inv[n-1] = fact_inv[n] * n % MOD C_to_A = [[] for _ in range(D+D+1)] remove = 0 for a,b in AB: c = a+b C_to_A[c].append(D-a) remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b] dp = np.zeros(D+D+1,np.int64) for A in C_to_A[::-1]: dp[1:] += dp[:-1].copy() np.add.at(dp,A,1) dp %= MOD x = (dp * dp[::-1] % MOD).sum() % MOD x -= remove x *= fact_inv[2] x %= MOD print(x)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s033294395
p04051
Wrong Answer
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10 ** 9 + 7 N = int(readline()) m = map(int,read().split()) AB = zip(m,m) D = 2000 def cumprod(arr,MOD): L = len(arr); Lsq = int(L**.5+1) arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq) for n in range(1,Lsq): arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD for n in range(1,Lsq): arr[n] *= arr[n-1,-1]; arr[n] %= MOD return arr.ravel()[:L] def make_fact(U,MOD): x = np.arange(U,dtype=np.int64); x[0] = 1 fact = cumprod(x,MOD) x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD) fact_inv = cumprod(x,MOD)[::-1] return fact,fact_inv fact,fact_inv = make_fact(10 ** 4,MOD) fact = fact.tolist() fact_inv = fact_inv.tolist() C_to_A = [[] for _ in range(D+D+1)] remove = 0 for a,b in AB: c = a+b C_to_A[c].append(D-a) remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b] remove %= MOD dp = np.zeros(D+D+1,np.int64) for A in C_to_A[::-1]: dp[1:] += dp[:-1].copy() np.add.at(dp,A,1) x = (dp * dp[::-1] % MOD).sum() % MOD x -= remove if x&1: x += MOD x //= 2 x %= MOD print(x)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s214606202
p04051
Wrong Answer
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10 ** 9 + 7 N = int(readline()) m = map(int,read().split()) AB = zip(m,m) D = 2000 def cumprod(arr,MOD): L = len(arr); Lsq = int(L**.5+1) arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq) for n in range(1,Lsq): arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD for n in range(1,Lsq): arr[n] *= arr[n-1,-1]; arr[n] %= MOD return arr.ravel()[:L] def make_fact(U,MOD): x = np.arange(U,dtype=np.int64); x[0] = 1 fact = cumprod(x,MOD) x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD) fact_inv = cumprod(x,MOD)[::-1] return fact,fact_inv fact,fact_inv = make_fact(10 ** 4,MOD) fact = fact.tolist() fact_inv = fact_inv.tolist() C_to_A = [[] for _ in range(D+D+1)] remove = 0 for a,b in AB: c = a+b C_to_A[c].append(D-a) remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b] remove %= MOD dp = np.zeros(D+D+1,np.int64) for A in C_to_A[::-1]: dp[1:] += dp[:-1] np.add.at(dp,A,1) x = (dp * dp[::-1] % MOD).sum() % MOD x -= remove if x&1: x += MOD x //= 2 x %= MOD print(x)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s041011593
p04051
Wrong Answer
import math def combinations_count(n, r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) N = int(input()) L = list(list(map(int, input().split())) for i in range(N)) cnt = 0 roop = 0 for i in range(N): for j in range(i+1, N): n = L[i][0] + L[i][1] + L[j][0] + L[j][1] r = L[i][0] + L[j][0] cnt += combinations_count(n, r) roop += 1 if roop == 100: cnt //= 1000000007 roop = 0 print(cnt)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s057479359
p04051
Wrong Answer
def proc(n): res = 1 for i in range(1,n+1): res *= i return res def co(a, b): res = proc(a) // proc(a-b) // proc(b) return res n = int(input()) a = [0 for i in range(n)] b = [0 for i in range(n)] for ni in range(n): a[ni], b[ni] = (int(i) for i in input().split(" ")) res = 0 for i in range(n): for j in range(i+1,n): res += co(a[i]+a[j]+b[i]+b[j], b[i]+b[j]) print(res)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s609443365
p04051
Wrong Answer
n = int(input()) arr = [map(int,raw_input().split()) for i in range(n)] count = 0 for i in range(n-1): for j in range(i+1,n): s = sum(arr[i]) + sum(arr[j]) comb = s*(s-1)/2 count += comb print count%(1000000000+7)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s577485755
p04051
Wrong Answer
n = int(input()) arr = [map(int,raw_input().split()) for i in range(n)] count = 0 for i in range(n-1): for j in range(i+1,n): s = sum(arr[i]) + sum(arr[j]) comb = s*(s-1)/2 count += comb print count
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s620955904
p04051
Wrong Answer
n = int(input()) arr = [[map(int,raw_input().split())] for i in range(n)] count = 0 for i in range(n): for j in range(i+1,n): if i != j : s = sum(arr[i][0]) + sum(arr[j][0]) comb = s*(s-1)/2 count += comb print count
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s841108737
p04051
Wrong Answer
import math #--- define function ___# def counter(x, y): return math.factorial(x + y) // (math.factorial(x) * math.factorial(y)) #--- main ___# N = int(input()) st = [] for i in range(N): a, b = input().split() st.append([int(a), int(b)]) count = 0 for i in range(N): for j in range(i + 1,N): l = [a + b for (a, b) in zip(st[i], st[j])] ans = counter(l[0], l[1]) count += ans print(count)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s581614051
p04051
Wrong Answer
ans=0 N=int(input()) kushi=[] for s in range(N): kushi.append(input()) for s in range(N): kushi1="".join(kushi[s]).rstrip("\n").split(" ") for j in range(N-s): if j==0: continue kushi2="".join(kushi[s+j]).rstrip("\n").split(" ") n=int(kushi1[0])+int(kushi2[0])+int(kushi1[1])+int(kushi2[1]) k=int(kushi1[1])+int(kushi2[1]) nn=n kk=k for m in range(k-1): nn=nn*(n-1) kk=kk*(k-1) n-=1 k-=1 ans+=nn/kk print(int(ans))
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s926826185
p04051
Wrong Answer
ans=0 N=int(input()) kushi=[] for s in range(N): kushi.append(input()) for s in range(N): kushi1="".join(kushi[s]).rstrip("\n").split(" ") for j in range(N-s): if j==0: continue kushi2="".join(kushi[s+j]).rstrip("\n").split(" ") n=int(kushi1[0])+int(kushi2[0])+int(kushi1[1])+int(kushi2[1]) k=int(kushi1[1])+int(kushi2[1]) for m in range(k-1): n=n*(n-1) k=k*(k-1) ans+=n/k print(ans)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s494423384
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(MAX+1): for i in xrange(MAX+1): dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print ((ans - dup) / 2 + p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s668150351
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+A_MAX+1): dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print ((ans - dup) / 2 + p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s322726026
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+B_MAX+1): dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print ((ans - dup) / 2 + p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s205364448
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+B_MAX+1): dp[j+1][i] += dp[j][i] dp[j][i+1] += dp[j][i] ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print ((ans - dup) / 2 + p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s255848007
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 10 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+B_MAX+1): dp[j+1][i] += dp[j][i] dp[j][i+1] += dp[j][i] ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print ((ans - dup) / 2 + p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s799491390
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 p = 10**9+7 F = [1]; Finv = [0]*(Base*4+1) for i in xrange(Base*4): F.append(F[i]*(i+1)%p) Finv[Base*4] = pow(F[Base*4],p-2,p) for i in xrange(Base*4,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(Base*2+2) for i in xrange(Base*2+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2])%p goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+A_MAX+1): dp[j+1][i] =(dp[j+1][i]+dp[j][i])%p dp[j][i+1] =(dp[j][i+1]+dp[j][i])%p ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print (ans - dup) / 2 % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s914842946
p04051
Wrong Answer
from collections import defaultdict def solve(): Base = 2000 p = 10**9+7 F = [1]; Finv = [0]*(Base*4+1) for i in xrange(Base*4): F.append(F[i]*(i+1)) Finv[Base*4] = pow(F[Base*4],p-2,p) for i in xrange(Base*4,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(Base*2+2) for i in xrange(Base*2+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2])%p goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+A_MAX+1): dp[j+1][i] += dp[j][i] dp[j][i+1] += dp[j][i] ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print (ans - dup) / 2 % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s606876553
p04051
Wrong Answer
def combi(p,q): m=10**9+7 bunsi=1 bunbo=1 for i in range(q): bunsi *= (p-i)%m bunbo *= (q-i)%m bunsi = bunsi%m bunbo = bunbo%m return bunsi//bunbo % m ans=0 n=int(input()) k=[] for i in range(n): a,b=map(int,input().split()) k.append([a,b]) for i in range(n-1): for j in range(i+1,n): p=k[i][0]+k[i][1]+k[j][0]+k[j][1] q=k[i][0]+k[j][0] ans += combi(p,q) ans = ans % (10**9+7) print(ans)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s094292724
p04051
Wrong Answer
N = int(raw_input()) L = [] for i in xrange(N): l = raw_input() L.append(map(int, l.split(" "))) result = 0 for j in xrange(N): M = range(N) del M[0:j + 1] for k in M: niku = (L[j][0] + L[j][1]) yasai = (L[k][0] + L[k][1]) niku_tmp = niku food = niku + yasai p = 1 q = 1 for o in xrange(niku): p *= food q *= niku food -= 1 niku_tmp -= 1 r = (p % (10**9 + 7)) / (q % (10**9 + 7)) result += r print(result * 2) % (10**9 + 7)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s864574613
p04051
Wrong Answer
import scipy.misc as scm def combi(p,q): return scm.comb(p,q,1) ans=0 n=int(input()) k=[] for i in range(n): a,b=map(int,input().split()) k.append([a,b]) for i in range(n-1): for j in range(1,n): p=k[i][0]+k[i][1]+k[j][0]+k[j][1] q=k[i][0]+k[j][0] ans += combi(p,q) % (10**9+7) print(ans)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s721193811
p04051
Wrong Answer
N = int(input()) L = [] for x in range(N): L.append(list(map(int,input().split()))) val = 0 for i in range(len(L)-1): for j in range(i+1,len(L)): vvv = (L[i][0] + L[j][0] + L[i][1] + L[j][1]) val = val + int(((vvv * (vvv-1))/2)) print(val)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s843924782
p04051
Wrong Answer
N = int(raw_input()) L = [] for i in xrange(N): l = raw_input() L.append(map(int, l.split(" "))) result = 0 for j in xrange(N): M = range(N) del M[0:j + 1] for k in M: niku = (L[j][0] + L[j][1]) yasai = (L[k][0] + L[k][1]) niku_tmp = niku food = niku + yasai p = 1 q = 1 for o in xrange(niku): p *= food q *= niku food -= 1 niku_tmp -= 1 r = (p % (10**9 + 7)) / (q % (10**9 + 7)) result += r print(result * 2) % (10**9 + 7)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s425879688
p04051
Wrong Answer
import math set_num = int(raw_input()) bbqsets = [] _append = bbqsets.append for _ in xrange(set_num): _append([ int(x) for x in raw_input().split() ]) maxnum = math.pow(10, 9) + 7 cache = {} def get_combinations(meats, veges): nums = sorted([meats, veges]) key = '{}:{}'.format(nums[0], nums[1]) if key in cache: return cache[key] allnum = meats + veges greater = nums[1] result = 1.0 for n in xrange(1, nums[0] + 1): t = greater + n result *= float(t) / n # print key, result result = round(result) cache[key] = result return result total = 0 for x in xrange(set_num): setA = bbqsets[x] for y in xrange(x + 1, set_num): setB = bbqsets[y] meats = setA[0] + setB[0] veges = setA[1] + setB[1] # print meats, veges combinations = get_combinations(meats, veges) total += combinations if total > maxnum: total = total % maxnum print total
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s194798494
p04051
Time Limit Exceeded
import sys from collections import defaultdict from itertools import accumulate def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs n = int(sys.stdin.buffer.readline()) mp = map(int, sys.stdin.buffer.read().split()) ab = list(zip(mp, mp)) atob = defaultdict(list) max_a, max_b = 0, 0 for a, b in ab: atob[a].append(b) max_a = max(max_a, a) max_b = max(max_b, b) MOD = 10 ** 9 + 7 dp = [0] * (max_b * 2 + 1) ans = 0 for i in range(-max_a, max_a + 1): if i < 0 and -i in atob: for j in atob[-i]: dp[-j + max_b] += 1 dp = [x % MOD for x in accumulate(dp)] if i > 0 and i in atob: for j in atob[i]: ans = (ans + dp[j + max_b]) % MOD facts, invs = prepare(2 * (max_a + max_b), MOD) ans = int(ans) for a, b in ab: ans = (ans - facts[2 * (a + b)] * invs[2 * a] * invs[2 * b]) % MOD print(ans * invs[2] % MOD)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s385245666
p04051
Time Limit Exceeded
N = int(input()) mod = int(1e+9 + 7) def extgcd(a, b): x, y, u, v, k, l = 1, 0, 0, 1, a, b while l != 0: x, y, u, v = u, v, x - u * (k // l), y - v * (k // l) k, l = l, k % l return x, y def inved(x): a, b = extgcd(x, mod) return a % mod frac = [1] for i in range(8000): frac.append((frac[i]*(i+1))%mod) invf = [] for i in range(8001): invf.append(inved(frac[i])) A = [list(map(int, input().split())) for i in range(N)] S = 0 for i in range(N): for j in range(i): F = frac[A[i][0] + A[j][0] + A[i][1] + A[j][1]] * invf[A[i][0] + A[j][0]] F %= mod F *= invf[A[i][1] + A[j][1]] F %= mod S += F S %= mod print(S)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s712033068
p04051
Time Limit Exceeded
U = 10**6 MOD = 10**9+7 fact = [1]*(U+1) fact_inv = [1]*(U+1) for i in range(1,U+1): fact[i] = (fact[i-1]*i)%MOD fact_inv[U] = pow(fact[U],MOD-2,MOD) for i in range(U,0,-1): fact_inv[i-1] = (fact_inv[i]*i)%MOD def comb(n,k): if k < 0 or k > n: return 0 z = fact[n] z *= fact_inv[k] z %= MOD z *= fact_inv[n-k] z %= MOD return z n = int(input()) ab = [ list(map(int, input().split())) for _ in range(n) ] ans = 0 for i in range(n-1): for j in range(i+1, n): ai, bi = ab[i] aj, bj = ab[j] ans += comb(ai+bi+aj+bj, ai+aj) print(ans%(10**9+7))
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s413675452
p04051
Time Limit Exceeded
def proc(n): res = 1 for i in range(1,n+1): res *= i return res def co(a, b): res = proc(a) // proc(a-b) // proc(b) return res n = int(input()) a = [0 for i in range(n)] b = [0 for i in range(n)] for ni in range(n): a[ni], b[ni] = (int(i) for i in input().split(" ")) res = 0 for i in range(n): for j in range(i+1,n): res += co(a[i]+a[j]+b[i]+b[j], b[i]+b[j]) print(res % (10**9+7))
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s973525278
p04051
Time Limit Exceeded
ans=0 N=int(input()) kushi=[] def nCk(n,k): N,K=n-1,k-1 tempN=n-1 tempK=k-1 for s in range(k-2): N=N*(tempN-1) K=K*(tempK-1) tempN-=1 tempK-=1 first=N//K N,K=n-1,k tempN=n-1 for s in range(k-1): N=N*(tempN-1) K=K*(k-1) tempN-=1 k-=1 second=N//K return int(first+second) for s in range(N): kushi.append(input()) for s in range(N): kushi1="".join(kushi[s]).rstrip("\n").split(" ") for j in range(N-s): if j==0: continue kushi2="".join(kushi[s+j]).rstrip("\n").split(" ") n=int(kushi1[0])+int(kushi2[0])+int(kushi1[1])+int(kushi2[1]) k=int(kushi1[1])+int(kushi2[1]) nn=n kk=k ans+=nCk(nn,kk) m=1000000007 print(int(ans)%m)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s896267625
p04051
Time Limit Exceeded
ans=0 N=int(input()) kushi=[] for s in range(N): kushi.append(input()) for s in range(N): kushi1="".join(kushi[s]).rstrip("\n").split(" ") for j in range(N-s): if j==0: continue kushi2="".join(kushi[s+j]).rstrip("\n").split(" ") n=int(kushi1[0])+int(kushi2[0])+int(kushi1[1])+int(kushi2[1]) k=int(kushi1[1])+int(kushi2[1]) nn=n kk=k for m in range(k-1): nn=nn*(n-1) kk=kk*(k-1) n-=1 k-=1 ans+=nn//kk m=1000000007 print(int(ans)%m)
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s310335791
p04051
Time Limit Exceeded
ans=0 N=int(input()) kushi=[] for s in range(N): kushi.append(input()) for s in range(N): kushi1="".join(kushi[s]).rstrip("\n").split(" ") for j in range(N-s): if j==0: continue kushi2="".join(kushi[s+j]).rstrip("\n").split(" ") n=int(kushi1[0])+int(kushi2[0])+int(kushi1[1])+int(kushi2[1]) k=int(kushi1[1])+int(kushi2[1]) nn=n kk=k for m in range(k-1): nn=nn*(n-1) kk=kk*(k-1) n-=1 k-=1 ans+=nn/kk print(int(ans))
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s977454608
p04051
Time Limit Exceeded
# Solution to: # http://agc001.contest.atcoder.jp/tasks/agc001_e import numpy as np from scipy import signal N = int(raw_input()) A = [] B = [] MAX_SIZE = 2000 MOD = 10**9+7 # Preompute choose table # CT = [[1], [1, 1]] # for a in xrange(2, MAX_SIZE*4+2): # CT.append([1]) # for b in xrange(1, a): # CT[a].append(CT[a-1][b-1] + CT[a-1][b]) # CT[a].append(1) # print "Precomputation done!" # Precompute factorials fact = [1] fact_inv = [1] for x in xrange(1, MAX_SIZE*2+5): fact.append((fact[x-1] * x) % MOD) fact_inv.append(pow(fact[x], (MOD-2), MOD)) # print fact, fact_inv # assert (fact[-1] * fact_inv[-1]) % MOD == 1, 'GG' mat = np.zeros([MAX_SIZE+1, MAX_SIZE+1]) for n in xrange(N): a, b = map(int, raw_input().split()) A.append(a) B.append(b) mat[a, b] += 1 mixed = signal.fftconvolve(mat, mat) for n in xrange(N): mixed[2*A[n], 2*B[n]] -= 1 mixed /= 2 mixed = np.rint(mixed) # print mixed[0:5, 0:5] tsum = 0 for t in xrange(MAX_SIZE*2+2): for a in xrange(t+1): b = t-a if a > MAX_SIZE+1: continue if b > MAX_SIZE+1: continue # print a,b plus = (fact[t] * fact_inv[a]) % MOD plus = (plus * fact_inv[b]) % MOD # plus = (plus * (t+1)) % MOD plus = (plus * int(mixed[a][b])) % MOD # if plus > 0: print a,b,plus tsum = (tsum + plus) % MOD print tsum
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s255547495
p04051
Time Limit Exceeded
import math set_num = int(raw_input()) bbqsets = [] _append = bbqsets.append for _ in xrange(set_num): _append([ int(x) for x in raw_input().split() ]) maxnum = int(math.pow(10, 9) + 7) fact = [0] * 8001 rev = [0] * 8001 for i in xrange(0, 8001): if i == 0: fact[0] = 1 rev[0] = 1 continue fact[i] = fact[i-1] * i rev[i] = pow(fact[i], maxnum - 2, maxnum) cache = {} total = 0 for x in xrange(set_num): setA = bbqsets[x] for y in xrange(x + 1, set_num): setB = bbqsets[y] nums = tuple(sorted([setA[0] + setB[0], setA[1] + setB[1]])) cmb = 0 if nums in cache: cmb = cache[nums] else: cmb = fact[nums[0] + nums[1]] * rev[nums[0]] * rev[nums[1]] cache[nums] = cmb # print meats, veges total = (total + cmb) % maxnum print total
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s752508606
p04051
Time Limit Exceeded
import math set_num = int(raw_input()) bbqsets = [] _append = bbqsets.append for _ in xrange(set_num): _append([ int(x) for x in raw_input().split() ]) maxnum = int(math.pow(10, 9) + 7) fact = [0] * 4001 rev = [0] * 4001 for i in xrange(0, 4001): if i == 0: fact[0] = 1 rev[0] = 1 continue fact[i] = fact[i-1] * i rev[i] = pow(fact[i], maxnum - 2, maxnum) cache = {} total = 0 for x in xrange(set_num): setA = bbqsets[x] for y in xrange(x + 1, set_num): setB = bbqsets[y] nums = tuple(sorted([setA[0] + setB[0], setA[1] + setB[1]])) cmb = 0 if nums in cache: cmb = cache[nums] else: cmb = fact[nums[0] + nums[1]] * rev[nums[0]] * rev[nums[1]] cache[nums] = cmb # print meats, veges total = (total + cmb) % maxnum print total
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s394431474
p04051
Time Limit Exceeded
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX*2+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(A,B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+A_MAX+1): dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p ans = 0 for (A,B),v in goals.iteritems(): ans = (ans+(dp[Base+B][Base+A]-F[A*2+B*2]*Finv[A*2]*Finv[B*2]%p)*v) % p print (ans + p)*pow(2,p-2,p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
s438313939
p04051
Time Limit Exceeded
from collections import defaultdict def solve(): Base = 2000 MAX = Base*2 p = 10**9+7 F = [1]; Finv = [0]*(MAX*2+1) for i in xrange(MAX*2): F.append(F[i]*(i+1)%p) Finv[MAX] = pow(F[MAX],p-2,p) for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p N = int(raw_input()) dup = A_MAX = B_MAX = 0 dp = [[0]*(MAX+2) for i in xrange(MAX+2)] goals = defaultdict(int) for i in xrange(N): A,B = map(int,raw_input().split()) dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2])%p if A > A_MAX: A_MAX = A if B > B_MAX: B_MAX = B goals[(Base+A,Base+B)] += 1 dp[Base-B][Base-A] += 1 for j in xrange(Base-B_MAX,Base+B_MAX+1): for i in xrange(Base-A_MAX,Base+A_MAX+1): dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p ans = 0 for (i,j),v in goals.iteritems(): ans = (ans + dp[j][i]*v) % p print (ans - dup + p)*pow(2,p-2,p) % p solve()
3 1 1 1 1 2 1
26
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>