message
stringlengths
2
67k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
463
109k
cluster
float64
19
19
__index_level_0__
int64
926
217k
Provide a correct Python 3 solution for this coding contest problem. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617
instruction
0
87,437
19
174,874
"Correct Solution: ``` while 1: b,r,g,c,s,t=map(int,input().split()) if [b,r,g,c,s,t].count(0)==6:break print(100+(b+r)*15+g*7+c*2+(b*5+r*3)*13-(t-(s+b*5+r*3))*3) ```
output
1
87,437
19
174,875
Provide a correct Python 3 solution for this coding contest problem. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617
instruction
0
87,438
19
174,876
"Correct Solution: ``` while 1: b,r,g,c,s,t = list(map(int,input().split())) if t == 0:break cnt = b * 5 + r * 3 + s #コイン不使用とするゲーム数 coins = (b * 5 + r * 3) * (15-2) coins += b * 15 coins += r * 15 coins += 7 * g coins += 2 * c coins += 100 - (t - cnt) * 3 print (coins) ```
output
1
87,438
19
174,877
Provide a correct Python 3 solution for this coding contest problem. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617
instruction
0
87,439
19
174,878
"Correct Solution: ``` while True: b,r,g,c,s,t = map(int,input().split(" ")) if b == r == g == c == s == t == 0: break print(100+(b+r)*15+g*7+c*2+(b*5+r*3)*13-(t-(s+b*5+r*3))*3) ```
output
1
87,439
19
174,879
Provide a correct Python 3 solution for this coding contest problem. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617
instruction
0
87,440
19
174,880
"Correct Solution: ``` while True: b,r,g,c,s,t=map(int, input().split()) if t==0: break x=100 y=b*5+r*3 z=t-y-s a=x+(b+r)*15+g*7+y*13+c*2-z*3 print(a) ```
output
1
87,440
19
174,881
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617 Submitted Solution: ``` import sys f = sys.stdin while True: seven, bar, grape, cherry, star, all_game = map(int, f.readline().split()) if seven == bar == grape == cherry == star == all_game == 0: break init_coin = 100 free_game = star bonus_game = seven * 5 + bar * 3 nomal_game = all_game - free_game - bonus_game last_coin = init_coin - bonus_game * 2 - nomal_game * 3 + (bonus_game + seven + bar) * 15 + grape * 7 + cherry * 2 print(last_coin) ```
instruction
0
87,441
19
174,882
Yes
output
1
87,441
19
174,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine. When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols. <image> A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games. If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals. During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals. Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left? Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t. Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format: b r g c s t b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000. The number of datasets does not exceed 120. Output For each input dataset, the number of medals remaining at hand is output on one line. Example Input 3 2 30 3 26 226 9 0 18 3 20 118 5 5 12 2 15 203 7 4 19 2 22 197 7 4 24 4 17 209 0 0 0 0 0 0 Output 127 793 414 629 617 Submitted Solution: ``` # AOJ 0229 Big Hit ! # Python3 2018.6.25 bal4u while 1: b, r, g, c, s, t = map(int, input().split()) if (b|r|g|c|s|t) == 0: break ans = 100 ans += 15*b+(15-2)*5*b t -= 5*b ans += 15*r+(15-2)*3*r t -= 3*r ans += 7*g ans += 2*c t -= s ans -= 3*t print(ans); ```
instruction
0
87,442
19
174,884
Yes
output
1
87,442
19
174,885
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,451
19
174,902
"Correct Solution: ``` from collections import deque class HopcroftKarp: def __init__(self, N0, N1): self.N0 = N0 self.N1 = N1 self.N = N = 2+N0+N1 self.G = [[] for i in range(N)] for i in range(N0): forward = [2+i, 1, None] forward[2] = backward = [0, 0, forward] self.G[0].append(forward) self.G[2+i].append(backward) self.backwards = bs = [] for i in range(N1): forward = [1, 1, None] forward[2] = backward = [2+N0+i, 0, forward] bs.append(backward) self.G[2+N0+i].append(forward) self.G[1].append(backward) def add_edge(self, fr, to): #assert 0 <= fr < self.N0 #assert 0 <= to < self.N1 v0 = 2 + fr v1 = 2 + self.N0 + to forward = [v1, 1, None] forward[2] = backward = [v0, 0, forward] self.G[v0].append(forward) self.G[v1].append(backward) def bfs(self): G = self.G level = [None]*self.N deq = deque([0]) level[0] = 0 while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) self.level = level return level[1] is not None def dfs(self, v, t): if v == t: return 1 level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w] and self.dfs(w, t): e[1] = 0 rev[1] = 1 return 1 return 0 def flow(self): flow = 0 G = self.G bfs = self.bfs; dfs = self.dfs while bfs(): *self.it, = map(iter, G) while dfs(0, 1): flow += 1 return flow def matching(self): return [cap for _, cap, _ in self.backwards] def gcd(m, n): r = m % n return gcd(n, r) if r else n while 1: M, N = map(int, input().split()) if M == N == 0: break B = [] while len(B) < M: B.extend(map(int, input().split())) R = [] while len(R) < N: R.extend(map(int, input().split())) hk = HopcroftKarp(M, N) for i in range(M): for j in range(N): if gcd(B[i], R[j]) > 1: hk.add_edge(i, j) print(hk.flow()) ```
output
1
87,451
19
174,903
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,452
19
174,904
"Correct Solution: ``` from collections import deque class Dinic(object): __slots__ = ["inf", "v_count", "edges", "iter", "level"] def __init__(self, v_count: int, edges: list): self.inf = 10**9 self.v_count = v_count self.edges = [[] for _ in [0]*v_count] self.iter = [0]*v_count self.level = None self._create_graph(edges) def _create_graph(self, _edges): edges = self.edges for origin, dest, cap in _edges: edges[origin].append([dest, cap, len(edges[dest])]) edges[dest].append([origin, 0, len(edges[origin])-1]) def solve(self, source: int, sink: int): max_flow = 0 while True: self.bfs(source) if self.level[sink] < 0: return max_flow self.iter = [0]*self.v_count flow = self.dfs(source, sink, self.inf) while flow > 0: max_flow += flow flow = self.dfs(source, sink, self.inf) def bfs(self, source: int): level, edges = [-1]*self.v_count, self.edges level[source] = 0 dq = deque([source]) popleft, append = dq.popleft, dq.append while dq: v = popleft() for dest, cap, _rev in edges[v]: if cap > 0 > level[dest]: level[dest] = level[v] + 1 append(dest) self.level = level def dfs(self, source: int, sink: int, flow: int) -> int: if source == sink: return flow while self.iter[source] < len(self.edges[source]): dest, cap, rev = edge = self.edges[source][self.iter[source]] if cap > 0 and self.level[source] < self.level[dest]: flowed = self.dfs(dest, sink, flow if flow < cap else cap) if flowed > 0: edge[1] -= flowed self.edges[dest][rev][1] += flowed return flowed self.iter[source] += 1 return 0 def get_prime_set(ub): from itertools import chain from math import sqrt if ub < 4: return ({}, {}, {2}, {2, 3})[ub] ub, ub_sqrt = ub+1, int(sqrt(ub))+1 primes = {2, 3} | set(chain(range(5, ub, 6), range(7, ub, 6))) du = primes.difference_update for n in chain(range(5, ub_sqrt, 6), range(7, ub_sqrt, 6)): if n in primes: du(range(n*3, ub, n*2)) return primes if __name__ == "__main__": from math import sqrt from collections import defaultdict primes = get_prime_set(int(sqrt(10**7))+1) answer = [] append_answer = answer.append while True: M, N = map(int, input().split()) if not M*N: break source, sink = M+N, M+N+1 edges, blue, red = [], [], [] for i in range(M): edges.append((i, sink, 1)) for i in range(M, M+N): edges.append((source, i, 1)) divisors = defaultdict(set) index = 0 # blue while index < M: for num in map(int, input().split()): for p in filter(lambda x: num % x == 0, primes): divisors[p].add(index) while num % p == 0: num //= p if num > 1: divisors[num].add(index) index += 1 # red while index < M+N: for num in map(int, input().split()): neighbors = set() update = neighbors.update for p in filter(lambda x: num % x == 0, primes): update(divisors[p]) while num % p == 0: num //= p if num > 1: update(divisors[num]) for dest in neighbors: edges.append((index, dest, 1)) index += 1 dinic = Dinic(sink+1, edges) append_answer(dinic.solve(source, sink)) print(*answer, sep="\n") ```
output
1
87,452
19
174,905
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,453
19
174,906
"Correct Solution: ``` import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) def gcd(x, y): if y == 0: return x return gcd(y, x % y) class BipartiteMatching: def __init__(self, N, M): self.N = N self.M = M self.pairA = [-1] * N self.pairB = [-1] * M self.edge = [[] for _ in range(N)] self.was = [0] * N self.iter = 0 def add(self, s, t): self.edge[s].append(t) def _DFS(self, s): self.was[s] = self.iter for t in self.edge[s]: if self.pairB[t] == -1: self.pairA[s] = t self.pairB[t] = s return True for t in self.edge[s]: if self.was[self.pairB[t]] != self.iter and self._DFS(self.pairB[t]): self.pairA[s] = t self.pairB[t] = s return True return False def solve(self): res = 0 while True: self.iter += 1 found = 0 for i in range(self.N): if self.pairA[i] == -1 and self._DFS(i): found += 1 if not found: break res += found return res def inp(num): it = (num + 9) // 10 res = [] for _ in range(it): res.extend(list(map(int, input().split()))) return res if __name__ == "__main__": ans = [] while True: M, N = map(int, input().split()) if M == 0 and N == 0: break match = BipartiteMatching(M, N) Blue = inp(M) Red = inp(N) for i, b in enumerate(Blue): for j, r in enumerate(Red): if gcd(b, r) != 1: match.add(i, j) ans.append(match.solve()) print(*ans, sep="\n") ```
output
1
87,453
19
174,907
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,454
19
174,908
"Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- from fractions import gcd class Dinic: class Edge: def __init__(self, to, cap, rev): self.to = to self.cap = cap self.rev = rev def __repr__(self): return "(to: {0} cap: {1} rev: {2})".format(self.to, self.cap, self. rev) def __init__(self,V): self.V = V self.size = [0 for i in range(V)] self.G = [[] for i in range(V)] def add_edge(self, _from, to, cap): self.G[_from].append(self.Edge(to, cap, self.size[to])) self.G[to].append(self.Edge(_from, 0, self.size[_from])) self.size[_from] += 1 self.size[to] += 1 def bfs(self,s): level = [-1 for i in range(self.V)] level[s] = 0 q = [] q.append(s) while q != []: v = q.pop(0) for u in self.G[v]: if u.cap > 0 and level[u.to] < 0: level[u.to] = level[v] + 1 q.append(u.to) return level def dfs(self, v, t, f): if v == t: return f for i in range(self.iterator[v],self.size[v]): self.iterator[v] = i edge = self.G[v][i] if edge.cap > 0 and self.level[v] < self.level[edge.to]: d = self.dfs(edge.to, t, f if f < edge.cap else edge.cap) if d > 0: self.G[v][i].cap -= d self.G[edge.to][edge.rev].cap += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.level = self.bfs(s) if self.level[t] < 0: return flow self.iterator = [0 for i in range(self.V)] while True: f = self.dfs(s, t, float('inf')) if f == 0: break flow += f while True: m,n = map(int,input().split()) if (m,n) == (0,0): break B = [] R = [] while True: for x in map(int, input().split()): B.append(x) if len(B) ==m: break while True: for x in map(int, input().split()): R.append(x) if len(R) == n: break dinic = Dinic(2 + m + n) _b = 1 _r = 1 + m for b_idx, b in enumerate(B): for r_idx, r in enumerate(R): if gcd(b,r) > 1: dinic.add_edge(_b + b_idx, _r + r_idx, 1) dinic.add_edge(0, b_idx + _b, 1) for r_idx, r in enumerate(R): dinic.add_edge(r_idx + _r, 1 + m + n, 1) dinic.max_flow(0, 1+m+n) ans = 0 for rev in dinic.G[1+m+n]: ans += rev.cap print(ans) ```
output
1
87,454
19
174,909
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,455
19
174,910
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class Flow(): def __init__(self, e, N): self.E = e self.N = N self.nl = list(range(N)) def max_flow(self, s, t): r = 0 e = self.E v = None def f(c): v[c] = 1 if c == t: return 1 for i in e[c]: if v[i] is None and f(i): e[c].remove(i) e[i].add(c) return 1 return while True: v = [None] * self.N if f(s) is None: break r += 1 return r def main(): rr = [] def f(m, n): b = LI() while len(b) < m: b += LI() r = LI() while len(r) < n: r += LI() s = m + n + 2 e = collections.defaultdict(set) for i in range(m): e[0].add(i+1) for i in range(n): e[m+i+1].add(s-1) for i in range(m): for j in range(n): if fractions.gcd(b[i], r[j]) > 1: e[i+1].add(m+j+1) fl = Flow(e, s) return fl.max_flow(0, s-1) while True: m, n = LI() if m == 0 and n == 0: break rr.append(f(m,n)) return '\n'.join(map(str, rr)) print(main()) ```
output
1
87,455
19
174,911
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,456
19
174,912
"Correct Solution: ``` import collections import fractions class Dinic: """Dinic Algorithm: find max-flow complexity: O(EV^2) used in GRL6A(AOJ) """ class edge: def __init__(self, to, cap, rev): self.to, self.cap, self.rev = to, cap, rev def __init__(self, V, E, source, sink): """ V: the number of vertexes E: adjacency list source: start point sink: goal point """ self.V = V self.E = [[] for _ in range(V)] for fr in range(V): for to, cap in E[fr]: self.E[fr].append(self.edge(to, cap, len(self.E[to]))) self.E[to].append(self.edge(fr, 0, len(self.E[fr])-1)) self.maxflow = self.dinic(source, sink) def dinic(self, source, sink): """find max-flow""" INF = float('inf') maxflow = 0 while True: self.bfs(source) if self.level[sink] < 0: return maxflow self.itr = [0] * self.V while True: flow = self.dfs(source, sink, INF) if flow > 0: maxflow += flow else: break def dfs(self, vertex, sink, flow): """find augmenting path""" if vertex == sink: return flow for i in range(self.itr[vertex], len(self.E[vertex])): self.itr[vertex] = i e = self.E[vertex][i] if e.cap > 0 and self.level[vertex] < self.level[e.to]: d = self.dfs(e.to, sink, min(flow, e.cap)) if d > 0: e.cap -= d self.E[e.to][e.rev].cap += d return d return 0 def bfs(self, start): """find shortest path from start""" que = collections.deque() self.level = [-1] * self.V que.append(start) self.level[start] = 0 while que: fr = que.popleft() for e in self.E[fr]: if e.cap > 0 and self.level[e.to] < 0: self.level[e.to] = self.level[fr] + 1 que.append(e.to) while True: M, N = map(int, input().split()) if M == 0 and N == 0: break blue, red = [], [] while True: for x in input().split(): blue.append(int(x)) if len(blue) == M: break while True: for x in input().split(): red.append(int(x)) if len(red) == N: break V = M + N + 2 edge = [[] for _ in range(V)] for i, b in enumerate(blue): for j, r in enumerate(red): if fractions.gcd(b, r) != 1: edge[i].append((M+j, 1)) for i in range(M): edge[M+N].append((i, 1)) for j in range(N): edge[M+j].append((M+N+1, 1)) print(Dinic(V, edge, M+N, M+N+1).maxflow) ```
output
1
87,456
19
174,913
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,457
19
174,914
"Correct Solution: ``` #ARC092-C 2D Plane 2N Points """ 重みなし2部マッチング問題 """ import sys readline = sys.stdin.buffer.readline def even(n): return 1 if n%2==0 else 0 #ネットワークフロー #最大流,最小カット import queue class Dinic(): #Dinic法,O(|E||V|**2) ※但し、実際にはもっと高速な動作 def __init__(self, v, inf = 10**10): # v:頂点数 # G:辺情報.各頂点に対し、[行き先,重み,(この辺を含めず)既に存在する行き先の辺数] # level:startからの距離(これはcapacityを考慮しない) bfsで毎回リセットされる # iter:各頂点について、どこまで調べ終わったかを記録する self.V = v self.inf = inf self.G = [[] for _ in range(v)] self.level = [0 for _ in range(v)] self.iter = [0 for _ in range(v)] def add_edge(self, from_, to, cap): # to: 行き先, cap: 容量, rev: 反対側の辺 # 無向グラフの場合、G[to]の方のcapを0→capにする必要があるので注意.('rev'はそのままで良い) self.G[from_].append({'to':to, 'cap':cap, 'rev':len(self.G[to])}) self.G[to].append({'to':from_, 'cap':0, 'rev':len(self.G[from_])-1}) # sからの最短距離をbfsで計算 def bfs(self, s): self.level = [-1 for _ in range(self.V)] self.level[s] = 0 que = queue.Queue() que.put(s) while not que.empty(): v = que.get() for i in range(len(self.G[v])): e = self.G[v][i] if e['cap'] > 0 and self.level[e['to']] < 0: self.level[e['to']] = self.level[v] + 1 que.put(e['to']) # 増加パスをdfsで探す def dfs(self, v, t, f): if v == t: return f for i in range(self.iter[v], len(self.G[v])): self.iter[v] = i e = self.G[v][i] if e['cap'] > 0 and self.level[v] < self.level[e['to']]: #流れているかつ、levelが大きいなら d = self.dfs(e['to'], t, min(f, e['cap'])) # d:流量 if d > 0: e['cap'] -= d #使用済みの分だけcapから引く self.G[e['to']][e['rev']]['cap'] += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) #levelの更新 # bfsでtに到達不可なら終了 if self.level[t] < 0 : return flow #イテレータの初期化(イテレータ:各頂点に対し、どこまで調べ終わったか) self.iter = [0 for _ in range(self.V)] f = self.dfs(s, t, self.inf) #f:そのパスの流量 while f > 0: flow += f f = self.dfs(s,t, self.inf) """素因数分解""" def factrize(n): b = 2 fct = [] while b*b <= n: while n % b == 0: n //= b #もし素因数を重複させたくないならここを加えてfct.append(b)を消す if not b in fct: fct.append(b) b = b+1 if n > 1: fct.append(n) return fct #リストが帰る def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) def lcm(a,b): return (a//gcd(a,b)*b) while True: m,n = map(int,readline().split()) if m == 0 and n == 0: break network = Dinic(m+n+2) source = 0 sink = m+n+1 blue = [] red = [] while len(blue) < m: blue += list(map(int,readline().split())) while len(red) < n: red += list(map(int,readline().split())) for i in range(m): for j in range(n): if gcd(blue[i],red[j]) > 1: network.add_edge(i+1,m+j+1,1) for i in range(m): network.add_edge(source,i+1,1) for i in range(n): network.add_edge(m+i+1,sink,1) print(network.max_flow(source,sink)) ```
output
1
87,457
19
174,915
Provide a correct Python 3 solution for this coding contest problem. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85
instruction
0
87,458
19
174,916
"Correct Solution: ``` # -*- coding: utf-8 -*- import sys from fractions import gcd def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 class BipartiteMatching: """ XとYの二部グラフの最大マッチング X={0,1,2,...|X|-1} Y={0,1,2,...,|Y|-1} edges[x]: xとつながるYの頂点のset match1[x]: xとマッチングされたYの頂点 match2[y]: yとマッチングされたXの頂点 """ def __init__(self, n, m): self.n = n self.m = m self.edges = [set() for _ in range(n)] self.match1 = [-1] * n self.match2 = [-1] * m def dfs(self, v, visited): """ :param v: X側の未マッチングの頂点の1つ :param visited: 空のsetを渡す(外部からの呼び出し時) :return: 増大路が見つかればTrue """ for u in self.edges[v]: if u in visited: continue visited.add(u) if self.match2[u] == -1 or self.dfs(self.match2[u], visited): self.match2[u] = v self.match1[v] = u return True return False def add(self, a, b): self.edges[a].add(b) def whois1(self, a): """ :param: グループ1の頂点 :return: ペアになるグループ2の頂点 """ return self.match1[a] def whois2(self, a): """ :param: グループ2の頂点 :return: ペアになるグループ1の頂点 """ return self.match2[a] def solve(self): # 増大路発見に成功したらTrue(=1)。合計することでマッチング数となる return sum(self.dfs(i, set()) for i in range(self.n)) while True: N, M = MAP() if N == M == 0: break A = [] B = [] for _ in range(ceil(N, 10)): A += LIST() for _ in range(ceil(M, 10)): B += LIST() bm = BipartiteMatching(N, M) for i, a in enumerate(A): for j, b in enumerate(B): if gcd(a, b) != 1: bm.add(i, j) ans = bm.solve() print(ans) ```
output
1
87,458
19
174,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from collections import deque INF = float("inf") TO = 0; CAP = 1; REV = 2 class Dinic: def __init__(self, N): self.N = N self.V = [[] for _ in range(N)] # to, cap, rev # 辺 e = V[n][m] の逆辺は V[e[TO]][e[REV]] self.level = [0] * N def add_edge(self, u, v, cap): self.V[u].append([v, cap, len(self.V[v])]) self.V[v].append([u, 0, len(self.V[u])-1]) def add_edge_undirected(self, u, v, cap): # 未検証 self.V[u].append([v, cap, len(self.V[v])]) self.V[v].append([u, cap, len(self.V[u])-1]) def bfs(self, s: int) -> bool: self.level = [-1] * self.N self.level[s] = 0 q = deque() q.append(s) while len(q) != 0: v = q.popleft() for e in self.V[v]: if e[CAP] > 0 and self.level[e[TO]] == -1: # capが1以上で未探索の辺 self.level[e[TO]] = self.level[v] + 1 q.append(e[TO]) return True if self.level[self.g] != -1 else False # 到達可能 def dfs(self, v: int, f) -> int: if v == self.g: return f for i in range(self.ite[v], len(self.V[v])): self.ite[v] = i e = self.V[v][i] if e[CAP] > 0 and self.level[v] < self.level[e[TO]]: d = self.dfs(e[TO], min(f, e[CAP])) if d > 0: # 増加路 e[CAP] -= d # cap を減らす self.V[e[TO]][e[REV]][CAP] += d # 反対方向の cap を増やす return d return 0 def solve(self, s, g): self.g = g flow = 0 while self.bfs(s): # 到達可能な間 self.ite = [0] * self.N f = self.dfs(s, INF) while f > 0: flow += f f = self.dfs(s, INF) return flow from fractions import gcd while True: m, n = map(int, input().split()) if m==n==0: break B = [] while len(B)!=m: B += list(map(int, input().split())) R = [] while len(R)!=n: R += list(map(int, input().split())) dinic = Dinic(m+n+2) s, g = m+n, m+n+1 for i in range(m): dinic.add_edge(s, i, 1) for i in range(m, m+n): dinic.add_edge(i, g, 1) for i, r in enumerate(B): for j, b in enumerate(R, m): if gcd(r, b) > 1: dinic.add_edge(i, j, 1) print(dinic.solve(s, g)) ```
instruction
0
87,459
19
174,918
Yes
output
1
87,459
19
174,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` import math from collections import deque class MaxFlow: class Edge: def __init__(self,to,cap,rev): self.to = to self.cap = cap self.rev = rev def __init__(self,n,inf = 10**9+7): self.n = n self.inf = inf self.level = [-1]*n self.iter = [0]*n self.e = [[] for _ in range(n)] def add_edge(self, from_, to, cap): self.e[from_].append(self.Edge(to, cap, len(self.e[to]))) self.e[to].append(self.Edge(from_, 0, len(self.e[from_])-1)) def bfs(self, start): self.level = [-1]*self.n dq = deque() self.level[start] = 0 dq.append(start) while dq: cur_vertex = dq.popleft() for edge in self.e[cur_vertex]: if edge.cap > 0 > self.level[edge.to]: self.level[edge.to] = self.level[cur_vertex] + 1 dq.append(edge.to) def dfs(self, cur_vertex, end_vertex, flow): if cur_vertex == end_vertex:return flow while self.iter[cur_vertex] < len(self.e[cur_vertex]): edge = self.e[cur_vertex][self.iter[cur_vertex]] if edge.cap > 0 and self.level[cur_vertex] < self.level[edge.to]: flowed = self.dfs(edge.to, end_vertex, min(flow, edge.cap)) if flowed > 0: edge.cap -= flowed self.e[edge.to][edge.rev].cap += flowed return flowed self.iter[cur_vertex] += 1 return 0 def compute(self, source, sink): flow = 0 while True: self.bfs(source) if self.level[sink] < 0:return flow self.iter = [0]*self.n while True: f = self.dfs(source, sink, self.inf) if f == 0:break flow += f def main(): while True: n,m = map(int,input().split()) if n==0:break a = [] while True: c = list(map(int,input().split())) a.extend(c) if len(a)>=n:break b = [] while True: c = list(map(int,input().split())) b.extend(c) if len(b)>=m:break MF = MaxFlow(n+m+2) s = n+m t = n+m+1 for i in range(n): for j in range(m): if math.gcd(a[i],b[j])!=1: MF.add_edge(i,j+n,1) for i in range(n): MF.add_edge(s,i,1) for i in range(m): MF.add_edge(i+n,t,1) print(MF.compute(s,t)) if __name__ == '__main__': main() ```
instruction
0
87,460
19
174,920
Yes
output
1
87,460
19
174,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow def gcd(a,b): while b!=0: a,b = b,a%b return a res = [] while True: m,n = map(int, input().split()) if m==0 and n==0: break bl = [] while len(bl)<m: bl.extend(list(map(int, input().split()))) rl = [] while len(rl)<n: rl.extend(list(map(int, input().split()))) dinic = Dinic(m+n+2) s = n+m t = n+m+1 for i in range(m): for j in range(n): if gcd(bl[i],rl[j])>1: dinic.add_edge(i,m+j,1) for i in range(m): dinic.add_edge(s,i,1) for j in range(m,n+m): dinic.add_edge(j,t,1) res.append(dinic.flow(s,t)) for r in res: print(r) ```
instruction
0
87,461
19
174,922
Yes
output
1
87,461
19
174,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` import collections import math class Dinic: """Dinic Algorithm: find max-flow complexity: O(EV^2) used in GRL6A(AOJ) """ class edge: def __init__(self, to, cap, rev): self.to, self.cap, self.rev = to, cap, rev def __init__(self, V, E, source, sink): """ V: the number of vertexes E: adjacency list source: start point sink: goal point """ self.V = V self.E = [[] for _ in range(V)] for fr in range(V): for to, cap in E[fr]: self.E[fr].append(self.edge(to, cap, len(self.E[to]))) self.E[to].append(self.edge(fr, 0, len(self.E[fr])-1)) self.maxflow = self.dinic(source, sink) def dinic(self, source, sink): """find max-flow""" INF = float('inf') maxflow = 0 while True: self.bfs(source) if self.level[sink] < 0: return maxflow self.itr = [0] * self.V while True: flow = self.dfs(source, sink, INF) if flow > 0: maxflow += flow else: break def dfs(self, vertex, sink, flow): """find augmenting path""" if vertex == sink: return flow for i in range(self.itr[vertex], len(self.E[vertex])): self.itr[vertex] = i e = self.E[vertex][i] if e.cap > 0 and self.level[vertex] < self.level[e.to]: d = self.dfs(e.to, sink, min(flow, e.cap)) if d > 0: e.cap -= d self.E[e.to][e.rev].cap += d return d return 0 def bfs(self, start): """find shortest path from start""" que = collections.deque() self.level = [-1] * self.V que.append(start) self.level[start] = 0 while que: fr = que.popleft() for e in self.E[fr]: if e.cap > 0 and self.level[e.to] < 0: self.level[e.to] = self.level[fr] + 1 que.append(e.to) while True: M, N = map(int, input().split()) if M == 0 and N == 0: break blue, red = [], [] while True: for x in input().split(): blue.append(int(x)) if len(blue) == M: break while True: for x in input().split(): red.append(int(x)) if len(red) == N: break V = M + N + 2 edge = [set() for _ in range(V)] for i, b in enumerate(blue): if b != 1: for j, r in enumerate(red): if r % b == 0: edge[i].add((M+j, 1)) for j in range(2, int(math.sqrt(b)) + 1): if b % j == 0: for k, r in enumerate(red): if r % j == 0 or r % (b // j) == 0: edge[i].add((M+k, 1)) for i in range(M): edge[M+N].add((i, 1)) for j in range(N): edge[M+j].add((M+N+1, 1)) d = Dinic(V, edge, M+N, M+N+1) print(d.maxflow) ```
instruction
0
87,462
19
174,924
Yes
output
1
87,462
19
174,925
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from collections import deque inf = 100000000000000000 def bfs(graph, s, t): V = len(graph) q = deque([(s, -1, -1)]) prev = [(-1, -1)] * V visited = [False] * V while len(q) > 0: (v, p, e) = q.popleft() if visited[v]: continue visited[v] = True prev[v] = p, e for i in range(len(graph[v])): e = graph[v][i] if e['cap'] > 0: q.append((e['to'], v, i)) if not visited[t]: return None else: return prev def decrease_graph(graph, s, t, prev): ((p, e), c) = prev[t], t m = inf while p != -1: m = min(graph[p][e]['cap'], m) ((p, e), c) = prev[p], p ((p, e), c) = prev[t], t while p != -1: graph[p][e]['cap'] -= m graph[c][graph[p][e]['rev']]['cap'] += m ((p, e), c) = prev[p], p return m def maximum_flow(graph, s, t): V = len(graph) h g = [[] for _ in range(V)] for v in range(V): nei = graph[v] for (u, cap) in nei: g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])}) g[u].append({'to' : v, 'from': u, 'cap': 0, 'rev': len(g[v]) - 1}) sum = 0 while True: prev = bfs(g, s, t) if prev == None: break sum += decrease_graph(g, s, t, prev) return sum def gcd(x, y): while y > 0: x, y = y, x % y return x def main(): while True: m, n = map(int, input().split()) if m == 0 and n == 0: return b = [] while len(b) < m: b += list(map(int, input().split())) r = [] while len(r) < n: r += list(map(int, input().split())) g = [[] for i in range(n + m + 2)] for i in range(m): g[0].append((i + 1, 1)) for i in range(n): g[m + i + 1].append((n + m + 1, 1)) for i in range(m): for j in range(n): if gcd(b[i], r[j]) > 1: g[i + 1].append((m + j + 1, 1)) print(maximum_flow(g, 0, n + m + 1)) main() ```
instruction
0
87,463
19
174,926
No
output
1
87,463
19
174,927
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from collections import deque inf = 100000000000000000 def dfs(graph, s, t): V = len(graph) prev = [(-1, -1)] * V visited = [False] * V visited[0] = True def iter(cur): if cur == t: return True for i in range(len(graph[cur])): e = graph[cur][i] if not visited[e['to']] and e['cap'] > 0 : prev[e['to']] = (cur, i) visited[e['to']] = True if iter(e['to']): return True return False iter(s) if not visited[t]: return None else: return prev def decrease_graph(graph, s, t, prev): ((p, e), c) = prev[t], t m = inf while p != -1: m = min(graph[p][e]['cap'], m) ((p, e), c) = prev[p], p ((p, e), c) = prev[t], t while p != -1: graph[p][e]['cap'] -= m graph[c][graph[p][e]['rev']]['cap'] += m ((p, e), c) = prev[p], p return m def maximum_flow(graph, s, t): V = len(graph) g = [[] for _ in range(V)] for v in range(V): nei = graph[v] for (u, cap) in nei: g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])}) g[u].append({'to' : v, 'from': u, 'cap': 0, 'rev': len(g[v]) - 1}) sum = 0 while True: prev = dfs(g, s, t) if prev == None: break sum += decrease_graph(g, s, t, prev) return sum def gcd(x, y): while y > 0: x, y = y, x % y return x def main(): while True: m, n = map(int, input().split()) if m == 0 and n == 0: return data = [] while len(data) < n + m: data += list(map(int, input().split())) b = data[:m] r = data[m:] while len(r) < n: r += list(map(int, input().split())) g = [[] for i in range(n + m + 2)] for i in range(m): g[0].append((i + 1, 1)) for i in range(n): g[m + i + 1].append((n + m + 1, 1)) for i in range(m): for j in range(n): if gcd(b[i], r[j]) > 1: g[i + 1].append((m + j + 1, 1)) print(maximum_flow(g, 0, n + m + 1)) main() ```
instruction
0
87,464
19
174,928
No
output
1
87,464
19
174,929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from fractions import gcd def add_edge(u, v): G[u].append(v) G[v].append(u) def dfs(v): used[v] = True for u in G[v]: w = match[u] if w < 0 or not used[w] and dfs(w): match[v] = u match[u] = v return True else: return False def bipartite_matching(): result = 0 match[:] = [-1] * (numv) for v in range(numv): if match[v] < 0: used[:] = [False] * numv if dfs(v): result += 1 return result while True: m, n = map(int, input().split()) if (m, n) == (0, 0): break numv = m + n cards = [] while len(cards) < numv: cards.extend(map(int, input().split())) blues = cards[:m] reds = cards[m:] G = [[] for i in range(numv)] used = [False] * numv match = [-1] * numv for i, b in enumerate(blues): for j, r in enumerate(reds, m): if gcd(b, r) > 1: add_edge(i, j) print(bipartite_matching()) ```
instruction
0
87,465
19
174,930
No
output
1
87,465
19
174,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. <image> Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. > m n > b1 ... bk ... bm > r1 ... rk ... rn > The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85 Example Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output 3 1 0 4 9 18 85 Submitted Solution: ``` from collections import deque inf = 100000000000000000 def bfs(graph, s, t): V = len(graph) q = deque([(s, -1, -1)]) prev = [(-1, -1)] * V visited = [False] * V while len(q) > 0: (v, p, e) = q.popleft() if visited[v]: continue visited[v] = True prev[v] = p, e for i in range(len(graph[v])): e = graph[v][i] if e['cap'] > 0: q.append((e['to'], v, i)) if not visited[t]: return None else: return prev def decrease_graph(graph, s, t, prev): ((p, e), c) = prev[t], t m = inf while p != -1: m = min(graph[p][e]['cap'], m) ((p, e), c) = prev[p], p ((p, e), c) = prev[t], t while p != -1: graph[p][e]['cap'] -= m graph[c][graph[p][e]['rev']]['cap'] += m ((p, e), c) = prev[p], p return m def maximum_flow(graph, s, t): V = len(graph) g = [[] for _ in range(V)] for v in range(V): nei = graph[v] for (u, cap) in nei: g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])}) g[u].append({'to' : v, 'from': u, 'cap': 0, 'rev': len(g[v]) - 1}) sum = 0 while True: prev = bfs(g, s, t) if prev == None: break sum += decrease_graph(g, s, t, prev) return sum def gcd(x, y): while y > 0: x, y = y, x % y return x def main(): while True: m, n = map(int, input().split()) if m == 0 and n == 0: return b = [] b += list(map(int, input().split())) r = [] r += list(map(int, input().split())) g = [[] for i in range(n + m + 2)] for i in range(m): g[0].append((i + 1, 1)) for i in range(n): g[m + i + 1].append((n + m + 1, 1)) for i in range(m): for j in range(n): if gcd(b[i], r[j]) > 1: g[i + 1].append((m + j + 1, 1)) print(maximum_flow(g, 0, n + m + 1)) main() ```
instruction
0
87,466
19
174,932
No
output
1
87,466
19
174,933
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,865
19
175,730
Tags: dfs and similar, dp Correct Solution: ``` n = int(input()) arr = list(input().split()) evl = lambda c1, c2: c1[0] == c2[0] or c1[1] == c2[1] vt = set() q = [arr[:]] while q: ar = q.pop() vt.add(''.join(ar)) if len(ar) > 3 and evl(ar[-1], ar[-4]): tmp = ar[:len(ar) - 1] tmp[-3] = ar[-1] if ''.join(tmp) not in vt: q.append(tmp) if len(ar) > 1 and evl(ar[-1], ar[-2]): tmp = ar[:len(ar) - 1] tmp[-1] = ar[-1] if len(tmp) == 1: print('YES') exit(0) elif ''.join(tmp) not in vt: q.append(tmp) print('NO' if n > 1 else 'YES') ```
output
1
87,865
19
175,731
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,866
19
175,732
Tags: dfs and similar, dp Correct Solution: ``` def f(a, b): return a[0] == b[0] or a[1] == b[1] def h(p): return p[0] == p[2] or p[1] == p[3] def g(p): return p[0] == p[4] or p[1] == p[5] n, t = int(input()), input().split()[:: -1] if n > 2: s = {t[0] + t[1] + t[2]} for k in range(3, n): d = set() for p in s: if f(t[k], p): d.add(p[2: ] + p[: 2]) if h(p): d.add(p[: 2] + p[4: ] + t[k]) s = d print('YES' if any(h(p) and g(p) for p in s) else 'NO') else: print('YES' if n == 1 or f(t[0], t[1]) else 'NO') ```
output
1
87,866
19
175,733
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,867
19
175,734
Tags: dfs and similar, dp Correct Solution: ``` n=int(input()) v=input() v = v.replace(' ', '') d={'': 0} def rasklad(s): if ( len(s) == 2 ): return 1 if s in d: return d[s] if s[-1] == s[-3] or s[-2] == s[-4]: if rasklad( s[0:-4] + s[-2:] ) : d[s]=1 return 1 if len(s) >= 8 and (s[-1] == s[-7] or s[-2] == s[-8]): if rasklad( s[0:-8] + s[-2:] + s[-6:-2]) : d[s]=1 return 1 d[s]=0 return 0 print("YES" if rasklad(v) else "NO") ```
output
1
87,867
19
175,735
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,868
19
175,736
Tags: dfs and similar, dp Correct Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: r = False if ok(c1, a[i]): r = r or dfs(i - 1, c2, c3, c1) if ok(c1, c2): r = r or dfs(i - 1, c1, c3, a[i]) memo[k] = r return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[1], a[0]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') ```
output
1
87,868
19
175,737
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,869
19
175,738
Tags: dfs and similar, dp Correct Solution: ``` d = dict() def mem(i,a): if(i == 1): print("YES") exit(0) if((i,a) in d): return b = list(a[::]) if(a[i-2][0] == a[i-1][0] or a[i-2][1] == a[i-1][1]): b[i-2] = a[i-1] mem(i-1,tuple(b[:i-1])) b[i-2] = a[i-2] if(i > 3 and (a[i-4][0] == a[i-1][0] or a[i-4][1] == a[i-1][1])): b[i-4] = a[i-1] mem(i-1,tuple(b[:i-1])) d[(i,a)] = 0 n = int(input()) a = list(input().split()) mem(n,tuple(a)) print("NO") ```
output
1
87,869
19
175,739
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,870
19
175,740
Tags: dfs and similar, dp Correct Solution: ``` memory = {} def main(): number_of_pile = (input()) card_deck = tuple(input().split()) if canComplete(card_deck): print('YES') else: print("NO") def canPutOnTop(pileToPut, pileToPutOn): result = pileToPut[0] == pileToPutOn[0] or pileToPut[1] == pileToPutOn[1] # print(pileToPut, pileToPutOn, result) return result def putOnPileBeforeLast(deck): lastPile = deck[-1] pileBeforeLastPile = deck[-2] if canPutOnTop(lastPile, pileBeforeLastPile): new_deck = deck[:-2] + (deck[-1],) return canComplete(new_deck) return False def putOnThirdPileBeforeLast(deck): if len(deck) >= 4: lastPile = deck[-1] thirdPileBeforeLastPile = deck[-4] if canPutOnTop(lastPile, thirdPileBeforeLastPile): new_deck = deck[:-4] + (deck[-1],) + deck[-3:-1] return canComplete(new_deck) return False def canComplete(card_deck: tuple): if len(card_deck) == 1: return True if card_deck in memory: return memory[card_deck] top = putOnPileBeforeLast(card_deck) left = putOnThirdPileBeforeLast(card_deck) memory[card_deck] = top or left return top or left if __name__ == '__main__': main() ```
output
1
87,870
19
175,741
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,871
19
175,742
Tags: dfs and similar, dp Correct Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: r = False if ok(c1, a[i]): r = r or dfs(i - 1, c2, c3, c1) if ok(c1, c2): r = r or dfs(i - 1, c1, c3, a[i]) memo[k] = r return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[1], a[0]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') # Made By Mostafa_Khaled ```
output
1
87,871
19
175,743
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
instruction
0
87,872
19
175,744
Tags: dfs and similar, dp Correct Solution: ``` res = {'': 1} def valid(a): if len(a) == 2: return 1 if a in res: return res[a] if (a[-1] == a[-3] or a[-2] == a[-4]): if (valid(a[:-4] + a[-2:])): res[a] = 1 return 1 if (len(a) > 7 and (a[-1] == a[-7] or a[-2] == a[-8])): if (valid(a[:-8] + a[-2:] + a[-6:-2])): res[a] = 1 return 1 res[a] = 0 return 0 n = int(input()) s = input() s = s.replace(' ', '') print("YES" if valid(s) else "NO") ```
output
1
87,872
19
175,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) arr = list(input().split()) def evl(c1, c2): return c1[0] == c2[0] or c1[1] == c2[1] vt = set() q = [arr[:]] while q: ar = q.pop() vt.add(''.join(ar)) if len(ar) > 3 and evl(ar[-1], ar[-4]): tmp = ar[:len(ar) - 1] tmp[-3] = ar[-1] if ''.join(tmp) not in vt: q.append(tmp) if len(ar) > 1 and evl(ar[-1], ar[-2]): tmp = ar[:len(ar) - 1] tmp[-1] = ar[-1] if len(tmp) == 1: print('YES') exit(0) elif ''.join(tmp) not in vt: q.append(tmp) print('NO' if n > 1 else 'YES') ```
instruction
0
87,873
19
175,746
Yes
output
1
87,873
19
175,747
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: memo[k] = dfs(i - 1, c1, c3, a[i]) or dfs(i - 1, c2, c3, c1) return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[0], a[1]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') ```
instruction
0
87,874
19
175,748
No
output
1
87,874
19
175,749
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) c = 0 s = input().split(" ") def m(): global c for i in range(len(s)): for j in range(i + 1, len(s)): if s[i][0] != s[j][0] and s[i][1] != s[j][1]: c += 1 if c > 1 or len(s) == 2: return "NO" return "YES" print(m()) ```
instruction
0
87,875
19
175,750
No
output
1
87,875
19
175,751
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) a = input().split() z = [1] for i in range(1, n): z.append(1) if (a[i][1] == a[i-1][1]) or (a[i][0] == a[i-1][0]): z[i] += z[i-1] if (i > 2) and ((a[i][1] == a[i-3][1]) or (a[i][0] == a[i-3][0])): z[i] = max(z[i], z[i-3] + 1) if z[-1] == n: print("YES") else: print("NO") #print(z) ```
instruction
0
87,876
19
175,752
No
output
1
87,876
19
175,753
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` d = dict() def mem(i,v1,v3): if(i == 1): print("YES") exit(0) if((i,v1,v3) in d): return if(i == 3): if(v1[0] == v3[0] or v1[1] == v3[1]): mem(i-1,a[i-1],v3) elif(v1[0] == a[i-1][0] or v1[1] == a[i-1][1]): mem(i-1,a[i-1],v3) if(i > 3 and (v3[0] == a[i-1][0] or v3[1] == a[i-1][1])): mem(i-1,v1,a[i-1]) d[(i,v1,v3)] = 0 n = int(input()) a = list(input().split()) v1 = a[0] if(n > 3): v3 = a[2] else: v3 = 0 mem(n,v1,v3) print("NO") ```
instruction
0
87,877
19
175,754
No
output
1
87,877
19
175,755
Provide tags and a correct Python 3 solution for this coding contest problem. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM
instruction
0
87,878
19
175,756
Tags: dp, games Correct Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(785) for i in range(300): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ```
output
1
87,878
19
175,757
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(785) for i in range(300): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ```
instruction
0
87,879
19
175,758
No
output
1
87,879
19
175,759
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(600) for i in range(100): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=input() if li==0: print("BitAryo") else: print("BitLGM") ```
instruction
0
87,880
19
175,760
No
output
1
87,880
19
175,761
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` n = int(input()) ss = input() from math import * import sys l = [int(s) for s in ss.split()] l.sort() if n == 1: ganaA = (l[0] != 0) elif n == 2: res = [[-1]*(l[0]+1) for i in range(0,l[1]+1)] for i in range(0, len(res)): for j in range(0, len(res[0])): if res[i][j] == -1: res[i][j] = 0 i1, j1, i2, j2 = i+1, j+1, i+1, j+1 while i1 <= l[1]: res[i1][j] = 1 i1 += 1 while j1 <= l[0]: res[i][j1] = 1 j1 += 1 while (i2 <= l[1]) and (j2 <= l[0]): res[i2][j2] = 1 i2 += 1 j2 += 1 ganaA = res[l[1]][l[0]] == 1 elif n == 3: res = [[[-1]*(l[0]+1) for i in range(l[1]+1)] for j in range(l[2] + 1)] for i in range(len(res)): for j in range(i,len(res[0])): for k in range(j,len(res[0][0])): if res[i][j][k] == -1: res[i][j][k] = 0 i1, j1, k1, i2, j2, k2 = i+1, j+1, k+1, i+1, j+1, k+1 while i1 <= l[2]: res[i1][j][k] = 1 i1 += 1 while j1 <= l[1]: res[i][j1][k] = 1 j1 += 1 while k1 <= l[0]: res[i][j][k1] = 1 k1 += 1 while (i2 <= l[2]) and (j2 <= l[1]) and (k1 <= l[0]): res[i2][j2][k2] = 1 i2 += 1 j2 += 1 k2 += 1 ganaA = res[l[2]][l[1]][l[0]] == 1 if ganaA: print("BitLGM") else : print("BitAryo") ```
instruction
0
87,881
19
175,762
No
output
1
87,881
19
175,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(600) for i in range(100): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ```
instruction
0
87,882
19
175,764
No
output
1
87,882
19
175,765
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,973
19
175,946
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` n , m = map(int,input().split()) if n == 1 : print(1) elif m > (n / 2) : print(m - 1 ) elif m <= (n / 2 ): print(m + 1) ```
output
1
87,973
19
175,947
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,974
19
175,948
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` n,m=map(int,input().split()) print(max(1,m-1+2*(m<n-m+1))) # Made By Mostafa_Khaled ```
output
1
87,974
19
175,949
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,975
19
175,950
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` n, m = input().split(' ') n = int(n) m = int(m) if n == 1 and m == 1: print(1) elif n - m <= m - 1: print(m - 1) else: print(m + 1) ```
output
1
87,975
19
175,951
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,976
19
175,952
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` def read_list(t): return [t(x) for x in input().split()] def read_line(t): return t(input()) def read_lines(t, N): return [t(input()) for _ in range(N)] N, M = read_list(int) small, large = M - 1, N - M if N == 1: print(1) elif small >= large: print(M-1) else: print(M+1) ```
output
1
87,976
19
175,953
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,977
19
175,954
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` #pyrival orz import os import sys import math from io import BytesIO, IOBase input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) ############ ---- Dijkstra with path ---- ############ def dijkstra(start, distance, path, n): # requires n == number of vertices in graph, # adj == adjacency list with weight of graph visited = [False for _ in range(n)] # To keep track of vertices that are visited distance[start] = 0 # distance of start node from itself is 0 for i in range(n): v = -1 # Initialize v == vertex from which its neighboring vertices' distance will be calculated for j in range(n): # If it has not been visited and has the lowest distance from start if not visited[v] and (v == -1 or distance[j] < distance[v]): v = j if distance[v] == math.inf: break visited[v] = True # Mark as visited for edge in adj[v]: destination = edge[0] # Neighbor of the vertex weight = edge[1] # Its corresponding weight if distance[v] + weight < distance[destination]: # If its distance is less than the stored distance distance[destination] = distance[v] + weight # Update the distance path[destination] = v # Update the path def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) def lcm(a, b): return (a*b)//gcd(a, b) def ncr(n, r): return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) def npr(n, r): return math.factorial(n)//math.factorial(n-r) def seive(n): primes = [True]*(n+1) ans = [] for i in range(2, n): if not primes[i]: continue j = 2*i while j <= n: primes[j] = False j += i for p in range(2, n+1): if primes[p]: ans += [p] return ans def factors(n): factors = [] x = 1 while x*x <= n: if n % x == 0: if n // x == x: factors.append(x) else: factors.append(x) factors.append(n//x) x += 1 return factors # Functions: list of factors, seive of primes, gcd of two numbers, # lcm of two numbers, npr, ncr def main(): try: n, m = invr() if n == 1: print(n) elif m - 1 >= n - m: print(m-1) else: print(m+1) except Exception as e: print(e) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
output
1
87,977
19
175,955
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,978
19
175,956
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` a,b=map(int,input().split()) c=0 if(a==1 and b==1): print(b) c=1 if(a%2==0 and c==0): if(b>a/2): print(b-1) elif(b<a/2): print(b+1) else: print(b+1) elif(c==0): if(b>(a+1)/2): print(b-1) elif(b<(a+1)/2): print(b+1) else: print(b-1) ```
output
1
87,978
19
175,957
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,979
19
175,958
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` n, m = map(int, input().split()) ans = 0 if n == 1 and m == 1: print (1) exit() if m <= n//2: ans = m+1 else: ans = m-1 print (ans) ```
output
1
87,979
19
175,959
Provide tags and a correct Python 3 solution for this coding contest problem. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
instruction
0
87,980
19
175,960
Tags: constructive algorithms, games, greedy, implementation, math Correct Solution: ``` #-*-coding:utf8;-*- #qpy:3 #qpy:console lst = [eval(i) for i in input().split()] if lst[0] - lst[1] > lst[1] - 1: print(str(lst[1]+1)+"\n") elif lst[0] == 1: print("1\n") else: print(str(lst[1]-1)+"\n") ```
output
1
87,980
19
175,961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less. Submitted Solution: ``` n,m=list(map(int,input().split())) positive=n-m negative=m-1 if(n==1 and m==1): print(m) elif(positive>negative): print(m+1) elif(negative>=positive): print(m-1) ```
instruction
0
87,981
19
175,962
Yes
output
1
87,981
19
175,963
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). Input The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Examples Input 3 1 Output 2 Input 4 3 Output 2 Note In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less. Submitted Solution: ``` a,b=map(int,input().split()) x=a-b y=b-1 if(a==1 and b==1): print(a) elif(x>y): print(b+1) else: print(b-1) ```
instruction
0
87,982
19
175,964
Yes
output
1
87,982
19
175,965