message
stringlengths
2
57.2k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
61
108k
cluster
float64
22
22
__index_level_0__
int64
122
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}.
instruction
0
54,520
22
109,040
Tags: math, number theory Correct Solution: ``` mod = 1000000009 def inv(x): return pow(x,mod - 2, mod) n, a, b, k = map(int, input().split()) s = input() q = pow(b, k, mod) * inv(pow(a, k, mod)) t = 0 for i in range(k): sgn = 1 if s[i] == '+' else -1 t += sgn * pow(a, n - i, mod) * pow(b, i, mod) t %= mod t += mod t %= mod print(((t * (1 - pow(q, (n + 1) // k, mod)) * inv(1 - q) % mod) + mod) % mod if q != 1 else (t * (n + 1) // k) % mod) ```
output
1
54,520
22
109,041
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}.
instruction
0
54,521
22
109,042
Tags: math, number theory Correct Solution: ``` n, a, b, k = map(int, input().split() ) s = input() mod = (int)( 1e9 + 9 ) ans = 0 t = (int)( (n + 1) / k ) def modInverse(a): return pow(a, mod - 2, mod) i = 0 for ind in range( len(s) ): first = ( pow(a, n - i, mod ) * pow( b, i, mod ) ) % mod num = ( pow( b, k, mod ) * modInverse( pow(a, k, mod) ) ) % mod if ( b >= a ) : den = ( num - 1 + mod ) % mod num = ( pow( num, t, mod ) - 1 + mod ) % mod else: den = ( 1 - num + mod ) % mod num = ( 1 - pow( num, t, mod ) + mod ) % mod if num == 0: temp = ( t * first ) % mod else: temp = ( num * modInverse(den) ) % mod temp = ( temp * first ) % mod if s[ind] == '+': ans = ( ans + temp )%mod elif s[ind] == '-': ans = ( ans - temp + mod ) % mod i += 1 if ( i > n ): i = 0 print (ans) ```
output
1
54,521
22
109,043
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}.
instruction
0
54,522
22
109,044
Tags: math, number theory Correct Solution: ``` MOD = 1000000009 def inv(n): return pow(n, MOD - 2, MOD) n, a, b, k = map(int, input().split()) q = (n + 1) // k string = input() s = [] for char in string: if char == "+": s.append(1) else: s.append(-1) res = 0 # final answer for i in range(k): res += (s[i] * pow(a, n - i, MOD) * pow(b, i, MOD)) res %= MOD n1 = pow(b, k, MOD) n2 = pow(a, k, MOD) n2 = inv(n2) T = n1 * n2 % MOD if a != b and T != 1: num = (pow(b, n + 1, MOD) - pow(a, n + 1, MOD)) % MOD # numerator d1 = (pow(b, k, MOD) - pow(a, k, MOD)) % MOD d1 = inv(d1) d2 = pow(a, n + 1 - k, MOD) d2 = inv(d2) R = (num * d1 * d2) % MOD res = (res * R) % MOD print(res) else: res *= q res %= MOD print(res) ```
output
1
54,522
22
109,045
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}.
instruction
0
54,523
22
109,046
Tags: math, number theory Correct Solution: ``` def pow_mod(x, y, p): number = 1 while y: if y & 1: number = number * x % p y >>= 1 x = x * x % p return number % p def inv(x, p): if 1 < x: return p - inv(p % x, x) * p // x return 1 def v(p, a, b, k): i = 1 while (pow_mod(a, k, (10 ** 9 + 9) ** i) - pow_mod(b, k, (10 ** 9 + 9) ** i)) == 0: i += 1 return i-1 def main(): p = 10 ** 9 + 9 n, a, b, k = map(int, input().split()) S = list(input()) for i in range(k): if S[i] == '+': S[i] = 1 else: S[i] = -1 s=0 if a != b: vp = p ** v(p, a, b, k) sum_mod = ((pow_mod(a, (n + 1), p * vp) - pow_mod(b, (n + 1), p * vp)) // vp) * inv(((pow_mod(a, k, p * vp) - pow_mod(b, k, p * vp)) // vp) % p, p) pa = pow_mod(a, k - 1, p) pb = 1 inv_a = inv(a, p) for i in range(k): s += (S[i] * pa * pb * sum_mod) % p pa = (pa * inv_a) % p pb = (pb * b) % p else: for i in range(k): s += S[i] * (n + 1) // k s *= pow_mod(a, n, p) s %= p print(s) main() ```
output
1
54,523
22
109,047
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}.
instruction
0
54,524
22
109,048
Tags: math, number theory Correct Solution: ``` MOD = int(1e9+9) n, a, b, k = map(int, input().split()) s = input() def solve(): res = 0 q = pow(b, k, MOD) * pow(pow(a, k, MOD), MOD-2, MOD) % MOD max_pow = pow(a, n, MOD) c = b * pow(a, MOD-2, MOD) % MOD for i in range(k): res += max_pow if s[i] == '+' else -max_pow res = (res % MOD + MOD) % MOD max_pow = max_pow * c % MOD t = (n + 1) // k if q == 1: return t * res % MOD z = (pow(q, t, MOD) - 1) * pow(q-1, MOD-2, MOD) % MOD return z * res % MOD print(solve()) ```
output
1
54,524
22
109,049
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}. Submitted Solution: ``` MOD = 1000000009 def inv(n): return pow(n, MOD - 2, MOD) n, a, b, k = map(int, input().split()) q = (n + 1) // k string = input() s = [] for char in string: if char == "+": s.append(1) else: s.append(-1) res = 0 # final answer for i in range(k): res += (s[i] * pow(a, n - i, MOD) * pow(b, i, MOD)) res %= MOD n1 = pow(b, k, MOD) n2 = pow(a, k, MOD) n2 = inv(n2) T = n1 * n2 % MOD if a != b or T != 1: num = (pow(b, n + 1, MOD) - pow(a, n + 1, MOD)) % MOD # numerator d1 = (pow(b, k, MOD) - pow(a, k, MOD)) % MOD d1 = inv(d1) d2 = pow(a, n + 1 - k, MOD) d2 = inv(d2) R = (num * d1 * d2) % MOD res = (res * R) % MOD print(res) else: R = q * T res *= R res %= MOD print(res) ```
instruction
0
54,525
22
109,050
No
output
1
54,525
22
109,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}. Submitted Solution: ``` mod = 10 ** 9 + 9 def gcd(a, b): if a == 0: return b, 0, 1 d0, x0, y0 = gcd(b % a, a) x = y0 - (b // a) * x0 y = x0 return d0, x, y def pow(a, k): if k == 0: return 1 if k == 1: return a if k % 2 == 1: return (pow(a, k - 1) * a) % mod return (pow(a, k // 2) ** 2) % mod n, a, b, k = map(int, input().split()) s = input() pa = [1] pb = [1] for i in range(k): pa.append((pa[-1] * a) % mod) pb.append((pb[-1] * b) % mod) ak = pa[-1] ans = pow(ak, n + 1 - k) sk = 0 for i in range(k): if s[i] == '+': sk += (pb[k - i - 1] * pa[i] * ans) % mod else: sk -= (pb[k - i - 1] * pa[i] * ans) % mod while sk < 0: sk += mod inva = list(gcd(ak, mod))[1] inva = inva % mod while inva < 0: inva += mod ans = 0 bk = pb[k] ak = inva q = (ak * bk) % mod c = (n + 1) // k if q == 1: print((sk * c) % mod) exit(0) if q == 0: print(sk) exit(0) qn = pow(q, c) invq = list(gcd(q - 1, mod))[1] % mod while invq < 0: invq += mod print((sk * (qn - 1) * invq) % mod) ```
instruction
0
54,526
22
109,052
No
output
1
54,526
22
109,053
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}. Submitted Solution: ``` n, a, b, k = [int(i) for i in input().split()] st = input() l = (n + 1) // k s = 0 mod = 1000000009 def f_pow(a, k): if k == 0: return 1 if k % 2 == 1: return f_pow(a, k - 1) * a % mod else: return f_pow(a * a % mod, k // 2) % mod def rev(b): return f_pow(b, mod - 2) for i in range(len(st)): sgn = 1 - 2 * (st[i] == '-') if a == b: s = (s + sgn * f_pow(a, n) * k) % mod else: g1 = f_pow(b, i) * f_pow(a, n - i) q = f_pow(b, k) * rev(f_pow(a, k)) res = g1 * (f_pow(q, l) - 1) * rev(q - 1) s = (s + sgn * res) % mod print(s) ```
instruction
0
54,527
22
109,054
No
output
1
54,527
22
109,055
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≀ i ≀ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i} by 10^{9} + 9. Note that the modulo is unusual! Input The first line contains four integers n, a, b and k (1 ≀ n ≀ 10^{9}, 1 ≀ a, b ≀ 10^{9}, 1 ≀ k ≀ 10^{5}). The second line contains a sequence of length k consisting of characters '+' and '-'. If the i-th character (0-indexed) is '+', then s_{i} = 1, otherwise s_{i} = -1. Note that only the first k members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integer β€” value of given expression modulo 10^{9} + 9. Examples Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 Note In the first example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = 2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2} = 7 In the second example: (βˆ‘ _{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 ≑ 999999228 \pmod{10^{9} + 9}. Submitted Solution: ``` MOD = 1000000009 def inv(n): return pow(n, MOD - 2, MOD) n, a, b, k = map(int, input().split()) q = (n + 1) // k string = input() s = [] for char in string: if char == "+": s.append(1) else: s.append(-1) res = 0 # final answer for i in range(k): res += (s[i] * pow(a, n - i, MOD) * pow(b, i, MOD)) res %= MOD n1 = pow(b, k, MOD) n2 = pow(a, k, MOD) n2 = inv(n2) T = n1 * n2 % MOD if a != b or T != 1: num = (pow(b, n + 1, MOD) - pow(a, n + 1, MOD)) % MOD # numerator d1 = (pow(b, k, MOD) - pow(a, k, MOD)) % MOD d1 = inv(d1) d2 = pow(a, n + 1 - k, MOD) d2 = inv(d2) R = (num * d1 * d2) % MOD res = (res * R) % MOD print(res) else: res *= q res %= MOD print(res) ```
instruction
0
54,528
22
109,056
No
output
1
54,528
22
109,057
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,941
22
109,882
Tags: binary search, brute force, math, number theory Correct Solution: ``` tests_number = int(input()) for _ in range(tests_number): l, r, m = list(map(int, input().split())) # m = n * a + b - c # m + c - b = n * a left_border = max(1, m + l - r) right_border = m + r - l result = (0, 0, 0) for a in range(l, r + 1): L = (left_border + a - 1) // a R = right_border // a if L <= R: s = L * a - m b = l c = s + b if c < l: dlt = l - c c += dlt b += dlt result = (a, b, c) break a, b, c = result # n * a + b - c = m n = (m - b + c) assert (n % a == 0) n //= a assert(n > 0) print(a, b, c) ```
output
1
54,941
22
109,883
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,942
22
109,884
Tags: binary search, brute force, math, number theory Correct Solution: ``` import sys input = sys.stdin.readline for _ in range(int(input())): l, r, m = map(int, input().split()) low = l - r high = r - l for a in range(l, r+1): lo = 1 hi = (m+high)//a + 2 while lo < hi: mi = (lo + hi) // 2 diff = m - mi * a if low <= diff <= high: if diff < 0: print(a, l, l - diff) else: print(a, r, r - diff) break elif diff < low: hi = mi else: lo = mi + 1 else: continue break ```
output
1
54,942
22
109,885
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,943
22
109,886
Tags: binary search, brute force, math, number theory Correct Solution: ``` for _ in range(int(input())): l,r,m = map(int,input().split()) rem = r - l for i in range(l,r+1): rem1 = m % i if rem1 <= rem and m >= i: print(i,l+rem1,l) break elif rem1 >= i - rem: print(i,l,l+i-rem1) break ```
output
1
54,943
22
109,887
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,944
22
109,888
Tags: binary search, brute force, math, number theory Correct Solution: ``` for t in range(int(input())): l, r, m = [int(x) for x in input().strip().split()] #print (l,l+m,l) d=r-l for i in range(l,r+1): #peint(i,l,m) v=m%i if v<=d and m>=i: print(i,l+v,l) break elif v>=i-d: #print(v) print(i,l,l+i-v) break ```
output
1
54,944
22
109,889
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,945
22
109,890
Tags: binary search, brute force, math, number theory Correct Solution: ``` import random def find_bc(diff, l, r): if diff > 0: return (r, r - diff) elif diff == 0: return (l, l) else: return (l, l - diff) def solve(l, r, m): starter = [l, l + 1, l + 2, r - 2, r - 1, r] for a in starter: n_1, n_2 = max(1, m // a), (m // a) + 1 for n in (n_1, n_2): if abs(n * a - m) <= (r - l): print (a, *find_bc(m - (n * a), l, r)) return for a in reversed(range(l, r + 1)): n_1, n_2 = max(1, m // a), (m // a) + 1 for n in (n_1, n_2): if abs(n * a - m) <= (r - l): print (a, *find_bc(m - (n * a), l, r)) return def run(): for t in range(int(input())): solve(*map(int, input().split())) run() ```
output
1
54,945
22
109,891
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,946
22
109,892
Tags: binary search, brute force, math, number theory Correct Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- testcases=int(input()) for j in range(testcases): l,r,m=map(int,input().split()) #find the smallest remainder for s in range(l,r+1): #ok i need to consider 2 cases r1=m%s r2=(-m)%s if s<=m: a=s b1=l+r1 c1=l ans=[a,b1,c1] if l<=b1 and b1<=r: print(*ans) break b2=l c2=l+r2 if l<=c2 and c2<=r: ans=[a,b2,c2] print(*ans) break else: #m<s n=1 a=s b=l c=l+(s-m) if l<=c and c<=r: ans=[a,b,c] print(*ans) break continue ```
output
1
54,946
22
109,893
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,947
22
109,894
Tags: binary search, brute force, math, number theory Correct Solution: ``` for _ in range(int(input())): l, r, m = map(int, input().split()) if l == r == m: print(l, r, m) continue for a in range(l, r + 1): x = m % a y = a - x if m > a: if x <= r - l: print(a, r, r - x) break elif y <= r - l: print(a, r - y, r) break if m < a: print(a, l, l + y) break ```
output
1
54,947
22
109,895
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer.
instruction
0
54,948
22
109,896
Tags: binary search, brute force, math, number theory Correct Solution: ``` tq = int(input()) for _ in range(tq): l, r, m = map(int, input().split()) mn = l-r mx = r-l b, c = 0, 0 for a in range(l, r+1): d = m%a if d >= mn and d <= mx and m >= a: b = l c = b-d if b < l or c < l or b > r or c > r: b = r c = b-d if b >= l and c >= l and b <= r and c <= r: print(a, b, c) break d = -1*(a-d) if d >= mn and d <= mx: b = l c = b-d if b < l or c < l or b > r or c > r: b = r c = b-d if b >= l and c >= l and b <= r and c <= r: print(a, b, c) break ```
output
1
54,948
22
109,897
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` # Author: S Mahesh Raju # Username: maheshraju2020 # Date: 19/07/2020 from sys import stdin,stdout from math import gcd, ceil, sqrt from collections import Counter from bisect import bisect_left, bisect_right ii1 = lambda: int(stdin.readline().strip()) is1 = lambda: stdin.readline().strip() iia = lambda: list(map(int, stdin.readline().strip().split())) isa = lambda: stdin.readline().strip().split() mod = 1000000007 def solve(l, r, m): for i in range(l, r + 1): if m % i == 0: return [i, l, l] else: need = m % i if r - l >= need and m - need > 0: return [i, r, r - need] elif i - need <= r - l: return [i, r - (i - need), r] tc = ii1() for _ in range(tc): l, r, m = iia() if l != r: print(*solve(l, r, m)) else: print(l, l, l) ```
instruction
0
54,949
22
109,898
Yes
output
1
54,949
22
109,899
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` #!/usr/bin/env python3 import io import os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) t = oint() """ m = na + b - c m - na = b - c l <= a, b, c <= r """ for _ in range(t): l, r, m = rint() for a in range(l, r+1): n1 = m//a n2 = n1 + 1 if n1 == 0: n1 += 1 diff1 = m - n1*a diff2 = m - n2*a if abs(diff1) < abs(diff2): n = n1 else: n = n2 diff = m - n*a if abs(diff) <= r-l: if diff > 0: b = r c = b-diff else: b = l c = b-diff break print(a, b, c) ```
instruction
0
54,950
22
109,900
Yes
output
1
54,950
22
109,901
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` t = int(input()) for case in range(t): l, r, m = map(int, input().split()) ans = [] for a in range(l, r + 1): mx = (m + r - l) // a mn = max(1, (m - r + l + a - 1) // a) if mx - mn >= 0: n = mx if m - n * a > 0: c = l b = m - n * a + c else: b = l c = n * a - m + b ans = [a, b, c] print(*ans) ```
instruction
0
54,951
22
109,902
Yes
output
1
54,951
22
109,903
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` for _ in range(int(input())): l, r, m = map(int, input().split()) for i in range(l, r + 1): n = (m + r - l) // i if n > 0 and n * i >= m + l - r: d = m - n * i if d < 0: print(i, r + d, r) else: print(i, l + d, l) break ```
instruction
0
54,952
22
109,904
Yes
output
1
54,952
22
109,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` for _ in range(int(input())): l,r,m=map(int,input().split()) if l==r: print(l,l,l) else: if m%r==0: print(r,r,r) elif m%l==0: print(l,l,l) else: for i in range(l,r): if (m%i)<=r-l: print(i,r,r - m%i) break elif i-(m%i)<=r-l: print(i,l,l + i -(m%i)) break ```
instruction
0
54,953
22
109,906
No
output
1
54,953
22
109,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` def solution(l,r,n): a = l while a <= r : rem = n % a rem = min(rem,a-rem) val = r - l q = n // a cond = True if q == 0 : cond = False if rem <= val : b = l c = b + rem if n % a == rem and cond : return a,c,b return a,b,c else: a = a + 1 for _ in range(int(input())): l,r,n = map(int,input().split()) print(*solution(l,r,n)) ```
instruction
0
54,954
22
109,908
No
output
1
54,954
22
109,909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` def do(): l, r, m = map(int,input().split()) low = max(0, m-abs(r-l)) high = m + abs(r-l) for a in range(l, r + 1): v1 = low / a v2 = high / a if v1.is_integer(): print(a, l, r) return if v2.is_integer(): print(a, r, l) return v1 = int(v1) v2 = int(v2) if (v2 - v1) != 0: diff = m - (a * v2) if diff == 0: print(a,l,l) return if diff > 0: print(a, l+diff, l) return if diff < 0: print(a, l, l + diff) return q = int(input()) for _ in range(q): do() ```
instruction
0
54,955
22
109,910
No
output
1
54,955
22
109,911
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l ≀ a,b,c ≀ r, and then he computes the encrypted value m = n β‹… a + b - c. Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that * a, b and c are integers, * l ≀ a, b, c ≀ r, * there exists a strictly positive integer n, such that n β‹… a + b - c = m. Input The first line contains the only integer t (1 ≀ t ≀ 20) β€” the number of test cases. The following t lines describe one test case each. Each test case consists of three integers l, r and m (1 ≀ l ≀ r ≀ 500 000, 1 ≀ m ≀ 10^{10}). The numbers are such that the answer to the problem exists. Output For each test case output three integers a, b and c such that, l ≀ a, b, c ≀ r and there exists a strictly positive integer n such that n β‹… a + b - c = m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Example Input 2 4 6 13 2 3 1 Output 4 6 5 2 2 3 Note In the first example n = 3 is possible, then n β‹… 4 + 6 - 5 = 13 = m. Other possible solutions include: a = 4, b = 5, c = 4 (when n = 3); a = 5, b = 4, c = 6 (when n = 3); a = 6, b = 6, c = 5 (when n = 2); a = 6, b = 5, c = 4 (when n = 2). In the second example the only possible case is n = 1: in this case n β‹… 2 + 2 - 3 = 1 = m. Note that, n = 0 is not possible, since in that case n is not a strictly positive integer. Submitted Solution: ``` from functools import reduce import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase input = lambda: sys.stdin.readline().rstrip("\r\n") def value():return tuple(map(int,input().split())) def arr():return [int(i) for i in input().split()] def sarr():return [i for i in input().split()] #----------------------------CODE------------------------------# t=int(input()) while t: t-=1 l,r,m=value() for i in range(l,r+1): if(m%i==0): print(i,r,r) break else: #print("Hello") res=m%i if(i-res<=r-l): #print("yes") print(i,r-(i-res),r) break if(res<=r-l): print(i,(r-res),r) break ```
instruction
0
54,956
22
109,912
No
output
1
54,956
22
109,913
Provide tags and a correct Python 3 solution for this coding contest problem. Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group. Jzzhu wonders how to get the maximum possible number of groups. Can you help him? Input A single integer n (1 ≀ n ≀ 105), the number of the apples. Output The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them. Examples Input 6 Output 2 6 3 2 4 Input 9 Output 3 9 3 2 4 6 8 Input 2 Output 0
instruction
0
55,161
22
110,322
Tags: constructive algorithms, number theory Correct Solution: ``` import math,sys,bisect,heapq from collections import defaultdict,Counter,deque from itertools import groupby,accumulate #sys.setrecursionlimit(200000000) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) 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)] MOD = 1000000000 + 7 def Y(c): print(["NO","YES"][c]) def y(c): print(["no","yes"][c]) def Yy(c): print(["No","Yes"][c]) def SieveOfEratosthenes(n): Z=[] prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): # Update all multiples of p for i in range(p * p, n+1, p): prime[i] = False p += 1 # Print all prime numbers for p in range(2, n): if prime[p]: Z.append(p) return Z N = int(input()) prime = SieveOfEratosthenes(N//2) if N <=3: print(0) else: mark = {} if N//2 not in prime: prime.append(N//2) Ans = [] prime = prime[::-1] for i in prime: Z= [] for j in range(1,N//i + 1): if mark.get(i*j,-1) == -1: Z.append(i*j) if len(Z) % 2 == 0: for k in Z: Ans.append(k) mark[k] = 1 else: for k in Z: if k!= 2*i: Ans.append(k) mark[k] = 1 print(len(Ans)//2) for i in range(0,len(Ans),2): print(Ans[i],Ans[i+1]) ```
output
1
55,161
22
110,323
Provide tags and a correct Python 3 solution for this coding contest problem. Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group. Jzzhu wonders how to get the maximum possible number of groups. Can you help him? Input A single integer n (1 ≀ n ≀ 105), the number of the apples. Output The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them. Examples Input 6 Output 2 6 3 2 4 Input 9 Output 3 9 3 2 4 6 8 Input 2 Output 0
instruction
0
55,162
22
110,324
Tags: constructive algorithms, number theory Correct Solution: ``` import math apples=int(input()) if apples<=3: print(0) else: halfpr=int(apples/2) def primes(n): primeslistsa=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317] yesnoprime=[False, False]+[True]*(n-1) t=0 while primeslistsa[t+1]<=int(math.sqrt(n)): t+=1 for x in range(t+1): for sa in range(2, int(n/primeslistsa[x]+1)): yesnoprime[primeslistsa[x]*sa]=False return yesnoprime primeslist=primes(halfpr) totallist=[False]*(apples+1) applepairs=[] for prime in range(len(primeslist)-1, 1, -1): if primeslist[prime]: numprimes=int(apples/prime) primesx=[int(i*prime) for i in range(1, numprimes+1) if not totallist[i*prime]] if len(primesx)%2==1: primesx.remove(2*prime) for pr in primesx: applepairs.append(pr) totallist[pr]=True print(int(len(applepairs)/2)) for t in range(int(len(applepairs)/2)): print(applepairs[2*t], applepairs[2*t+1]) ```
output
1
55,162
22
110,325
Provide tags and a correct Python 3 solution for this coding contest problem. Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group. Jzzhu wonders how to get the maximum possible number of groups. Can you help him? Input A single integer n (1 ≀ n ≀ 105), the number of the apples. Output The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them. Examples Input 6 Output 2 6 3 2 4 Input 9 Output 3 9 3 2 4 6 8 Input 2 Output 0
instruction
0
55,163
22
110,326
Tags: constructive algorithms, number theory Correct Solution: ``` apples=int(input()) if apples<=3: print(0) else: halfpr=int(apples/2) def primes(n): isPrime = [True for i in range(n + 1)] isPrime[0] = isPrime[1] = False idx = 2 while idx * idx <= n: if isPrime[idx]: for i in range(idx * 2, n, idx): isPrime[i] = False idx += 1 return isPrime primeslist=primes(halfpr) totallist=[False]*(apples+1) applepairs=[] for prime in range(len(primeslist)-1, 1, -1): if primeslist[prime]: numprimes=int(apples/prime) primesx=[int(i*prime) for i in range(1, numprimes+1) if not totallist[i*prime]] if len(primesx)%2==1: primesx.remove(2*prime) for pr in primesx: applepairs.append(pr) totallist[pr]=True print(int(len(applepairs)/2)) for t in range(int(len(applepairs)/2)): print(applepairs[2*t], applepairs[2*t+1]) ```
output
1
55,163
22
110,327
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group. Jzzhu wonders how to get the maximum possible number of groups. Can you help him? Input A single integer n (1 ≀ n ≀ 105), the number of the apples. Output The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them. Examples Input 6 Output 2 6 3 2 4 Input 9 Output 3 9 3 2 4 6 8 Input 2 Output 0 Submitted Solution: ``` apples=int(input()) lastprime=int(apples/2) totappls=[int(x) for x in range(2, apples+1)] sa=[] if apples<=3: print(0) else: def primes(n): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 # Copied code """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*int((n-i*i-1)/(2*i)+1) return [i for i in range(3,n,2) if sieve[i]]+[2] for prime in primes(lastprime+1): primelist=[int(prime*x) for x in range(1, int(apples/prime)+1) if prime*x in totappls] if len(primelist)%2==0: for t in range(int(len(primelist)/2)): sa.append(str(primelist[2*t])+' '+str(primelist[2*t+1])) else: primelist.remove(2*prime) for t in range(int(len(primelist)/2)): sa.append(str(primelist[2*t])+' '+str(primelist[2*t+1])) [totappls.remove(x) for x in primelist] print(len(sa)) for element in sa: print(element) ```
instruction
0
55,164
22
110,328
No
output
1
55,164
22
110,329
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,254
22
110,508
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` def main(): n = int(input()) l = list(map(int, input().split())) seive = [False, True] * max(l) a = len(seive) for i in range(3, int(a ** .5) + 1, 2): if seive[i]: for j in range(i * i, a, i): seive[j] = False i = l.count(1) if i: res = [1] * i for a in l: if a > 1 and seive[a + 1]: res.append(a) break if len(res) > 1: print(len(res)) print(*res) return l0, l1 = [], [] for a in l: if a != 1: if a & 1: for b in l0: if seive[a + b]: print(2) print(a, b) return l1.append(a) else: for b in l1: if seive[a + b]: print(2) print(a, b) return l0.append(a) print(1) print(l[0]) if __name__ == '__main__': main() ```
output
1
55,254
22
110,509
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,255
22
110,510
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` def primes_upto(limit): is_prime = [False] * 2 + [True] * (limit - 1) # remove odd numbers for n in range(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)`` if is_prime[n]: for i in range(n*n, limit+1, n): is_prime[i] = False return [i for i, prime in enumerate(is_prime) if prime] check = set(primes_upto(2*10**6 + 500)) n = int(input()) arr = list(map(int,input().split())) one_present = False cnt_ones = 0 for i in arr: if i == 1: one_present = True cnt_ones += 1 num1,num2,prime = 0,0,0 for i in range(n): for j in range(n): if one_present: if cnt_ones > 1: if arr[i] + arr[j] in check and arr[i] + 1 in check and arr[j] + 1 in check and arr[i] != 1 and arr[j] != 1: num1 = arr[i] num2 = arr[j] if arr[i] + 1 in check and arr[i] != 1: num1 = arr[i] if arr[i] in check: prime = arr[i] else: if arr[i] + arr[j] in check and arr[i] != 1 and arr[j] != 1: num1 = arr[i] num2 = arr[j] if arr[i] + 1 in check and arr[i] != 1: num1 = arr[i] if arr[i] in check: prime = arr[i] else: if arr[i] + arr[j] in check: num1 = arr[i] num2 = arr[j] if arr[i] in check: prime = arr[i] if one_present: if cnt_ones > 1: if num2 == 0: if num1 == 0: print(cnt_ones) for i in range(cnt_ones): print(1, end= ' ') else: print(cnt_ones + 1) for i in range(cnt_ones): print(1, end=' ') print(num1, end= ' ') else: print(cnt_ones + 2) for i in range(cnt_ones): print(i, end=' ') print(num1, end=' ') print(num2, end=' ') else: if num2 == 0: if num1 == 0: print(1) print(1) else: print(2) print('1 '+str(num1)) else: print(2) print(str(num1) + ' ' + str(num2)) else: if num1 == 0: if prime == 0: print(1) print(arr[0]) else: print(1) print(prime) else: print(2) print(str(num1) + ' ' + str(num2)) ```
output
1
55,255
22
110,511
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,256
22
110,512
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` def main(): input() l0, l1, ones = [], [], 0 for a in map(int, input().split()): if a == 1: ones += 1 else: (l1 if a & 1 else l0).append(a) seive = [False, True] * (((max(l0) if l0 else 0) + (max(l1) if l1 else 0)) // 2 + 1) a = len(seive) for i in range(3, int(a ** .5) + 1, 2): if seive[i]: for j in range(i * i, a, i): seive[j] = False if ones: res = ['1'] * ones for a in l0: if a > 1 and seive[a + 1]: res.append(str(a)) break if len(res) > 1: print(len(res)) print(' '.join(res)) return for a in l1: for b in l0: if seive[a + b]: print(2) print(a, b) return print(1) print(1 if ones else (l0 if l0 else l1)[0]) if __name__ == '__main__': main() ```
output
1
55,256
22
110,513
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,257
22
110,514
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True n = int(input()) arr = list(map(int,input().split())) count = {} for i in arr: try: count[i] += 1 except KeyError: count[i] = 1 numbers = list(count.keys()) numbers.sort() a,b = -1,-1 flag = False for i in range(len(numbers)-1): for j in range(i+1,len(numbers)): if isPrime(numbers[i]+numbers[j]): a = numbers[i] b = numbers[j] flag = True break if flag: break if a == b and a == -1: if numbers[0] == 1 and count[1] > 1: print(count[1]) for i in range(count[1]): print(1,end=' ') print() else: print(1) print(numbers[0]) elif a == 1: print(count[1]+1) for i in range(count[1]): print(1,end=' ') print(b) else: if numbers[0] == 1 and count[1] > 2: print(count[1]) for i in range(count[1]): print(1,end=' ') print() else: print(2) print(a,b) ```
output
1
55,257
22
110,515
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,258
22
110,516
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` import sys #import random from bisect import bisect_right as rb from collections import deque #sys.setrecursionlimit(10**8) from queue import PriorityQueue from math import * input_ = lambda: sys.stdin.readline().strip("\r\n") ii = lambda : int(input_()) il = lambda : list(map(int, input_().split())) ilf = lambda : list(map(float, input_().split())) ip = lambda : input_() fi = lambda : float(input_()) ap = lambda ab,bc,cd : ab[bc].append(cd) li = lambda : list(input_()) pr = lambda x : print(x) prinT = lambda x : print(x) f = lambda : sys.stdout.flush() mod = 10**9 + 7 mx = 2*10**6 + 10 prime = [1 for i in range (2*10**6 + 10)] prime[0] = 0 prime[1] = 0 p = 2 while (p*p)<mx : if (prime[p] == 1) : for i in range (p*p,mx,p) : prime[i] = 0 p+=1 n = ii() a = il() d = {} for i in a : d[i] = d.get(i,0) + 1 if (d.get(1) and d[1]>=2) : for i in range (n) : if (a[i] > 1 and prime[a[i] + 1] == 1) : print(d[1] + 1) for j in range (d[1]) : print(1,end = ' ') print(a[i]) exit(0) print(d[1]) t = 0 for i in range (d[1]) : print(1, end = ' ') exit(0) for i in range (n) : for j in range (i+1,n) : if (prime[a[i]+a[j]] == 1) : print(2) print(a[i],a[j]) exit(0) print(1) print(a[0]) ```
output
1
55,258
22
110,517
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,259
22
110,518
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` n = int(input()) L = list(map(int, input().split())) P = [-1 for _ in range(2000001)] def premier(n): if P[n] >= 0: return P[n] for i in range(2,int(n**0.5)+1): if n%i==0: P[n] = False return False P[n] = True return True e = L.count(1) if n == 1: print(1) print(L[0]) elif e > 1: L.sort() i = 1 while i < n and L[i] == 1: i += 1 u = i ok = 0 while i < n: if premier(L[i]+1): print(u+1) for j in range(u): print(1,end=" ") print(L[i]) ok = 1 break i += 1 if ok == 0: print(u) for i in range(u): print(1,end=" ") else: ok = 0 for i in range(n-1): for j in range(i+1,n): t = premier(L[i]+L[j]) if t: print(2) print(str(L[i])+" "+str(L[j])) ok = 1 break if ok: break if ok == 0: print(1) print(L[0]) ```
output
1
55,259
22
110,519
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,260
22
110,520
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` import sys def get_primes(n: int): from itertools import chain from array import array primes = {2, 3} is_prime = (array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0))*((n-1)//6)) for i in chain.from_iterable((range(5, n+1, 6), range(7, n+1, 6))): if is_prime[i]: primes.add(i) for j in range(i*3, n+1, i*2): is_prime[j] = 0 return is_prime, primes n = int(input()) a = list(map(int, input().split())) is_prime, primes = get_primes(2*10**6) one_count = a.count(1) if one_count > 1: for i in range(n): if a[i] != 1 and is_prime[a[i] + 1]: print(one_count + 1) print(*([1]*one_count + [a[i]])) exit() else: print(one_count) print(*([1] * one_count)) else: for i in range(n): for j in range(n): if i != j and is_prime[a[i] + a[j]]: print(2) print(a[i], a[j]) exit() else: print(1) print(a[0]) ```
output
1
55,260
22
110,521
Provide tags and a correct Python 3 solution for this coding contest problem. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83
instruction
0
55,261
22
110,522
Tags: constructive algorithms, greedy, number theory Correct Solution: ``` # =============================================================================================== # importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import ceil, floor from copy import * from collections import deque, defaultdict from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl from operator import * # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is returned # ============================================================================================== # fast I/O region BUFSIZE = 8192 from sys import stderr class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") # =============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### # =============================================================================================== # some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def nextline(): out("\n") # as stdout.write always print sring. def testcase(t): for p in range(t): solve() def pow(x, y, p): res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a # N=100000 # mod = 10**9 +7 # fac = [1, 1] # finv = [1, 1] # inv = [0, 1] # # for i in range(2, N + 1): # fac.append((fac[-1] * i) % mod) # inv.append(mod - (inv[mod % i] * (mod // i) % mod)) # finv.append(finv[-1] * inv[-1] % mod) # # # def comb(n, r): # if n < r: # return 0 # else: # return fac[n] * (finv[r] * finv[n - r] % mod) % mod ##############Find sum of product of subsets of size k in a array # ar=[0,1,2,3] # k=3 # n=len(ar)-1 # dp=[0]*(n+1) # dp[0]=1 # for pos in range(1,n+1): # dp[pos]=0 # l=max(1,k+pos-n-1) # for j in range(min(pos,k),l-1,-1): # dp[j]=dp[j]+ar[pos]*dp[j-1] # print(dp[k]) def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10] return list(accumulate(ar)) def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4] return list(accumulate(ar[::-1]))[::-1] def lcm(a,b): return (a*b)//gcd(a,b) # ========================================================================================= from collections import defaultdict def numberOfSetBits(i): i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 def N(): return int(inp()) def solve(): def primesbelow(N): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 # """ Input N>=6, Returns a list of primes, 2 <= p < N """ correction = N % 6 > 1 N = {0: N, 1: N - 1, 2: N + 4, 3: N + 3, 4: N + 2, 5: N + 1}[N % 6] sieve = [True] * (N // 3) sieve[0] = False for i in range(int(N ** .5) // 3 + 1): if sieve[i]: k = (3 * i + 1) | 1 sieve[k * k // 3::2 * k] = [False] * ((N // 6 - (k * k) // 6 - 1) // k + 1) sieve[(k * k + 4 * k - 2 * k * (i % 2)) // 3::2 * k] = [False] * ( (N // 6 - (k * k + 4 * k - 2 * k * (i % 2)) // 6 - 1) // k + 1) return [2, 3] + [(3 * i + 1) | 1 for i in range(1, N // 3 - correction) if sieve[i]] primes=set(primesbelow(2*(10**6))) n=N() ar=lis() c1=0 pm1=[] already=set() pr=[] found=[] for i in range(n): if ar[i]==1: c1+=1 if ar[i]+1 in primes and ar[i]>1: pm1.append(ar[i]) if ar[i] in primes: pr.append(ar[i]) for j in already: if ar[i]+j in primes: found.append((j,ar[i])) already.add(ar[i]) if c1>=3: if pm1: print(c1+1) print("1 "*c1 + str(pm1[0])) return else: print(c1) print("1 "*c1) return if c1==2: if pm1: print(c1+1) print("1 "*c1 + str(pm1[0])) return if found: print(2) print(found[0][0],found[0][1]) return print(1) print(ar[0]) solve() #testcase(int(inp())) ```
output
1
55,261
22
110,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` #import sys import math #sys.stdin = open('in', 'r') n = int(input()) a = [int(x) for x in input().split()] #n,m = map(int, input().split()) p1 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511] primes = dict(map(lambda x:(x, True), p1)) def isPrime(v): if v in primes: return primes[v] sqrti = math.sqrt(v) for i in range(len(p1)): if v % p1[i] == 0: primes[v] = False return False if p1[i] > sqrti: break primes[v] = True return True done=False cnt1 = sum(filter(lambda x: x == 1, a)) if cnt1 > 1: for i in range(n): if a[i] != 1 and isPrime(a[i] + 1): done=True print(cnt1 + 1) print(a[i],end=' ') print(str.join(' ', ['1']*cnt1)) break if not done: done = True print(cnt1) print(str.join(' ', ['1']*cnt1)) else: for i in range(n): for j in range(n): if i != j and isPrime(a[i]+a[j]): done = True print(2) print(f'{a[i]} {a[j]}') break if done: break if not done: print(1) print(a[0]) ```
instruction
0
55,262
22
110,524
Yes
output
1
55,262
22
110,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` from time import time n = int(input()) a = list(map(int, input().split())) start = time() cache = {} def is_prime(n): if n not in cache: cache[n] = _is_prime(n) return cache[n] def _is_prime(n): if n == 2 or n == 3: return True if n < 2 or n % 2 == 0: return False if n < 9: return True if n % 3 == 0: return False r = int(n ** 0.5) f = 5 while f <= r: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True s = {} i = 0 while i < len(a): if a[i] > 1 and a[i] in s: a.pop(i) else: s[a[i]] = True i += 1 p = [0] * len(a) for i in range(0, len(a)): for j in range(i + 1, len(a)): if not is_prime(a[i] + a[j]): p[i] += 1 p[j] += 1 while True: mx = max(p) if mx == 0: break mi = p.index(mx) for i in range(0, len(a)): if i == mi or not is_prime(a[mi] + a[i]): p[i] -= 1 a.pop(mi) p.pop(mi) print(len(a)) print(" ".join(map(str, a))) #print(time() - start) ```
instruction
0
55,263
22
110,526
Yes
output
1
55,263
22
110,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` n = int(input()) t = list(map(int, input().split())) k = t.count(1) s = ' '.join('1' * k) p = [0] + [1, 0] * 1000000 for i in range(3, 1415, 2): if p[i]: p[i * i::2 * i] = [0] * ((2000000 - i * i) // 2 // i + 1) if k > 1: for q in t: if q > 1 and p[1 + q]: exit(print(k + 1, q, s)) exit(print(k, s)) for i in range(n): for j in range(i + 1, n): if p[t[i] + t[j]]: exit(print(2, t[i], t[j])) print(1, t[0]) ```
instruction
0
55,264
22
110,528
Yes
output
1
55,264
22
110,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` import math n = input() a = list(map(int, input().split(" "))) result = list() def is_prime(n): if n % 2 == 0 and n > 2: return False return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2)) # take all one if possible has_one = False for m in a: if m == 1: result.append(m) has_one = True # if there is one, take any even number which satisfied if has_one == True: for m in a: if is_prime(m+1): result.append(m) break else: has_answer = False # if no one, the answer should be one odd and one even for i in range(len(a)): x = a[i] for j in range(len(a)): if i == j: continue y = a[j] if is_prime(x+y): result.append(x) result.append(y) has_answer = True break if has_answer == True: break if has_answer == False: # if no combination exist, just add one number result.append(a[i]) print(len(result)) print(" ".join(map(str,result))) ```
instruction
0
55,265
22
110,530
No
output
1
55,265
22
110,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` # =============================================================================================== # importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import ceil, floor from copy import * from collections import deque, defaultdict from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl from operator import * # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is returned # ============================================================================================== # fast I/O region BUFSIZE = 8192 from sys import stderr class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") # =============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### # =============================================================================================== # some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def nextline(): out("\n") # as stdout.write always print sring. def testcase(t): for p in range(t): solve() def pow(x, y, p): res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a # N=100000 # mod = 10**9 +7 # fac = [1, 1] # finv = [1, 1] # inv = [0, 1] # # for i in range(2, N + 1): # fac.append((fac[-1] * i) % mod) # inv.append(mod - (inv[mod % i] * (mod // i) % mod)) # finv.append(finv[-1] * inv[-1] % mod) # # # def comb(n, r): # if n < r: # return 0 # else: # return fac[n] * (finv[r] * finv[n - r] % mod) % mod ##############Find sum of product of subsets of size k in a array # ar=[0,1,2,3] # k=3 # n=len(ar)-1 # dp=[0]*(n+1) # dp[0]=1 # for pos in range(1,n+1): # dp[pos]=0 # l=max(1,k+pos-n-1) # for j in range(min(pos,k),l-1,-1): # dp[j]=dp[j]+ar[pos]*dp[j-1] # print(dp[k]) def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10] return list(accumulate(ar)) def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4] return list(accumulate(ar[::-1]))[::-1] def lcm(a,b): return (a*b)//gcd(a,b) # ========================================================================================= from collections import defaultdict def numberOfSetBits(i): i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 def N(): return int(inp()) def solve(): def primesbelow(N): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 # """ Input N>=6, Returns a list of primes, 2 <= p < N """ correction = N % 6 > 1 N = {0: N, 1: N - 1, 2: N + 4, 3: N + 3, 4: N + 2, 5: N + 1}[N % 6] sieve = [True] * (N // 3) sieve[0] = False for i in range(int(N ** .5) // 3 + 1): if sieve[i]: k = (3 * i + 1) | 1 sieve[k * k // 3::2 * k] = [False] * ((N // 6 - (k * k) // 6 - 1) // k + 1) sieve[(k * k + 4 * k - 2 * k * (i % 2)) // 3::2 * k] = [False] * ( (N // 6 - (k * k + 4 * k - 2 * k * (i % 2)) // 6 - 1) // k + 1) return [2, 3] + [(3 * i + 1) | 1 for i in range(1, N // 3 - correction) if sieve[i]] primes=set(primesbelow(2*(10**6))) n=N() ar=lis() c1=0 pm1=[] already=set() pr=[] found=[] for i in range(n): if ar[i]==1: c1+=1 if ar[i]+1 in primes and ar[i]>1: pm1.append(ar[i]) if ar[i] in primes: pr.append(ar[i]) for j in already: if ar[i]+j in primes: found.append((j,ar[i])) already.add(ar[i]) if c1>=3: if pm1: print(c1+1) print("1 "*c1 + str(pm1[0])) return else: print(c1) print("1 "*c1) return if c1==2: if pm1: print(c1+1) print("1 "*c1 + str(pm1[0])) return if found: print(2) print(found[0][0],found[0][1]) return if pr: print(1) print(pr[0]) return print(0) print() solve() #testcase(int(inp())) ```
instruction
0
55,266
22
110,532
No
output
1
55,266
22
110,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` import math def prime(n): if n % 2 == 0 and n > 2: return False return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2)) n = int(input()) l = list(map(int, input().split())) if l.count(1) > 1: for i in range(n): if l[i] != 1: if prime(1 + l[i]): print(1 + l.count(1)) for j in range(l.count(1)): print (1, end = ' ') print (l[i]) exit() for i in range(n): for j in range(i+1, n): if prime(l[i] + l[j]): print (2) print (l[i], l[j]) exit() print (1) print (l[0]) ```
instruction
0
55,267
22
110,534
No
output
1
55,267
22
110,535
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it. Input The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a. The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a. Output On the first line print integer m β€” the maximum possible size of simple subset of a. On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 Submitted Solution: ``` #### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque, Counter from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def print(*args, **kwargs): sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def si(): return str(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def lsi(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 5 FACT = [0] * maxN INV_FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def inverseFactorials(): n = len(INV_FACT) INV_FACT[n-1] = inverse(FACT[n-1]) for i in range(n-2, -1, -1): INV_FACT[i] = multiply(INV_FACT[i+1], i+1) def coeffBinom(n, k): if n < k: return 0 return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n-k])) ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ## END OF LIBRARIES ## ###################### def is_prime(n): if n == 1: return False i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True n = ii() a = li() a.sort() idx1 = 0 for el in a: if el == 1: idx1 += 1 count = 0 pari = list() dispari = list() for el in a: if el % 2 == 0: pari.append(el) else: dispari.append(el) s2 = list() s1 = list() st = list() flag = False if len(pari) > 0 and pari[0] == 2: s1.append(2) count = max(count,1) if a[0] == 1: for p in pari: if is_prime(p+1) == True: count = idx1+1 flag = True st.append(p) break for d in dispari: if d > 1 and is_prime(d) == True: s1.append(d) count = max(count,1) break for p in pari: for d in dispari: if is_prime(p+d): s2.append([p,d]) count = max(count,2) break if count != 0: print(count) if flag == True: num = st.pop() l = [1 for i in range(idx1)] l.append(num) print_list(l) else: if count == 1: res = s1.pop() print(res) elif count == 2: res = s2.pop() print_list(res) else: print(1) print(a[0]) ```
instruction
0
55,268
22
110,536
No
output
1
55,268
22
110,537
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` n, p = map(int, input().split()) s = input() ans = 0 if p == 2 or p == 5: for i in range(n): if int(s[i]) % p == 0: ans += i+1 else: mod = [-1 for i in range(p)] mod[0] = 0 mp = 0 m10 = 1 for i in range(n-1, -1, -1): mp = (mp + int(s[i]) * m10) % p mod[mp] += 1 m10 = (10 * m10) % p for m in mod: ans += m*(m+1)//2 print(ans) ```
instruction
0
55,398
22
110,796
Yes
output
1
55,398
22
110,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` n, p = map(int,input().split()) D = input() count = [0] * p out = 0 if 10 % p == 0: for i in range(n): if int(D[i]) % p == 0: out += i + 1 else: mod = 0 ten = 1 count[0] = 1 for i in range(n): mod = (mod + int(D[n-i-1]) * ten) % p count[mod] += 1 ten *= 10 % p for c in count: out += (c * (c - 1)) // 2 print(out) ```
instruction
0
55,404
22
110,808
No
output
1
55,404
22
110,809
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya likes to solve equations. Today he wants to solve (x~div~k) β‹… (x mod k) = n, where div and mod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, k and n are positive integer parameters, and x is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible x. Can you help him? Input The first line contains two integers n and k (1 ≀ n ≀ 10^6, 2 ≀ k ≀ 1000). Output Print a single integer x β€” the smallest positive integer solution to (x~div~k) β‹… (x mod k) = n. It is guaranteed that this equation has at least one positive integer solution. Examples Input 6 3 Output 11 Input 1 2 Output 3 Input 4 6 Output 10 Note The result of integer division a~div~b is equal to the largest integer c such that b β‹… c ≀ a. a modulo b (shortened a mod b) is the only integer c such that 0 ≀ c < b, and a - c is divisible by b. In the first sample, 11~div~3 = 3 and 11 mod 3 = 2. Since 3 β‹… 2 = 6, then x = 11 is a solution to (x~div~3) β‹… (x mod 3) = 6. One can see that 19 is the only other positive integer solution, hence 11 is the smallest one.
instruction
0
55,631
22
111,262
Tags: math Correct Solution: ``` n, k = map(int, input().split()) a = [] for i in range(1, min(int(n ** 0.5) + 1, k)): if n % i == 0: a.append((i, n // i)) ans = int(1e10) for i in range(len(a)): if a[i][0] * k + a[i][1] < ans and a[i][1] < k: ans = a[i][0] * k + a[i][1] if a[i][1] * k + a[i][0] < ans and a[i][0] < k: ans = a[i][1] * k + a[i][0] print(ans) ```
output
1
55,631
22
111,263
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya likes to solve equations. Today he wants to solve (x~div~k) β‹… (x mod k) = n, where div and mod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, k and n are positive integer parameters, and x is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible x. Can you help him? Input The first line contains two integers n and k (1 ≀ n ≀ 10^6, 2 ≀ k ≀ 1000). Output Print a single integer x β€” the smallest positive integer solution to (x~div~k) β‹… (x mod k) = n. It is guaranteed that this equation has at least one positive integer solution. Examples Input 6 3 Output 11 Input 1 2 Output 3 Input 4 6 Output 10 Note The result of integer division a~div~b is equal to the largest integer c such that b β‹… c ≀ a. a modulo b (shortened a mod b) is the only integer c such that 0 ≀ c < b, and a - c is divisible by b. In the first sample, 11~div~3 = 3 and 11 mod 3 = 2. Since 3 β‹… 2 = 6, then x = 11 is a solution to (x~div~3) β‹… (x mod 3) = 6. One can see that 19 is the only other positive integer solution, hence 11 is the smallest one.
instruction
0
55,632
22
111,264
Tags: math Correct Solution: ``` n,k = map(int,input().split()) mn = 10**9 for i in range(1,k): if n % i == 0: mn = min(mn, n//i*k + i) print(mn) ```
output
1
55,632
22
111,265
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya likes to solve equations. Today he wants to solve (x~div~k) β‹… (x mod k) = n, where div and mod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, k and n are positive integer parameters, and x is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible x. Can you help him? Input The first line contains two integers n and k (1 ≀ n ≀ 10^6, 2 ≀ k ≀ 1000). Output Print a single integer x β€” the smallest positive integer solution to (x~div~k) β‹… (x mod k) = n. It is guaranteed that this equation has at least one positive integer solution. Examples Input 6 3 Output 11 Input 1 2 Output 3 Input 4 6 Output 10 Note The result of integer division a~div~b is equal to the largest integer c such that b β‹… c ≀ a. a modulo b (shortened a mod b) is the only integer c such that 0 ≀ c < b, and a - c is divisible by b. In the first sample, 11~div~3 = 3 and 11 mod 3 = 2. Since 3 β‹… 2 = 6, then x = 11 is a solution to (x~div~3) β‹… (x mod 3) = 6. One can see that 19 is the only other positive integer solution, hence 11 is the smallest one.
instruction
0
55,633
22
111,266
Tags: math Correct Solution: ``` import sys import math import itertools import functools import collections import operator import fileinput import copy ORDA = 97 def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) n, k = mi() d = sorted(divs(n)) for item in d: if item < k: c = item print(c + k * (n // c)) ```
output
1
55,633
22
111,267
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya likes to solve equations. Today he wants to solve (x~div~k) β‹… (x mod k) = n, where div and mod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, k and n are positive integer parameters, and x is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible x. Can you help him? Input The first line contains two integers n and k (1 ≀ n ≀ 10^6, 2 ≀ k ≀ 1000). Output Print a single integer x β€” the smallest positive integer solution to (x~div~k) β‹… (x mod k) = n. It is guaranteed that this equation has at least one positive integer solution. Examples Input 6 3 Output 11 Input 1 2 Output 3 Input 4 6 Output 10 Note The result of integer division a~div~b is equal to the largest integer c such that b β‹… c ≀ a. a modulo b (shortened a mod b) is the only integer c such that 0 ≀ c < b, and a - c is divisible by b. In the first sample, 11~div~3 = 3 and 11 mod 3 = 2. Since 3 β‹… 2 = 6, then x = 11 is a solution to (x~div~3) β‹… (x mod 3) = 6. One can see that 19 is the only other positive integer solution, hence 11 is the smallest one.
instruction
0
55,634
22
111,268
Tags: math Correct Solution: ``` a,k=map(int,input().split()) l=[] for i in range(1,round(a**0.5)+1): if a%i==0: if a//i<k: #print([i,a//i]) l.append([i,a//i]) if i<k: #print([a//i,i]) l.append([a//i,i]) #print() b=[] for i in l: b.append(i[0]*k+i[1]) print(min(b)) ```
output
1
55,634
22
111,269