text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` a=input().split() b,c=int(a[0]),int(a[2]) if a[1]=="+":print(b+c) else:print(b-c) ``` Yes
87,400
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` A,op,B=input().split() A,B=int(A),int(B) if op=="+": print(A+B) else: print(A-B) ``` Yes
87,401
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` # python my_input = str(input()) print(str(eval(my_input))) ``` Yes
87,402
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` a,c,b=input().split() print(int(a)+int(b) if c=="+" else int(a)-int(b)) ``` Yes
87,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` a = input().split() return int(a[0]) + int(a[2]) if a[1] == "+" else int(a[0]) - int(a[2]) ``` No
87,404
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` # python user_input = input("Input in format A op B") input_sep = user_input.split() input_a = int(input_sep[0]) operator = input_sep[1] input_b = int(input_sep[2]) if operator is '+': print(int(input_a + input_b)) elif operator is '-': print(int(input_a - input_b)) ``` No
87,405
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` A = int(input()) B = int(input()) OP = input(plus_minus) if OP == "-": print(int(A) + int(B)) elif OP == "+": print(int(A) - int(B)) ``` No
87,406
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her. Constraints * 1≦A,B≦10^9 * op is either `+` or `-`. Input The input is given from Standard Input in the following format: A op B Output Evaluate the formula and print the result. Examples Input 1 + 2 Output 3 Input 5 - 7 Output -2 Submitted Solution: ``` a,op,b=input() a=int(a) b=int(b) if op=="+": print(a+b) else: print(a-b) ``` No
87,407
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` N, K, X, Y = (int(input()) for i in range(4)) print(X*K+Y*(N-K) if N>K else X*N) ```
87,408
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` n, k, x, y = [int(input()) for i in range(4)] print(x * min(n, k) + y * max(n - k, 0)) ```
87,409
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` n,k,x,y = [int(input()) for i in range(4)] ans = min(k,n)*x+max(0,n-k)*y print(ans) ```
87,410
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` n,k,x,y = (int(input()) for _ in range(4)) print(min(n,k)*x + y*max(n-k,0)) ```
87,411
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` n,k,x,y=[int(input()) for i in range(4)] if n>k: print(k*x+(n-k)*y) else: print(n*x) ```
87,412
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` N, K, X, Y = [int(input()) for x in range(4)] print(min(K, N) * X + max(N - K, 0) * Y) ```
87,413
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` [n,k,x,y]=[int(input())for _ in range(4)];print(min(n,k)*x+(max(n,k)-k)*y) ```
87,414
Provide a correct Python 3 solution for this coding contest problem. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 "Correct Solution: ``` N,K,X,Y=[int(input()) for _ in [0]*4] print(min(N,K)*X+max(0,N-K)*Y) ```
87,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` n, k, x, y = [int(input()) for _ in range(4)] print(n*x-(x-y)*max(n-k, 0)) ``` Yes
87,416
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` N,K,X,Y=map(int,open(0).read().split()) print(X*min(N,K)+Y*(N-min(N,K))) ``` Yes
87,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` n, k, x, y = map(int, [input() for i in range(4)]) print( min(n, k)*x + max(n-k, 0)*y ) ``` Yes
87,418
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` a,b,c,d=[int(input()) for i in range(4)] if a<b: print(a*c) else: print(b*c+(a-b)*d) ``` Yes
87,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` N, K, X, Y, = map(int, input().split()) # 高橋くんの宿泊数 N # 宿泊費が変わる  K # 初期宿泊費 X # K+1以降の宿泊費 Y # subは宿泊数の超過分 sub = N - K # もしNよりKが大きい場合、そのまま計算する if N < K: answer = N * X print(answer) # KよりNが大きい場合 K * X + (N - K) * Y elif K < N: answer = K * X + sub * Y print(answer) ``` No
87,420
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` n=int(input()) k=int(input()) x=int(input()) y=int(input()) if n>k: kane=k*x+(n-k)*y else: kane=k*x print(kane) ``` No
87,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` n=int(input()) k=int(input()) x=int(input()) y=int(input()) if n>k: print(x+(n-k)*y) else: print(x) ``` No
87,422
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a hotel with the following accommodation fee: * X yen (the currency of Japan) per night, for the first K nights * Y yen per night, for the (K+1)-th and subsequent nights Tak is staying at this hotel for N consecutive nights. Find his total accommodation fee. Constraints * 1 \leq N, K \leq 10000 * 1 \leq Y < X \leq 10000 * N,\,K,\,X,\,Y are integers. Input The input is given from Standard Input in the following format: N K X Y Output Print Tak's total accommodation fee. Examples Input 5 3 10000 9000 Output 48000 Input 2 3 10000 9000 Output 20000 Submitted Solution: ``` N, K, X, Y = map(int, input().split()) print(X * K + (N - K) * Y) ``` No
87,423
Provide a correct Python 3 solution for this coding contest problem. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 "Correct Solution: ``` dp = [[0 for _ in range(1001)] for _ in range(10)] dp[1][0] = dp[0][0] = 1 for now in range(1, 101): for used in range(9, 0, -1): dpu = dp[used] dpu_1 = dp[used - 1] for s in range(now, 1001): dpu[s] = dpu_1[s - now] + dpu[s] while True: n, s = map(int, input().split()) if not n: break print(dp[n][s]) ```
87,424
Provide a correct Python 3 solution for this coding contest problem. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 "Correct Solution: ``` dp = [[0 for j in range(1001)] for i in range(10)] dp[0][0] = 1 for j in range(1, 1001): dp[0][j] = 0 for k in range(0, 101): for i in range(9, 0, -1): for j in range(k, 1001): dp[i][j] += dp[i - 1][j - k] while True: n, s = map(int, input().split()) if n + s == 0: break print(dp[n][s]) ```
87,425
Provide a correct Python 3 solution for this coding contest problem. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 "Correct Solution: ``` ans = [[0 for i in range(1001)] for j in range(11)] ans[0][0] = 1 for i in range(101): for n in range(9, -1, -1): for s in range(1001 - i): ans[n + 1][s + i] += ans[n][s] while True: n, s = map(int, input().split()) if n == 0: break print(ans[n][s]) ```
87,426
Provide a correct Python 3 solution for this coding contest problem. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 "Correct Solution: ``` """ now...今注目する値 used...使った数字の数 sum...それまでの合計 dp[now][used][sum]...nowまででused個の数字を使って合計sumの場合の数 dp[now][used][sum] = dp[now - 1][used - 1][sum - now] + dp[now - 1][used][sum] (used >= 1 and sum >= now) dp[now - 1][used][sum] (used == 0 or sum < now) 2次元化 dp[used][sum]...used個の数字を使って合計sumの場合の数 dp[used][sum] = dp[used - 1][sum - now] + dp[used][sum] (used >= 1 and sum >= now) ただし、usedの大きい順に更新する(更新がかぶるため) """ dp = [[0 for _ in range(1001)] for _ in range(10)] dp[1][0] = 1 dp[0][0] = 1 for now in range(1, 101): for used in range(9, 0, -1): for s in range(now, 1001): dp[used][s] = dp[used - 1][s - now] + dp[used][s] while True: n, s = map(int, input().split()) if not n: break print(dp[n][s]) ```
87,427
Provide a correct Python 3 solution for this coding contest problem. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 "Correct Solution: ``` # AOJ 0097 Sum of Integers II # Python3 2018.6.15 bal4u dp = [[0 for s in range(1001)] for n in range(11)] dp[0][0] = 1 for k in range(101): for n in range(9, -1, -1): for s in range(1001-k): dp[n+1][s+k] += dp[n][s] while True: n, s = list(map(int, input().split())) if n == 0: break print(dp[n][s]) ```
87,428
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 Submitted Solution: ``` def dp(m,n,s): if n == 1: if s > 100 or s < m: return 0 else: return 1 else: sum_ = 0 for i in range(m,min(s,100)+1): sum_ += dp(i+1,n-1,s-i) return sum_ while(1): n,s = [int(i) for i in input().split()] if n == 0 and s == 0: break print(dp(0,n,s)) ``` No
87,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 Submitted Solution: ``` from itertools import combinations from sys import stdin sample = [_ for _ in range(101)] for _ in stdin.readlines() : n, s = map(int, _.split()) if n == 0 and s == 0 : break print(len([_2 for _2, _3 in enumerate(combinations(sample[:s], n)) if sum(_3) == s])) ``` No
87,430
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 Submitted Solution: ``` def dp(m,n,s): if n == 1: if s > 100 or s < m: return 0 else: return 1 else: sum_ = 0 for i in range(m,min(s,100)+1): sum_ += dp(i,n-1,s-i) return sum_ fact = [0,0,1,3,6,10,15,21,28,36,45] while(1): n,s = [int(i) for i in input().split()] if n == 0 and s == 0: break print(dp(0,n,s-fact[n])) ``` No
87,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 + 1 + 5 = 6 0 + 2 + 4 = 6 There are three ways. Input Given multiple datasets. For each dataset, n (1 ≤ n ≤ 9) and s (0 ≤ s ≤ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input. The number of datasets does not exceed 50. Output For each dataset, output the number of combinations in which the sum of n integers is s on one line. No input is given with more than 1010 combinations. Example Input 3 6 3 1 0 0 Output 3 0 Submitted Solution: ``` from copy import copy from itertools import combinations from sys import stdin sample = [_ for _ in range(101)] for _ in stdin.readlines() : n, s = map(int, _.split()) if n == 0 and s == 0 : break print(len([_2 for _2 in combinations(sample[:s], n) if sum(_2) == s])) ``` No
87,432
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 "Correct Solution: ``` # Aizu Problem 0229: Big Hit import sys, math, os, bisect # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") while True: b, r, g, c, s, t = [int(_) for _ in input().split()] if t == 0: break print(100 + 95 * b + 63 * r + 7 * g + 2 * c - 3 * (t - s)) ```
87,433
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 "Correct Solution: ``` while 1: b, r, g, c, s, t = map(int, input().split()) if t == 0: break ans = 100 ans += (15 - 2) * (5*b) + (15 - 3) * b t -= 6*b ans += (15 - 2) * (3*r) + (15 - 3) * r t -= 4*r ans += (7 - 3) * g t -= g ans += (2 - 3) * c t -= c t -= s ans += (0 - 3) * t print(ans) ```
87,434
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 "Correct Solution: ``` while True: b, r, g, c, s, t = map(int, input().split()) if b + r + g + c + s + t == 0: break normal_game = t - b * 5 - r * 3 bonus_game = b * 5 + r * 3 print(100 + bonus_game * 16 + 15 * b + 15 * r + 7 * g + 2 * c + 3 * s - t * 3) ```
87,435
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 "Correct Solution: ``` while 1: b, r, g, c, s, t = map(int, input().split()) if [b, r, g, c, s, t] == [0, 0, 0, 0, 0, 0]: break medal = 100 bonus = b * 5 + r * 3 normal = t - bonus - s medal = medal + b * 15 + r * 15 + bonus * 15 + g * 7 + c * 2 medal = medal - normal * 3 - bonus * 2 print(medal) ```
87,436
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 "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) ```
87,437
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 "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) ```
87,438
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 "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) ```
87,439
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 "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) ```
87,440
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) ``` Yes
87,441
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); ``` Yes
87,442
Provide a correct Python 3 solution for this coding contest problem. Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3". | 0| 2| 2| 1| 1 ---|---|---|---|---|--- 1| | | #| | 1| | #| | | 1| | | | | # 3| | #| #| #| Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output "1" if the information written on the paper is relevant, or "0" otherwise. Examples Input 5 4 0 2 2 1 1 1 1 1 3 Output 1 Input 5 5 3 2 3 0 0 4 2 0 0 2 Output 0 "Correct Solution: ``` import sys,heapq _,a,q=[[-int(e) for e in sys.stdin.readline().split() if e!='0'] for _ in[0]*3] heapq.heapify(q) for e in a: t=[] for _ in [0]*-e: if not q:print(0);exit() if q[0]!=-1:heapq.heappush(t,q[0]+1) heapq.heappop(q) while t:heapq.heappush(q,t[0]);heapq.heappop(t) print(int(not q)) ```
87,443
Provide a correct Python 3 solution for this coding contest problem. Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3". | 0| 2| 2| 1| 1 ---|---|---|---|---|--- 1| | | #| | 1| | #| | | 1| | | | | # 3| | #| #| #| Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output "1" if the information written on the paper is relevant, or "0" otherwise. Examples Input 5 4 0 2 2 1 1 1 1 1 3 Output 1 Input 5 5 3 2 3 0 0 4 2 0 0 2 Output 0 "Correct Solution: ``` w, h = map(int, input().split()) lst1 = sorted(map(int, input().split())) lst2 = sorted(map(int, input().split())) while lst1: num = lst1.pop() for i in range(-1, -num - 1, -1): lst2[i] -= 1 lst2.sort() if sum(lst2) == 0 and min(lst2) == 0: print(1) else: print(0) ```
87,444
Provide a correct Python 3 solution for this coding contest problem. Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3". | 0| 2| 2| 1| 1 ---|---|---|---|---|--- 1| | | #| | 1| | #| | | 1| | | | | # 3| | #| #| #| Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output "1" if the information written on the paper is relevant, or "0" otherwise. Examples Input 5 4 0 2 2 1 1 1 1 1 3 Output 1 Input 5 5 3 2 3 0 0 4 2 0 0 2 Output 0 "Correct Solution: ``` import sys w,h = map(int, input().split()) sumC = 0 sumR = 0 col = list(map(int, input().split())) row = list(map(int, input().split())) for c in col : sumC += c for r in row : sumR += r if sumR != sumC : print(0) sys.exit(0) for i in range(w): row.sort(reverse=True) for j in range(h): if not col[i] or not row[j] : break row[j] -= 1 col[i] -= 1 if col[i] > 0 : print(0) sys.exit(0) print(1) ```
87,445
Provide a correct Python 3 solution for this coding contest problem. Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3". | 0| 2| 2| 1| 1 ---|---|---|---|---|--- 1| | | #| | 1| | #| | | 1| | | | | # 3| | #| #| #| Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output "1" if the information written on the paper is relevant, or "0" otherwise. Examples Input 5 4 0 2 2 1 1 1 1 1 3 Output 1 Input 5 5 3 2 3 0 0 4 2 0 0 2 Output 0 "Correct Solution: ``` W, H = map(int, input().split()) *A, = map(int, input().split()) *B, = map(int, input().split()) A.sort(reverse=1) ok =+ (sum(A) == sum(B)) for a in A: B.sort(reverse=1) for i in range(a): if i >= len(B) or B[i] == 0: ok = 0 break B[i] -= 1 print(ok) ```
87,446
Provide a correct Python 3 solution for this coding contest problem. Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3". | 0| 2| 2| 1| 1 ---|---|---|---|---|--- 1| | | #| | 1| | #| | | 1| | | | | # 3| | #| #| #| Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output "1" if the information written on the paper is relevant, or "0" otherwise. Examples Input 5 4 0 2 2 1 1 1 1 1 3 Output 1 Input 5 5 3 2 3 0 0 4 2 0 0 2 Output 0 "Correct Solution: ``` input() A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] ans = 0 A.sort(reverse=True) for a in A: B.sort(reverse=True) for i in range(a): B[i] -= 1 if min(B) < 0: ans = 0 break if max(B) == 0: ans = 1 print(ans) ```
87,447
Provide a correct Python 3 solution for this coding contest problem. Emacs is a text editor which is widely used by many programmers. The advantage of Emacs is that we can move a cursor without arrow keys and the mice. For example, the cursor can be moved right, left, down, and up by pushing f, b, n, p with the Control Key respectively. In addition, cut-and-paste can be performed without the mouse. Your task is to write a program which simulates key operations in the Emacs-like editor. The program should read a text and print the corresponding edited text. The text consists of several lines and each line consists of zero or more alphabets and space characters. A line, which does not have any character, is a blank line. The editor has a cursor which can point out a character or the end-of-line in the corresponding line. The cursor can also point out the end-of-line in a blank line. In addition, the editor has a buffer which can hold either a string (a sequence of characters) or a linefeed. The editor accepts the following set of commands (If the corresponding line is a blank line, the word "the first character" should be "the end-of-line"): * a Move the cursor to the first character of the current line. * e Move the cursor to the end-of-line of the current line. * p Move the cursor to the first character of the next upper line, if it exists. If there is no line above the current line, move the cursor to the first character of the current line. * n Move the cursor to the first character of the next lower line, if it exists. If there is no line below the current line, move the cursor to the first character of the current line. * f Move the cursor by one character to the right, unless the cursor points out the end-of-line. If the cursor points out the end-of-line and there is a line below the current line, move the cursor to the first character of the next lower line. Otherwise, do nothing. * b Move the cursor by one character to the left, unless the cursor points out the first character. If the cursor points out the first character and there is a line above the current line, move the cursor to the end-of-line of the next upper line. Otherwise, do nothing. * d If the cursor points out a character, delete the character (Characters and end-of-line next to the deleted character are shifted to the left). If the cursor points out the end-of-line and there is a line below, the next lower line is appended to the end-of-line of the current line (Lines below the current line are shifted to the upper). Otherwise, do nothing. * k If the cursor points out the end-of-line and there is a line below the current line, perform the command d mentioned above, and record a linefeed on the buffer. If the cursor does not point out the end-of-line, cut characters between the cursor (inclusive) and the end-of-line, and record them on the buffer. After this operation, the cursor indicates the end-of-line of the current line. * y If the buffer is empty, do nothing. If the buffer is holding a linefeed, insert the linefeed at the cursor. The cursor moves to the first character of the new line. If the buffer is holding characters, insert the characters at the cursor. The cursor moves to the character or end-of-line which is originally pointed by the cursor. The cursor position just after reading the text is the beginning of the first line, and the initial buffer is empty. Constraints * The number of lines in the text given as input ≤ 10 * The number of characters in a line given as input ≤ 20 * The number of commands ≤ 300 * The maximum possible number of lines in the text during operations ≤ 100 * The maximum possible number of characters in a line during operations ≤ 1000 Input The input consists of only one data-set which includes two parts. The first part gives a text consisting of several lines. The end of the text is indicated by a line (without quotes): "END_OF_TEXT" This line should not be included in the text. Next part gives a series of commands. Each command is given in a line. The end of the commands is indicated by a character '-'. Output For the input text, print the text edited by the commands. Example Input hyo ni END_OF_TEXT f d f f k p p e y a k y y n y - Output honihoni honi "Correct Solution: ``` import sys class Cursor: BOL = (0,1) def __init__(self, text): assert isinstance(text, list) self.text = text for i in range(len(self.text)): self.text[i] = list(self.text[i]) self.buffer = "" self.line = 1 self.pos = self.BOL def _check(self): assert self.line > 0 def _get_pos_end_of_line(self): last_char = len(self.text[self.line-1]) return (last_char, last_char+1) def has_prev_line(self): return self.line > 1 def has_next_line(self): return self.line < len(self.text) def beginning_of_line(self): self.pos = self.BOL def end_of_line(self): self.pos = self._get_pos_end_of_line() def prev_line(self): if self.has_prev_line(): self.line -= 1 def next_line(self): if self.has_next_line(): self.line += 1 def right_pos(self): if self._get_pos_end_of_line() != self.pos: self.pos = (self.pos[0] + 1, self.pos[1] + 1) else: if self.has_next_line(): self.line += 1 self.pos = self.BOL def left_pos(self): if self.BOL != self.pos: self.pos = (self.pos[0] - 1, self.pos[1] - 1) else: if self.has_prev_line(): self.line -= 1 self.pos = self._get_pos_end_of_line() def remove_current_pos(self): if self._get_pos_end_of_line() != self.pos: self.text[self.line-1] = self.text[self.line-1][:self.pos[0]] + self.text[self.line-1][self.pos[1]:] else: if self.has_next_line(): self.text[self.line-1][self.pos[0]:self.pos[0]] = self.text[self.line] self.text.pop(self.line) def remove_to_end_of_line(self): if self._get_pos_end_of_line() == self.pos: if self.has_next_line(): self.remove_current_pos() self.buffer = '\n' else: self.buffer = self.text[self.line - 1][self.pos[0]:] self.text[self.line - 1] = self.text[self.line - 1][:self.pos[0]] self.pos = self._get_pos_end_of_line() def paste(self): if self.buffer: if self.buffer == '\n': prev = self.text[self.line - 1][:self.pos[0]] next = self.text[self.line - 1][self.pos[0]:] self.text[self.line - 1] = prev self.text.insert(self.line, next) self.line += 1 self.pos = self.BOL else: self.text[self.line - 1][self.pos[0]:self.pos[0]] = self.buffer self.pos = (self.pos[0] + len(self.buffer), self.pos[1] + len(self.buffer)) def print(self): for t in self.text: print("".join(t)) class Editor: def __init__(self, text): assert isinstance(text, list) self.cursor = Cursor(text) def process(self, commands): if not isinstance(commands, list): commands = [commands] for command in commands: self._process(command) self.cursor.print() def _process(self, command): if command == 'a': self.cursor.beginning_of_line() elif command == 'e': self.cursor.end_of_line() elif command == 'p': self.cursor.prev_line() self.cursor.beginning_of_line() elif command == 'n': self.cursor.next_line() self.cursor.beginning_of_line() elif command == 'f': self.cursor.right_pos() elif command == 'b': self.cursor.left_pos() elif command == 'd': self.cursor.remove_current_pos() elif command == 'k': self.cursor.remove_to_end_of_line() elif command == 'y': self.cursor.paste() def load_input(): inputs = [] while True: try: inputs.append(input()) except EOFError: return inputs def split_end_of_text(inputs): assert inputs assert 'END_OF_TEXT' in inputs for i in range(len(inputs)): if inputs[i] == 'END_OF_TEXT': return inputs[:i], inputs[i+1:] else: print("DO NOT DETECT END_OF_TEXT. got:", inputs) sys.exit(1) def main(): inputs = load_input() text, commands = split_end_of_text(inputs) # Attacking assert 'END_OF_TEXT' not in text assert 'END_OF_TEXT' not in commands editor = Editor(text) editor.process(commands) main() ```
87,448
Provide a correct Python 3 solution for this coding contest problem. Emacs is a text editor which is widely used by many programmers. The advantage of Emacs is that we can move a cursor without arrow keys and the mice. For example, the cursor can be moved right, left, down, and up by pushing f, b, n, p with the Control Key respectively. In addition, cut-and-paste can be performed without the mouse. Your task is to write a program which simulates key operations in the Emacs-like editor. The program should read a text and print the corresponding edited text. The text consists of several lines and each line consists of zero or more alphabets and space characters. A line, which does not have any character, is a blank line. The editor has a cursor which can point out a character or the end-of-line in the corresponding line. The cursor can also point out the end-of-line in a blank line. In addition, the editor has a buffer which can hold either a string (a sequence of characters) or a linefeed. The editor accepts the following set of commands (If the corresponding line is a blank line, the word "the first character" should be "the end-of-line"): * a Move the cursor to the first character of the current line. * e Move the cursor to the end-of-line of the current line. * p Move the cursor to the first character of the next upper line, if it exists. If there is no line above the current line, move the cursor to the first character of the current line. * n Move the cursor to the first character of the next lower line, if it exists. If there is no line below the current line, move the cursor to the first character of the current line. * f Move the cursor by one character to the right, unless the cursor points out the end-of-line. If the cursor points out the end-of-line and there is a line below the current line, move the cursor to the first character of the next lower line. Otherwise, do nothing. * b Move the cursor by one character to the left, unless the cursor points out the first character. If the cursor points out the first character and there is a line above the current line, move the cursor to the end-of-line of the next upper line. Otherwise, do nothing. * d If the cursor points out a character, delete the character (Characters and end-of-line next to the deleted character are shifted to the left). If the cursor points out the end-of-line and there is a line below, the next lower line is appended to the end-of-line of the current line (Lines below the current line are shifted to the upper). Otherwise, do nothing. * k If the cursor points out the end-of-line and there is a line below the current line, perform the command d mentioned above, and record a linefeed on the buffer. If the cursor does not point out the end-of-line, cut characters between the cursor (inclusive) and the end-of-line, and record them on the buffer. After this operation, the cursor indicates the end-of-line of the current line. * y If the buffer is empty, do nothing. If the buffer is holding a linefeed, insert the linefeed at the cursor. The cursor moves to the first character of the new line. If the buffer is holding characters, insert the characters at the cursor. The cursor moves to the character or end-of-line which is originally pointed by the cursor. The cursor position just after reading the text is the beginning of the first line, and the initial buffer is empty. Constraints * The number of lines in the text given as input ≤ 10 * The number of characters in a line given as input ≤ 20 * The number of commands ≤ 300 * The maximum possible number of lines in the text during operations ≤ 100 * The maximum possible number of characters in a line during operations ≤ 1000 Input The input consists of only one data-set which includes two parts. The first part gives a text consisting of several lines. The end of the text is indicated by a line (without quotes): "END_OF_TEXT" This line should not be included in the text. Next part gives a series of commands. Each command is given in a line. The end of the commands is indicated by a character '-'. Output For the input text, print the text edited by the commands. Example Input hyo ni END_OF_TEXT f d f f k p p e y a k y y n y - Output honihoni honi "Correct Solution: ``` text = [] while True: s = input() if s == "END_OF_TEXT": break text.append(s) x, y = 0, 0 buff = "" while True: c = input() if c == "-": break elif c == "a": x = 0 elif c == "e": x = len(text[y]) elif c == "p": if y == 0: x = 0 else: x = 0 y -= 1 elif c == "n": if y == len(text) - 1: x = 0 else: y += 1 x = 0 elif c == "f": if x != len(text[y]): x += 1 elif y != len(text) - 1: y += 1 x = 0 elif c == "b": if x != 0: x -= 1 elif y != 0: y -= 1 x = len(text[y]) elif c =="d": if x < len(text[y]): text[y] = text[y][:x] + text[y][x + 1:] elif y != len(text) - 1: text[y] = text[y] + text[y + 1] text = text[:y + 1] + text[y + 2:] elif c == "k": if x < len(text[y]): buff = text[y][x:] text[y] = text[y][:x] elif y != len(text) - 1: text[y] = text[y] + text[y + 1] text = text[:y + 1] + text[y + 2:] buff = "\n" elif c =="y": if buff == "\n": new_row = text[y][x:] text[y] = text[y][:x] text = text[:y + 1] + [new_row] + text[y + 1:] x = 0 y += 1 else: text[y] = text[y][:x] + buff + text[y][x:] x += len(buff) print(*text, sep="\n") ```
87,449
Provide a correct Python 3 solution for this coding contest problem. Emacs is a text editor which is widely used by many programmers. The advantage of Emacs is that we can move a cursor without arrow keys and the mice. For example, the cursor can be moved right, left, down, and up by pushing f, b, n, p with the Control Key respectively. In addition, cut-and-paste can be performed without the mouse. Your task is to write a program which simulates key operations in the Emacs-like editor. The program should read a text and print the corresponding edited text. The text consists of several lines and each line consists of zero or more alphabets and space characters. A line, which does not have any character, is a blank line. The editor has a cursor which can point out a character or the end-of-line in the corresponding line. The cursor can also point out the end-of-line in a blank line. In addition, the editor has a buffer which can hold either a string (a sequence of characters) or a linefeed. The editor accepts the following set of commands (If the corresponding line is a blank line, the word "the first character" should be "the end-of-line"): * a Move the cursor to the first character of the current line. * e Move the cursor to the end-of-line of the current line. * p Move the cursor to the first character of the next upper line, if it exists. If there is no line above the current line, move the cursor to the first character of the current line. * n Move the cursor to the first character of the next lower line, if it exists. If there is no line below the current line, move the cursor to the first character of the current line. * f Move the cursor by one character to the right, unless the cursor points out the end-of-line. If the cursor points out the end-of-line and there is a line below the current line, move the cursor to the first character of the next lower line. Otherwise, do nothing. * b Move the cursor by one character to the left, unless the cursor points out the first character. If the cursor points out the first character and there is a line above the current line, move the cursor to the end-of-line of the next upper line. Otherwise, do nothing. * d If the cursor points out a character, delete the character (Characters and end-of-line next to the deleted character are shifted to the left). If the cursor points out the end-of-line and there is a line below, the next lower line is appended to the end-of-line of the current line (Lines below the current line are shifted to the upper). Otherwise, do nothing. * k If the cursor points out the end-of-line and there is a line below the current line, perform the command d mentioned above, and record a linefeed on the buffer. If the cursor does not point out the end-of-line, cut characters between the cursor (inclusive) and the end-of-line, and record them on the buffer. After this operation, the cursor indicates the end-of-line of the current line. * y If the buffer is empty, do nothing. If the buffer is holding a linefeed, insert the linefeed at the cursor. The cursor moves to the first character of the new line. If the buffer is holding characters, insert the characters at the cursor. The cursor moves to the character or end-of-line which is originally pointed by the cursor. The cursor position just after reading the text is the beginning of the first line, and the initial buffer is empty. Constraints * The number of lines in the text given as input ≤ 10 * The number of characters in a line given as input ≤ 20 * The number of commands ≤ 300 * The maximum possible number of lines in the text during operations ≤ 100 * The maximum possible number of characters in a line during operations ≤ 1000 Input The input consists of only one data-set which includes two parts. The first part gives a text consisting of several lines. The end of the text is indicated by a line (without quotes): "END_OF_TEXT" This line should not be included in the text. Next part gives a series of commands. Each command is given in a line. The end of the commands is indicated by a character '-'. Output For the input text, print the text edited by the commands. Example Input hyo ni END_OF_TEXT f d f f k p p e y a k y y n y - Output honihoni honi "Correct Solution: ``` class Editer: def __init__(self, text): # カーソルの位置 self.row = 0 #行 self.col = 0 #列 # 編集中のテキスト self.text = [list(t) + ['\n'] for t in text] # バッファー self.buffer = [] def row_head(self): return 0 def row_tail(self): return len(self.text) - 1 def col_head(self): return 0 def col_tail(self): return len(self.text[self.row]) - 1 def __repr__(self): return ''.join(''.join(t) for t in self.text) def command_a(self): # カーソルを現在の行の先頭文字に移動 self.col = self.col_head() def command_e(self): # カーソルを現在の行の行末に移動 self.col = self.col_tail() def command_p(self): # 上に行があれば、カーソルを上の行に if self.row != self.row_head() : self.row -= 1 # カーソルを先頭に self.col = self.col_head() def command_n(self): # 下に行があれば if self.row != self.row_tail(): # カーソルを下の行に移動 self.row += 1 # カーソルを先頭文字に移動 self.col = self.col_head() def command_b(self): # カーソルが行末にない場合 if self.col != self.col_head(): # カーソルを1つ左に移動 self.col -= 1 # カーソルが行末にあり、上に行がある場合 elif self.row != self.row_head(): # カーソルを前の行の先頭に self.row -= 1 self.col = self.col_tail() def command_f(self): # カーソルが行末にない場合 if self.col != self.col_tail(): # カーソルを1つ右に移動 self.col += 1 # カーソルが行末にあり、下に行がある場合 elif self.row != self.row_tail(): # カーソルを次の行の先頭に self.row += 1 self.col = self.col_head() def command_d(self): # カーソルが行末にない場合 if self.col != self.col_tail(): # カーソルの文字を削除 self.text[self.row].pop(self.col) # カーソルが行末を指し、下に行がある場合 elif self.row != self.row_tail(): # 下の行をそのままカーソルの位置に繋げ、以下の行は上にシフト self.text[self.row].pop(self.col_tail()) self.text[self.row] += self.text.pop(self.row+1) def command_k(self): # カーソルが行末にない場合 if self.col != self.col_tail(): # カーソルが指す文字を含めた右側すべての文字を切り取りそれをバッファに記録する。 self.buffer = self.text[self.row][self.col:-1] self.text[self.row] = self.text[self.row][:self.col] + ['\n'] # カーソルは元の行の行末を指すようになる self.col = self.col_tail() # カーソルが行末にあり、下に行がある場合 elif self.row != self.row_tail(): # バッファに改行を記録する。 self.buffer = ['\n'] # 下の行をそのままカーソルの位置に繋げる。以下の行は上にシフトされる。 self.text[self.row].pop(self.col_tail()) self.text[self.row] += self.text.pop(self.row+1) def command_y(self): ''' カーソルが指す文字の直前にバッファを挿入 カーソルの位置はもともと指していた文字へ移動 バッファの内容が改行なら ''' if self.buffer != ['\n']: self.text[self.row][self.col:self.col] = self.buffer self.col += len(self.buffer) else: self.text.insert(self.row+1, self.text[self.row][self.col:]) self.text[self.row] = self.text[self.row][:self.col] + ['\n'] self.row += 1 self.col = self.col_head() def main(): # input text text = [] while True: str = input() if str == 'END_OF_TEXT': break text.append(str) editer = Editer(text) # input command while True: command = input() if command == 'a' : editer.command_a() elif command == 'e' : editer.command_e() elif command == 'p' : editer.command_p() elif command == 'n' : editer.command_n() elif command == 'f' : editer.command_f() elif command == 'b' : editer.command_b() elif command == 'd' : editer.command_d() elif command == 'y' : editer.command_y() elif command == 'k' : editer.command_k() elif command == '-' : break print(editer, end='') if __name__ == '__main__': main() ```
87,450
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 "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()) ```
87,451
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 "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") ```
87,452
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 "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") ```
87,453
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 "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) ```
87,454
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 "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()) ```
87,455
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 "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) ```
87,456
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 "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)) ```
87,457
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 "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) ```
87,458
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)) ``` Yes
87,459
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() ``` Yes
87,460
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) ``` Yes
87,461
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) ``` Yes
87,462
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() ``` No
87,463
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() ``` No
87,464
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()) ``` No
87,465
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() ``` No
87,466
Provide a correct Python 3 solution for this coding contest problem. The earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully confined the spread of the infection within a square grid of areas. Recently, public health specialists found an interesting pattern with regard to the transition of infected areas. At each step in time, every area in the grid changes its infection state according to infection states of its directly (horizontally, vertically, and diagonally) adjacent areas. * An infected area continues to be infected if it has two or three adjacent infected areas. * An uninfected area becomes infected if it has exactly three adjacent infected areas. * An area becomes free of the virus, otherwise. Your mission is to fight against the virus and disinfect all the areas. The Ministry of Health lets an anti-virus vehicle prototype under your command. The functionality of the vehicle is summarized as follows. * At the beginning of each time step, you move the vehicle to one of the eight adjacent areas. The vehicle is not allowed to move to an infected area (to protect its operators from the virus). It is not allowed to stay in the same area. * Following vehicle motion, all the areas, except for the area where the vehicle is in, change their infection states according to the transition rules described above. Special functionality of the vehicle protects its area from virus infection even if the area is adjacent to exactly three infected areas. Unfortunately, this virus-protection capability of the vehicle does not last. Once the vehicle leaves the area, depending on the infection states of the adjacent areas, the area can be infected. The area where the vehicle is in, which is uninfected, has the same effect to its adjacent areas as an infected area as far as the transition rules are concerned. The following series of figures illustrate a sample scenario that successfully achieves the goal. Initially, your vehicle denoted by @ is found at (1, 5) in a 5 × 5-grid of areas, and you see some infected areas which are denoted by #'s. <image> Firstly, at the beginning of time step 1, you move your vehicle diagonally to the southwest, that is, to the area (2, 4). Note that this vehicle motion was possible because this area was not infected at the start of time step 1. Following this vehicle motion, infection state of each area changes according to the transition rules. The column "1-end" of the figure illustrates the result of such changes at the end of time step 1. Note that the area (3, 3) becomes infected because there were two adjacent infected areas and the vehicle was also in an adjacent area, three areas in total. In time step 2, you move your vehicle to the west and position it at (2, 3). Then infection states of other areas change. Note that even if your vehicle had exactly three infected adjacent areas (west, southwest, and south), the area that is being visited by the vehicle is not infected. The result of such changes at the end of time step 2 is as depicted in "2-end". Finally, in time step 3, you move your vehicle to the east. After the change of the infection states, you see that all the areas have become virus free! This completely disinfected situation is the goal. In the scenario we have seen, you have successfully disinfected all the areas in three time steps by commanding the vehicle to move (1) southwest, (2) west, and (3) east. Your mission is to find the length of the shortest sequence(s) of vehicle motion commands that can successfully disinfect all the areas. Input The input is a sequence of datasets. The end of the input is indicated by a line containing a single zero. Each dataset is formatted as follows. n a11 a12 ... a1n a21 a22 ... a2n ... an1 an2 ... ann Here, n is the size of the grid. That means that the grid is comprised of n × n areas. You may assume 1 ≤ n ≤ 5. The rest of the dataset consists of n lines of n letters. Each letter aij specifies the state of the area at the beginning: '#' for infection, '.' for free of virus, and '@' for the initial location of the vehicle. The only character that can appear in a line is '#', '.', or '@'. Among n × n areas, there exists exactly one area which has '@'. Output For each dataset, output the minimum number of time steps that is required to disinfect all the areas. If there exists no motion command sequence that leads to complete disinfection, output -1. The output should not contain any other extra character. Examples Input 3 ... .@. ... 3 .## .#. @## 3 ##. #.. @.. 5 ....@ ##... #.... ...#. ##.## 5 #...# ...#. #.... ...## ..@.. 5 #.... ..... ..... ..... ..@.. 5 #..#. #.#.# .#.#. ....# .#@## 5 ..##. ..#.. #.... #.... .#@.. 0 Output 0 10 -1 3 2 1 6 4 Input 3 ... .@. ... 3 .## .#. @## 3 . .. @.. 5 ....@ ... .... ...#. .## 5 ...# ...#. .... ...## ..@.. 5 .... ..... ..... ..... ..@.. 5 ..#. .#.# .#.#. ....# .#@## 5 ..##. ..#.. .... .... .#@.. 0 Output 0 10 -1 3 2 1 6 4 "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**13 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) def main(): rr = [] oks = collections.defaultdict(set) def a2k(a, n): r = 0 for i in range(n): for j in range(n): r *= 3 r += a[i][j] return r def k2a(k, n): a = [] for i in range(n): t = [] for j in range(n): t.append(k%3) k //= 3 a.append(t[::-1]) return a def moves(a,n): si = sj = -1 for i in range(n): for j in range(n): if a[i][j] == 2: si = i sj = j break if si >= 0: break r = set() a[si][sj] = 0 for i in range(max(0,si-1), min(n,si+2)): for j in range(max(0,sj-1), min(n,sj+2)): if a[i][j] != 0 or (si == i and sj == j): continue a[i][j] = 2 na = [[0]*n for _ in range(n)] zf = 1 for k in range(n): for l in range(n): if a[k][l] == 2: continue c = 0 for m in range(max(0, k-1), min(n, k+2)): for o in range(max(0, l-1), min(n, l+2)): if m == k and o == l: continue if a[m][o] > 0: c += 1 if (a[k][l] == 0 and c == 3) or (a[k][l] == 1 and 2 <= c <= 3): na[k][l] = 1 zf = 0 na[i][j] = 2 if zf == 1: return 'ok' r.add(a2k(na, n)) a[i][j] = 0 return r def f(n): sd = {} sd['.'] = 0 sd['#'] = 1 sd['@'] = 2 a = [[sd[c] for c in S()] for _ in range(n)] zf = 1 for i in range(n): for j in range(n): if a[i][j] == 1: zf = 0 break if zf == 1: return 0 r = inf d = collections.defaultdict(lambda: inf) k = a2k(a,n) q = set([k]) d[k] = 0 t = 0 while q: t += 1 nq = set() if q & oks[n]: return t for k in q: a = k2a(k,n) r = moves(a,n) if r == 'ok': oks[n].add(k) return t for nk in r: if d[nk] > t: d[nk] = t nq.add(nk) q = nq return -1 while 1: n = I() if n == 0: break rr.append(f(n)) return '\n'.join(map(str, rr)) print(main()) ```
87,467
Provide a correct Python 3 solution for this coding contest problem. The earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully confined the spread of the infection within a square grid of areas. Recently, public health specialists found an interesting pattern with regard to the transition of infected areas. At each step in time, every area in the grid changes its infection state according to infection states of its directly (horizontally, vertically, and diagonally) adjacent areas. * An infected area continues to be infected if it has two or three adjacent infected areas. * An uninfected area becomes infected if it has exactly three adjacent infected areas. * An area becomes free of the virus, otherwise. Your mission is to fight against the virus and disinfect all the areas. The Ministry of Health lets an anti-virus vehicle prototype under your command. The functionality of the vehicle is summarized as follows. * At the beginning of each time step, you move the vehicle to one of the eight adjacent areas. The vehicle is not allowed to move to an infected area (to protect its operators from the virus). It is not allowed to stay in the same area. * Following vehicle motion, all the areas, except for the area where the vehicle is in, change their infection states according to the transition rules described above. Special functionality of the vehicle protects its area from virus infection even if the area is adjacent to exactly three infected areas. Unfortunately, this virus-protection capability of the vehicle does not last. Once the vehicle leaves the area, depending on the infection states of the adjacent areas, the area can be infected. The area where the vehicle is in, which is uninfected, has the same effect to its adjacent areas as an infected area as far as the transition rules are concerned. The following series of figures illustrate a sample scenario that successfully achieves the goal. Initially, your vehicle denoted by @ is found at (1, 5) in a 5 × 5-grid of areas, and you see some infected areas which are denoted by #'s. <image> Firstly, at the beginning of time step 1, you move your vehicle diagonally to the southwest, that is, to the area (2, 4). Note that this vehicle motion was possible because this area was not infected at the start of time step 1. Following this vehicle motion, infection state of each area changes according to the transition rules. The column "1-end" of the figure illustrates the result of such changes at the end of time step 1. Note that the area (3, 3) becomes infected because there were two adjacent infected areas and the vehicle was also in an adjacent area, three areas in total. In time step 2, you move your vehicle to the west and position it at (2, 3). Then infection states of other areas change. Note that even if your vehicle had exactly three infected adjacent areas (west, southwest, and south), the area that is being visited by the vehicle is not infected. The result of such changes at the end of time step 2 is as depicted in "2-end". Finally, in time step 3, you move your vehicle to the east. After the change of the infection states, you see that all the areas have become virus free! This completely disinfected situation is the goal. In the scenario we have seen, you have successfully disinfected all the areas in three time steps by commanding the vehicle to move (1) southwest, (2) west, and (3) east. Your mission is to find the length of the shortest sequence(s) of vehicle motion commands that can successfully disinfect all the areas. Input The input is a sequence of datasets. The end of the input is indicated by a line containing a single zero. Each dataset is formatted as follows. n a11 a12 ... a1n a21 a22 ... a2n ... an1 an2 ... ann Here, n is the size of the grid. That means that the grid is comprised of n × n areas. You may assume 1 ≤ n ≤ 5. The rest of the dataset consists of n lines of n letters. Each letter aij specifies the state of the area at the beginning: '#' for infection, '.' for free of virus, and '@' for the initial location of the vehicle. The only character that can appear in a line is '#', '.', or '@'. Among n × n areas, there exists exactly one area which has '@'. Output For each dataset, output the minimum number of time steps that is required to disinfect all the areas. If there exists no motion command sequence that leads to complete disinfection, output -1. The output should not contain any other extra character. Examples Input 3 ... .@. ... 3 .## .#. @## 3 ##. #.. @.. 5 ....@ ##... #.... ...#. ##.## 5 #...# ...#. #.... ...## ..@.. 5 #.... ..... ..... ..... ..@.. 5 #..#. #.#.# .#.#. ....# .#@## 5 ..##. ..#.. #.... #.... .#@.. 0 Output 0 10 -1 3 2 1 6 4 Input 3 ... .@. ... 3 .## .#. @## 3 . .. @.. 5 ....@ ... .... ...#. .## 5 ...# ...#. .... ...## ..@.. 5 .... ..... ..... ..... ..@.. 5 ..#. .#.# .#.#. ....# .#@## 5 ..##. ..#.. .... .... .#@.. 0 Output 0 10 -1 3 2 1 6 4 "Correct Solution: ``` from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write dd0 = ((-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1)) DD = [] for k in range(6): ddk = [] for y in range(k): for x in range(k): v = 0 for dx, dy in dd0: nx = x + dx; ny = y + dy if not 0 <= nx < k or not 0 <= ny < k: continue v |= 1 << (ny*k + nx) ddk.append(v) DD.append(ddk) L = (1 << 16) bc = [0]*L for v in range(1, L): bc[v] = bc[v ^ (v & -v)] + 1 def solve(): N = int(readline()) if N == 0: return False dd = dd0 dk = DD[N] state = 0 for i in range(N): s = readline().strip() for j, c in enumerate(s): if c == '#': state |= 1 << (N*i + j) elif c == '@': sx = j; sy = i U = {(state, sx, sy): 0} que = deque([(state, sx, sy)]) while que: state, x, y = key = que.popleft() d = U[key] if state == 0: write("%d\n" % d) break for dx, dy in dd: nx = x + dx; ny = y + dy if not 0 <= nx < N or not 0 <= ny < N: continue b = 1 << (ny*N + nx) if state & b: continue state ^= b n_state = 0 for k in range(N*N): v = state & dk[k] if state & (1 << k): if v and 2 <= bc[v // (v & -v)] <= 3: n_state |= (1 << k) else: if v and bc[v // (v & -v)] == 3: n_state |= (1 << k) if n_state & b: n_state ^= b n_key = (n_state, nx, ny) if n_key not in U: U[n_key] = d+1 que.append(n_key) state ^= b else: write("-1\n") return True while solve(): ... ```
87,468
Provide a correct Python 3 solution for this coding contest problem. We understand that reading English is a great pain to many of you. So we’ll keep this problem statememt simple. Write a program that reports the point equally distant from a set of lines given as the input. In case of no solutions or multiple solutions, your program should report as such. Input The input consists of multiple datasets. Each dataset is given in the following format: n x1,1 y1,1 x1,2 y1,2 x2,1 y2,1 x2,2 y2,2 ... xn,1 yn,1 xn,2 yn,2 n is the number of lines (1 ≤ n ≤ 100); (xi,1, yi,1) and (xi,2, yi,2) denote the different points the i-th line passes through. The lines do not coincide each other. The coordinates are all integers between -10000 and 10000. The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed. Output For each dataset, print a line as follows. If there is exactly one point equally distant from all the given lines, print the x- and y-coordinates in this order with a single space between them. If there is more than one such point, just print "Many" (without quotes). If there is none, just print "None" (without quotes). The coordinates may be printed with any number of digits after the decimal point, but should be accurate to 10-4. Example Input 2 -35 -35 100 100 -49 49 2000 -2000 4 0 0 0 3 0 0 3 0 0 3 3 3 3 0 3 3 4 0 3 -4 6 3 0 6 -4 2 3 6 6 -1 2 -4 6 0 Output Many 1.5000 1.5000 1.000 1.000 "Correct Solution: ``` import sys readline = sys.stdin.readline write = sys.stdout.write EPS = 1e-9 def line_cross_point(P1, P2, Q1, Q2): x0, y0 = P1; x1, y1 = P2 x2, y2 = Q1; x3, y3 = Q2 dx0 = x1 - x0; dy0 = y1 - y0 dx1 = x3 - x2; dy1 = y3 - y2 s = (y0-y2)*dx1 - (x0-x2)*dy1 sm = dx0*dy1 - dy0*dx1 if -EPS < sm < EPS: return None return x0 + s*dx0/sm, y0 + s*dy0/sm def bisector(P1, P2, Q1, Q2): x0, y0 = P1; x1, y1 = P2 x2, y2 = Q1; x3, y3 = Q2 dx0 = x1 - x0; dy0 = y1 - y0 dx1 = x3 - x2; dy1 = y3 - y2 cp = line_cross_point(P1, P2, Q1, Q2) if cp is None: return None cx, cy = cp d0 = (dx0**2 + dy0**2)**.5 d1 = (dx1**2 + dy1**2)**.5 return [ ((cx, cy), (cx + (dx0*d1 + dx1*d0), cy + (dy0*d1 + dy1*d0))), ((cx, cy), (cx + (dx0*d1 - dx1*d0), cy + (dy0*d1 - dy1*d0))), ] def line_point_dist2(p1, p2, q): x, y = q x1, y1 = p1; x2, y2 = p2 dx = x2 - x1; dy = y2 - y1 dd = dx**2 + dy**2 sv = (x - x1) * dy - (y - y1) * dx return abs(sv / dd**.5) def check(LS, q): ds = [line_point_dist2(p1, p2, q) for p1, p2 in LS] return all(abs(ds[0] - e) < EPS for e in ds) def solve(): N = int(readline()) if N == 0: return False P = [] for i in range(N): x1, y1, x2, y2 = map(int, readline().split()) P.append(((x1, y1), (x2, y2))) if N <= 2: write("Many\n") return True s = [] for i in range(N): p1, p2 = P[i] for j in range(i): q1, q2 = P[j] bs = bisector(p1, p2, q1, q2) if bs is None: continue s.append(bs) if len(s) > 1: break else: continue break if len(s) < 2: write("None\n") return True ans = [] b1, b2 = s for p1, p2 in b1: for q1, q2 in b2: cp = line_cross_point(p1, p2, q1, q2) if cp is None: continue if check(P, cp): cx, cy = cp for ax, ay in ans: if abs(cx - ax) < EPS and abs(cy - ay) < EPS: break else: ans.append(cp) if len(ans) == 0: write("None\n") elif len(ans) > 1: write("Many\n") else: write("%.16f %.16f\n" % ans[0]) return True while solve(): ... ```
87,469
Provide a correct Python 3 solution for this coding contest problem. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No "Correct Solution: ``` import bisect n=int(input()) a=sorted(list(map(int,input().split()))) d=[0]*(n+1) for i in range(1,n+1):d[i]=d[i-1]+a[i-1] m=int(input()) b,c=[list(map(int,input().split())) for _ in [0,0]] [print(['Yes','No'][d[bisect.bisect_right(a,b[i])]<c[i]]) for i in range(m)] ```
87,470
Provide a correct Python 3 solution for this coding contest problem. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No "Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) a.sort() import bisect c = list(map(int, input().split())) d = [0] * (n+1) for i in range(n): d[i+1] = a[i] for i in range(n): d[i+1] += d[i] for i in range(m): n = bisect.bisect_right(a,b[i]) n = d[n] if c[i] <= n: print("Yes") else: print("No") ```
87,471
Provide a correct Python 3 solution for this coding contest problem. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No "Correct Solution: ``` from itertools import accumulate from bisect import bisect_right as br n = int(input()) alst = sorted(map(int, input().split())) acc = list(accumulate(alst)) m = int(input()) blst = list(map(int, input().split())) clst = list(map(int, input().split())) for b, c in zip(blst, clst): index = br(alst, b) print(["No", "Yes"][(acc[index - 1] if index > 0 else 0) >= c]) ```
87,472
Provide a correct Python 3 solution for this coding contest problem. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No "Correct Solution: ``` import bisect n=int(input()) a=sorted(list(map(int,input().split()))) d=[0]*(n+1) for i in range(1,n+1):d[i]=d[i-1]+a[i-1] m=int(input()) b,c=[list(map(int,input().split())) for _ in [0,0]] for i in range(m): p=bisect.bisect_right(a,b[i]) print(['Yes','No'][d[p]<c[i]]) ```
87,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright : @Huki_Hara # Created : 2015-03-14 n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) c = list(map(int, input().split())) l = [True] * m for i in range(m): sum = 0 for j in range(n): if a[j] <= b[i]: sum += a[j] if sum < c[i]: l[i] = False for i in range(m): if l[i] : print("Yes") else: print("No") ``` No
87,474
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) c = map(int, input().split()) l = [False] * m a.sort() for i in range(m): sum = 0 for j in range(n): if a[j] > b[i]: break sum += a[j] if sum >= c.__next__(): l[i] = True for i in range(m): if l[i]: print("Yes") else: print("No") ``` No
87,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No Submitted Solution: ``` input() a=list(map(int,input().split())) m=int(input()) b,c=[list(map(int,input().split())) for _ in [0,0]] [print(['Yes','No'][sum([j for j in a if j<=b[i]])<c[i]])for i in range(m)] ``` No
87,476
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright : @Huki_Hara # Created : 2015-03-14 n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) c = list(map(int, input().split())) l = [] i = 0 while i < m: sum = 0 j = 0 while j < n: if a[j] <= b[i]: sum += a[j] j += 1 i += 1 l.append(sum) i = 0 while i < m: if l[i] >= c[i]: print("Yes") else: print("No") i += 1 ``` No
87,477
Provide a correct Python 3 solution for this coding contest problem. D: Sunburn-Suntan- story Aizunyan is a second-year student who belongs to the programming contest club of Wakagamatsu High School, commonly known as the Prokon club. Cute like an angel. Aizu Nyan is planning to participate in this summer festival, so I made a schedule for the band to go to listen to. I'm worried about sunburn here. All live performances are held outdoors, but Aizu Nyan has a constitution that makes it easy to get sunburned, so if you are exposed to too much ultraviolet rays outdoors for a long time, you will get sunburned immediately. I plan to avoid UV rays as much as possible by evacuating indoors while there is no live performance, but during the live performance, it will inevitably hit the sun. Therefore, Aizu Nyan thought about taking measures against ultraviolet rays by applying sunscreen. problem If you apply sunscreen, you can get the effect for T minutes from the time you apply it. Sunscreen can only be applied once, so I want to use it effectively. Aizu Nyan is outdoors from the start time to the end time of the live, and is indoors at other times. You'll be given a live schedule that Aizu Nyan will listen to, so find the maximum amount of time you can get the sunscreen effect while you're outdoors. Input format The input can be given in the following format. T N s_1 t_1 ... s_N t_N The first line is given an integer T that represents the time it takes to get the sunscreen effect. The second line is given an integer N that represents the number of live concerts Aizu Nyan listens to. The following N lines are given the integer s_i, which represents the start time of the live that Aizu Nyan listens to thi, and the integer t_i, which represents the end time, separated by spaces. Constraint * 1 ≤ T ≤ 10 ^ {15} * 1 ≤ N ≤ 10 ^ 5 * 0 ≤ s_i <t_i ≤ 10 ^ {15} (1 ≤ i ≤ N) * The start time of the (i + 1) th live is the same as or later than the end time of the i-th live. That is, t_i ≤ s_ {i + 1} (1 ≤ i <N) output Print one line for the maximum amount of time you can get the sunscreen effect while you're outdoors. Input example 1 20 1 0 10 Output example 1 Ten Input example 2 20 1 0 100 Output example 2 20 Input example 3 9 3 1 5 9 11 13 20 Output example 3 7 Input example 4 twenty five Five 9 12 15 20 21 25 28 40 45 60 Output example 4 twenty one Example Input 20 1 0 10 Output 10 "Correct Solution: ``` # AOJ 2801: Suntan # Python3 2018.7.11 bal4u from bisect import bisect_left T, N = int(input()), int(input()) a, s, t = [0], [0], [0] for i in range(N): _s, _t = map(int, input().split()) s.append(_s) t.append(_t) a.append(a[-1]+_t-_s) s.append(1<<62) t.append(1<<62) ans = 0 k = bisect_left(t, T, 0)-1 for i in range(1, N+1): x = s[i]+T k = bisect_left(t, x, k)-1 y = a[k]-a[i-1] if x > s[k+1]: y += x-s[k+1] if y > ans: ans = y; if ans == T: break print(ans) ```
87,478
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` n=int(input()) tf=list(map(str,input().split())) tf_r=tf[::-1] while n!=1: if tf_r[-1]=="T" and tf_r[-2]=="F": del tf_r[-2:] tf_r.append("F") n-=1 else: del tf_r[-2:] tf_r.append("T") n-=1 print(tf_r[0]) ```
87,479
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` n=int(input()) p=list(map(str,input().split())) m=p[0] for i in range(1,n): if m+p[i]=="TF": m="F" else: m="T" print(m) ```
87,480
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` from functools import reduce def M(x, y): if x == 'T' and y == 'F': return 'F' else: return 'T' _ = input() P = input().split() print(reduce(M, P)) ```
87,481
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` n = int(input()) li = input().split() bef = li[0] for i in range(1, n): bef = "F" if bef == "T" and li[i] == "F" else "T" print(bef) ```
87,482
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` def inpl(): return list(map(int, input().split())) N = int(input()) P = input().split() def calc(a, b): if a == "T" and b == "F": return "F" else: return "T" a = P[0] for b in P[1:]: a = calc(a, b) print(a) ```
87,483
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` from functools import reduce def M(x, y): if x == 'T' and y == 'T': return 'T' elif x == 'T' and y == 'F': return 'F' elif x == 'F' and y == 'T': return 'T' else: return 'T' _ = input() P = input().split() print(reduce(M, P)) ```
87,484
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` N=int(input()) kekka=list(input().split()) #print(kekka) base=kekka[0] #result=0 for i in range(1,N): if base=="F": base="T" #print(1) elif base=="T": if kekka[i]=="T": base="T" #print(2) else: base="F" #print(3) print(base) ```
87,485
Provide a correct Python 3 solution for this coding contest problem. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = SR() return l mod = 1000000007 #A """ h,w = LI() a,b = LI() y = h//a x = w//b ans = h*w-a*y*b*x print(ans) """ #B def m(x,y): if x == y: return "T" if x == "F": return "T" return "F" n = I() p = input().split() ans = p[0] for i in range(1,n): ans = m(ans,p[i]) print(ans) #C """ s = [[1]*8 for i in range(4)] for i in range(4): s.insert(2*i,[1,0,1,0,1,0,1,0]) for j in range(8): for i in range(1,8): s[j][i] += s[j][i-1] for j in range(8): for i in range(1,8): s[i][j] += s[i-1][j] s.insert(0,[0,0,0,0,0,0,0,0,0]) for i in range(1,9): s[i].insert(0,0) q = I() for _ in range(q): a,b,c,d = LI() print(s[c][d]-s[c][b-1]-s[a-1][d]+s[a-1][b-1]) """ #D """ n = I() a = LI() dp = [float("inf") for i in range(n)] b = [[] for i in range(n)] ind = [0 for i in range(100001)] for i in range(n): k = bisect.bisect_left(dp,a[i]) dp[k] = a[i] b[k].append(a[i]) ind[a[i]] = max(i,ind[a[i]]) for i in range(n): if dp[i] == float("inf"):break b[i].sort() b[i] = b[i][::-1] i -= 1 ans = b[i][0] now_ind = ind[b[i][0]] now_v = b[i][0] while i >= 0: for j in b[i]: if ind[j] < now_ind and j < now_v: ans += j now_ind = ind[j] now_v = j i -= 1 print(ans) """ #E #F #G #H #I #J #K #L #M #N #O #P #Q #R #S #T ```
87,486
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T Submitted Solution: ``` def M(i,j): return "F" if i=="T" and j=="F" else "T" n = int(input()) p = input().split() ret = p[0] for i in range(1,n): ret = M(ret,p[i]) print(ret) ``` Yes
87,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T Submitted Solution: ``` # /usr/bin/python # -*- coding: utf-8 -*- import sys def machine(x,y): if x=='T' and y=='F': return 'F' else: return 'T' N = int(input()) Pn = list(map(str, sys.stdin.readline().rstrip().split())) for i in range(1, N): Pn[i] = machine(Pn[i-1], Pn[i]) print(Pn[-1]) ``` Yes
87,488
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T Submitted Solution: ``` N = int(input()) P = input().split() if P[0]=="T" and P[1]=="F": out = "F" else: out = "T" for i in range(N-2): if out == "T" and P[i+2] == "F": out = "F" else: out = "T" print(out) ``` Yes
87,489
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2 "Correct Solution: ``` N, M = map(int, input().split()) A = [input() for _ in range(N)] B = [input() for _ in range(N)] ans = 0 for a, b in zip(A, B): for aa, bb in zip(a, b): if aa != bb: ans += 1 print(ans) ```
87,490
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): N, M = LI() A = SR(N) B = SR(N) ans = 0 for i in range(N): for j in range(M): if A[i][j] != B[i][j]: ans += 1 print(ans) return if __name__ == "__main__": solve() ```
87,491
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2 "Correct Solution: ``` N, M = map(int, input().split()) table1 = '' table2 = '' for i in range(2 * N): if i < N: table1 += input() else: table2 += input() count = 0 for i, j in zip(table1, table2): if i != j: count+=1 print(count) ```
87,492
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2 "Correct Solution: ``` n,m = map(int,input().split()) a = [list(list(input())) for i in range(n)] b = [list(list(input())) for i in range(n)] cout = 0 for i in range(n): for j in range(m): if a[i][j] != b[i][j]: cout += 1 print(cout) ```
87,493
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` #中国人郵便問題 import sys input = sys.stdin.readline INF = float('inf') v, e = map(int, input().split()) adj = [[INF]*v for _ in range(v)] for i in range(v): adj[i][i] = 0 odd_vertices = [0]*v total_d = 0 for _ in range(e): s, t, d = map(int, input().split()) adj[s][t] = adj[t][s] = min(adj[s][t], d) odd_vertices[s] ^= 1 odd_vertices[t] ^= 1 total_d += d def warshall(): for k in range(v): for i in range(v): for j in range(v): if adj[i][k] != INF and adj[k][j] != INF: adj[i][j] = min(adj[i][j],adj[i][k] + adj[k][j]) def solve(): if sum(odd_vertices) == 0: return total_d else: warshall() odd = [] for i in range(v): if odd_vertices[i] == 1: odd += [i] n = len(odd) dp = [INF]*(1<<n) dp[0] = 0 for state in range(1<<n): for sp in range(n): if not((state>>sp) & 1): for ep in range(n): d = adj[odd[sp]][odd[ep]] if sp != ep and not((state>>ep) & 1) and d != INF: new_state = state|(1<<sp)|(1<<ep) dp[new_state] = min(dp[new_state], dp[state]+d) return total_d + dp[-1] print(solve()) ```
87,494
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` def warshall_floyd(distance_table, point_size): for k in range(point_size): for i in range(point_size): for j in range(point_size): if distance_table[i][j] > distance_table[i][k] + distance_table[k][j]: distance_table[i][j] = distance_table[i][k] + distance_table[k][j] class bit: def __createtable(): table = [None] * 64 mask64 = (1 << 64) - 1 hash = 0x03F566ED27179461 for i in range(64): table[hash >> 58] = i hash = (hash << 1) & mask64 return table __table = __createtable() def number_of_trailing_zeros(x): if x == 0:return 64 mask64 = (1 << 64) - 1 return bit.__table[((bit.lowest_one(x) * 0x03F566ED27179461) & mask64) >> 58] def lowest_one(i): return i & -i def ccp(distance_table, point_size, v): if v: i = bit.number_of_trailing_zeros(v) v ^= (1 << i) return min(ccp(distance_table, point_size, v ^ (1 << j)) + distance_table[i][j] for j in range(point_size) if v & 1 << j) else: return 0 import sys readline = sys.stdin.readline point_size, e = map(int, readline().split()) distance_table = [[float('inf')] * point_size for _ in range(point_size)] cost = 0 v = 0 for _ in range(e): s, t, d = map(int, readline().split()) distance_table[s][t] = min(distance_table[s][t], d) distance_table[t][s] = min(distance_table[t][s], d) v ^= 1 << s ^ 1 << t cost += d warshall_floyd(distance_table, point_size) print(cost + ccp(distance_table, point_size, v)) ```
87,495
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` import sys f_i = sys.stdin V, E = map(int, f_i.readline().split()) # adjacency matrix no_edge = float("inf") adj = [[no_edge] * V for i in range(V)] for i in range(V): adj[i][i] = 0 odd_b = 0 # bit DP to record odd vertex ans = 0 # acceptance of input for l_i in f_i: s, t, d = map(int, l_i.split()) t_d = min(d, adj[s][t]) adj[s][t] = t_d adj[t][s] = t_d odd_b ^= 1 << s odd_b ^= 1 << t ans += d if odd_b: # Warshall???Floyd Algorithm for k in range(V): for i in range(V): for j in range(V): adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]) # Minimum weighted matching mw = [no_edge] * (odd_b + 1) mw[0] = 0 digits = len(bin(odd_b)) - 2 for b in range(odd_b): for i in range(0, digits): if not (b & (1 << i)) and odd_b & (1 << i): for j in range(i + 1, digits): if not (b & (1 << j)) and odd_b & (1 << j): t_b = b + (1 << i) + (1 << j) if t_b == t_b & odd_b: t_w = mw[t_b] mw[t_b] = min(t_w, mw[b] + adj[i][j]) # Output ans += mw[odd_b] print(ans) else: print(ans) ```
87,496
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` def warshall_floyd(n, dists): prev = [t.copy() for t in dists] for k in range(n): current = [[0] * n for _ in range(n)] prev_k = prev[k] for i in range(n): prev_i, current_i = prev[i], current[i] prev_i_k = prev_i[k] for j in range(n): current_i[j] = min(prev_i[j], prev_i_k + prev_k[j]) prev = current return prev def solve(n, links, total_d, odd_vertices): if not odd_vertices: return total_d d_table = warshall_floyd(n, links) d_table = [[d for oj, d in enumerate(d_table[oi]) if oj in odd_vertices] for ni, oi in enumerate(odd_vertices)] ndt = len(d_table) bit_dict = {1 << i: i for i in range(ndt)} def minimum_pair(remains): if not remains: return 0 b = remains & -remains remains ^= b i = bit_dict[b] return min(minimum_pair(remains ^ (1 << j)) + d_table[i][j] for j in range(ndt) if remains & (1 << j)) return total_d + minimum_pair((1 << ndt) - 1) v, e = map(int, input().split()) dists = [[float('inf')] * v for _ in range(v)] for i in range(v): dists[i][i] = 0 odd_vertices = [0] * v total_d = 0 for _ in range(e): s, t, d = map(int, input().split()) dists[s][t] = min(dists[s][t], d) dists[t][s] = min(dists[t][s], d) odd_vertices[s] ^= 1 odd_vertices[t] ^= 1 total_d += d print(solve(v, dists, total_d, [i for i, v in enumerate(odd_vertices) if v])) ```
87,497
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` import math import sys from typing import List def warshall_floyd(table: List[List[int]], vertex_num: int) -> None: for k in range(vertex_num): for i in range(vertex_num): for j in range(vertex_num): table[i][j] = min(table[i][j], table[i][k] + table[k][j]) if __name__ == "__main__": V, E = map(lambda x: int(x), input().split()) adj = [[sys.maxsize] * V for i in range(V)] for i in range(V): adj[i][i] = 0 odd_b = 0 # bit DP to record odd vertex ans = 0 for _ in range(E): s, t, d = map(lambda x: int(x), input().split()) min_d = min(d, adj[s][t]) adj[s][t] = min_d adj[t][s] = min_d odd_b ^= 1 << s odd_b ^= 1 << t ans += d if odd_b: warshall_floyd(adj, V) # Minimum weighted matching mw = [sys.maxsize] * (odd_b + 1) mw[0] = 0 digits = int(math.log2(odd_b)) + 1 for b, mw_b in enumerate(mw): if 1 == b % 2: continue for i in range(0, digits): if b & (1 << i): continue for j in range(i + 1, digits): if b & (1 << j): continue t_b = b + (1 << i) + (1 << j) if t_b == t_b & odd_b: t_w = mw[t_b] mw[t_b] = min(t_w, mw_b + adj[i][j]) ans += mw[odd_b] print(ans) else: print(ans) ```
87,498
Provide a correct Python 3 solution for this coding contest problem. For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * The route must go through every edge at least once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ |E| ≤ 1,000 * 0 ≤ di ≤ 1,000 * si ≠ ti * The graph is connected Input |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge). Note that there can be multiple edges between a pair of vertices. Output Print the shortest distance in a line. Examples Input 4 4 0 1 1 0 2 2 1 3 3 2 3 4 Output 10 Input 4 5 0 1 1 0 2 2 1 3 3 2 3 4 1 2 5 Output 18 Input 2 3 0 1 1 0 1 2 0 1 3 Output 7 "Correct Solution: ``` #!python3 iim = lambda: map(int, input().rstrip().split()) from heapq import heappush, heappop def resolve(): V, E = iim() inf = float("inf") ad = [[inf]*V for i in range(V)] bi = [1<<i for i in range(V)] ans = 0 odd = 0 for i in range(E): s, t, d = iim() ad[s][t] = ad[t][s] = min(d, ad[s][t]) odd ^= bi[s] odd ^= bi[t] ans += d if odd == 0: print(ans) return for _ in range(V): nochage = True for i in range(V): for j in range(i+1, V): for k in range(V): d1 = ad[i][j] d2 = ad[i][k] + ad[k][j] if d1 > d2: nochage = False ad[i][j] = d2 ad[j][i] = d2 if nochage: break v2 = [i for i in range(V) if odd & bi[i]] n2 = len(v2) dp = {0: 0} dk = [0] for i in range(n2): ii = bi[i] vi = v2[i] for j in range(i + 1, n2): jj = bi[j] vj = v2[j] for state in dk: ij = ii | jj if state & ij: continue st2 = state | ij co2 = dp[state] + ad[vi][vj] if st2 in dp: if co2 < dp[st2]: dp[st2] = co2 else: dp[st2] = co2 dk.append(st2) ans += dp[(1<<n2)-1] print(ans) if __name__ == "__main__": resolve() ```
87,499