Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
def C(n, r): ret = 1 for i in range(r): ret = ret * (n - i) // (i + 1); return ret def f(N): N = [int(ch) for ch in reversed(str(N))] cnt, nonzero = 0, 0 for k in range(len(N)-1, -1, -1): if N[k] > 0: for i in range(4 - nonzero): cnt += C(k, i) * pow(9, i) nonzero += 1 for i in range(4 - nonzero): cnt += (N[k] - 1) * C(k, i) * pow(9, i) if nonzero > 3: break return cnt for run in range(int(input())): l, r = list(map(int, input().split())) print(f(r+1) - f(l))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
def C(n, r): ret = 1 for i in range(r): ret = ret * (n - i) // (i + 1); return ret def f(N): N = [int(ch) for ch in reversed(str(N))] cnt, nonzero = 0, 0 for k in range(len(N)-1, -1, -1): if N[k] > 0: for i in range(4 - nonzero): cnt += C(k, i) * pow(9, i) nonzero += 1 for i in range(4 - nonzero): cnt += (N[k] - 1) * C(k, i) * pow(9, i) if nonzero > 3: break return cnt for run in range(int(input())): l, r = map(int, input().split()) print(f(r+1) - f(l))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
def cnt(l, c): if l <= c: return 10**c res = 1 if c > 0: res += l*9 if c > 1: res += l*(l-1)*9*9//2 if c > 2: res += l*(l-1)*(l-2)*9*9*9//6 return res def calc(n, c): x=str(n) xn = len(x) if xn <= c: return n+1 res = cnt(xn-1, c)+1 d = int(x[0]) if d > 1: res += cnt(xn-1, c-1)*(d - 1) if c > 1: for i in range(1,xn): d = int(x[i]) if d != 0: res += calc(int(x[i:]), c-1)-1 break return res """ans=0 for i in range(90000): s = str(i) if len(s)-s.count('0') < 3: ans+=1 print(ans)""" #ans=calc(1000000,3) # 15850 t = int(input()) for i in range(t): l,r = map(int, input().split()) print(calc(r,3)-calc(l-1,3))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
from bisect import bisect_left as bl c=[] def gen(n,nz): if len(n)>=19: return nonlocal c c.append(int(n)) if nz==3: n+="0" gen(n,nz) return gen(n+"0",nz) for i in ("123456789"): gen(n+i,nz+1) for i in ("123456789"): gen(i,1) c.append(10**18) c.sort() n=int(input()) for i in range(n): a,b=list(map(int,input().split())) x=min(bl(c,b),len(c)-1) y=bl(c,a) if x==y and b<c[x]: print(0) elif (c[x]==b and c[y]==a) or c[x]==b: print(x-y+1) else: print(x-y)
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import math def combi(L,n): return math.factorial(L)//math.factorial(n)//math.factorial(L-n) T=int(input()) LR=[list(map(int,input().split())) for i in range(T)] def classy(less,nonzero,now): L=len(str(now)) if less==0: if 3-nonzero>=L: return 10**L else: ANS=1 for i in range(1,3-nonzero+1): ANS+=combi(L,i)*(9**i) return ANS else: if nonzero<=2: if now<=9: return now+1 else: if int(str(now)[0])==1: return classy(1,nonzero+1,int(str(now)[1:]))\ +classy(0,nonzero,int("9"*(L-1)))\ else: return classy(1,nonzero+1,int(str(now)[1:]))\ +(int(str(now)[0])-1)*classy(0,nonzero+1,int("9"*(L-1)))\ +classy(0,nonzero,int("9"*(L-1))) else: return 1 for l,r in LR: print(classy(1,0,r)-classy(1,0,l-1))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import math def combi(L,n): return math.factorial(L)//math.factorial(n)//math.factorial(L-n) T=int(input()) LR=[list(map(int,input().split())) for i in range(T)] def classy(less,nonzero,now): L=len(str(now)) if less==0: if 3-nonzero>=L: return 10**L else: ANS=1 for i in range(1,3-nonzero+1): ANS+=combi(L,i)*(9**i) return ANS else: if nonzero<=2: if now<=9: return now+1 else: if int(str(now)[0])==1: return classy(1,nonzero+1,int(str(now)[1:]))\ +classy(0,nonzero,int("9"*(L-1)))\ else: return classy(1,nonzero+1,int(str(now)[1:]))\ +(int(str(now)[0])-1)*classy(0,nonzero+1,int("9"*(L-1)))\ +classy(0,nonzero,int("9"*(L-1))) else: return 1 for l,r in LR: print(classy(1,0,r)-classy(1,0,l-1))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
dp = [[-1]*20 for _ in range(5)] def dfs(pos, cnt, limited, dp, nums): if cnt > 3: return 0 if pos == -1: return 1 if not limited and dp[cnt][pos] != -1: return dp[cnt][pos] upper = nums[pos] if limited else 9 tmp = 0 for i in range(upper + 1): tmp += dfs(pos - 1, cnt + (i > 0), limited&(i==upper), dp, nums) if not limited: dp[cnt][pos] = tmp return tmp def classy(num): nums = [] while num: nums.append(num % 10) num //= 10 return dfs(len(nums) - 1, 0, 1, dp, nums) def __starting_point(): T = int(input()) for _ in range(T): L, R = map(int, input().split()) print(classy(R) - classy(L-1)) __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import sys class Solution(): mem = dict() def classy_Numbers(self): T = int(sys.stdin.readline().strip()) self.queries = [] for t in range(T): left, right = list(map(int, sys.stdin.readline().strip().split())) print(self.dp(right, 3) - self.dp(left-1, 3)) def dp(self, target, limit): num_digits = len(str(target)) if (target, limit) in self.mem: return self.mem[(target, limit)] if limit == 0: return 1 if num_digits <= limit: return target + 1 top_digit = target // (10 ** (num_digits-1)) res = target % (10 ** (num_digits-1)) ret = self.dp(res, limit-1) \ + max(top_digit-1, 0) * self.dp(10**(num_digits-1)-1, limit-1) \ + self.dp(10**(num_digits-1)-1, limit) self.mem[(target, limit)] = ret return ret sol = Solution() sol.classy_Numbers() # print(sol.dp(1, 3)) # print(sol.dp(1234, 4, 3)) # print(sol.dp(12345, 5, 3))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import math def check(x): cnt = 0 while(x): cnt = cnt + (x%10 != 0) x = math.floor(x/10) return cnt<=3 def bl(x): ans = 0 for i in range(1,x+1): if check(i): ans = ans+1 return ans def jc(x): sm = 1 for i in range(1,x+1): sm = sm * i return sm def c(x,y): if x<y: return 0 return jc(x)/(jc(x-y)*jc(y)) def cal1(x,y): ans = 0 for i in range(1,min(x,y)+1): ans = ans + c(x,i)*(9**i) return ans+1 def revers(x): ans = 0 while(x): ans = ans*10+ x%10 x = x.__floordiv__(10) return ans def cal2(x): rx = revers(x) ans = 0 cnt = 0 l = 0 l_ = 0 while(x): l = l+1 x = x.__floordiv__(10) while(rx): now = rx % 10 rx = rx.__floordiv__(10) l_ = l_ + 1 if now!=0: cnt = cnt+1 else: continue ans = ans + (now-1)*cal1(l-l_,3-cnt) + cal1(l-l_,3-cnt+1) if cnt>=3: break return ans T = int(input()) for i in range(T): x,y = list(map(int,input().split())) print(int(cal2(y)-cal2(x-1)))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
from itertools import combinations all_classy = [] def all_possible(a): if len(a) == 0: return [['0']*19] lower = all_possible(a[1:]) ans = [] for l in lower: for x in range(1, 10): this = l[:] this[a[0]] = str(x) ans.append(this) return ans for i in range(1, 4): for a in combinations(range(19), i): this_possible = all_possible(a) this_possible = [int(''.join(x)) for x in this_possible] all_classy += this_possible all_classy.sort() def ge(num): if num == 1: return 0 lower, upper = 0, len(all_classy) while lower < upper - 1: mid = (lower+upper)>>1 if(all_classy[mid] < num): lower = mid else: upper = mid return upper def le(num): lower, upper = 0, len(all_classy) while lower < upper-1: mid = (lower+upper)>>1 if all_classy[mid] > num: upper = mid else: lower = mid return lower q = int(input()) for i in range(q): l, r = map(int, input().strip().split()) x, y = ge(l), le(r) print(y-x+1)
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import sys MAX_N = 20 MAX_DIG = 3 dp = [[0] * (MAX_DIG + 1) for i in range(MAX_N)] def calc_dp(): dp[0][0] = 1 for i in range(1, MAX_N): dp[i][0] = 1 for j in range(MAX_DIG): dp[i][j + 1] += 9 * dp[i - 1][j] dp[i][j + 1] += dp[i - 1][j + 1] def first_dig(n): cnt = 0 while n >= 10: n //= 10 cnt += 1 return n, cnt def calc_ans(n): ans = 0 for n_digs in range(MAX_DIG, -1, -1): x, cnt = first_dig(n) for i in range(n_digs): ans += x * dp[cnt][i] ans += dp[cnt][n_digs] n -= x * 10 ** cnt return ans def main(): calc_dp() T = int(input()) for _ in range(T): l, r = map(int, input().split()) print(calc_ans(r) - calc_ans(l - 1) if l > 0 else 0) def __starting_point(): main() __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import sys MAX_N = 20 MAX_DIG = 3 dp = [[0] * (MAX_DIG + 1) for i in range(MAX_N)] def calc_dp(): dp[0][0] = 1 for i in range(1, MAX_N): dp[i][0] = 1 for j in range(MAX_DIG): dp[i][j + 1] += 9 * dp[i - 1][j] dp[i][j + 1] += dp[i - 1][j + 1] def first_dig(n): cnt = 0 while n >= 10: n //= 10 cnt += 1 return n, cnt def calc_ans(n): ans = 0 for n_digs in range(MAX_DIG, -1, -1): x, cnt = first_dig(n) for i in range(n_digs): ans += x * dp[cnt][i] ans += dp[cnt][n_digs] n -= x * 10 ** cnt return ans def main(): calc_dp() T = int(input()) for _ in range(T): l, r = map(int, input().split()) print(calc_ans(r) - calc_ans(l - 1) if l > 0 else 0) def __starting_point(): main() __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import sys MAX_N = 20 MAX_DIG = 3 dp = [[0] * (MAX_DIG + 1) for i in range(MAX_N)] def calc_dp(): dp[0][0] = 1 for i in range(1, MAX_N): dp[i][0] = 1 for j in range(MAX_DIG): dp[i][j + 1] += 9 * dp[i - 1][j] dp[i][j + 1] += dp[i - 1][j + 1] def first_dig(n): cnt = 0 while n >= 10: n //= 10 cnt += 1 return n, cnt def calc_ans(n): ans = 0 for n_digs in range(MAX_DIG, -1, -1): x, cnt = first_dig(n) for i in range(n_digs): ans += x * dp[cnt][i] ans += dp[cnt][n_digs] n -= x * 10 ** cnt return ans def main(): calc_dp() T = int(input()) for _ in range(T): l, r = map(int, input().split()) print(calc_ans(r) - calc_ans(l - 1)) def __starting_point(): main() __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
com = [[0]*19 for i in range(19)] def solve(x): sx, non_zero, res = str(x), 3, 0 for i in range(len(sx)): if not int(sx[i]): continue for j in range(0, non_zero+1): res += com[len(sx)-i-1][j] * 9 ** j non_zero -= 1 if non_zero == -1: break for j in range(0, non_zero+1): res += (int(sx[i])-1) * com[len(sx)-i-1][j] * 9 ** j return res def main(): t = int(input()) for i in range(19): com[i][0] = 1 for i in range(1, 19): for j in range(1, i+1): com[i][j] = com[i-1][j] + com[i-1][j-1] for i in range(t): l, r = list(map(int, input().split())) print(solve(r+1)-solve(l)) def __starting_point(): main() __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
t=int(input()) l=[] r=[] for i in range (t): q,w=[int(el) for el in input().split()] l.append(q) r.append(w) def count(x): if x<=1110: return x s=str(x) n=len(s) cz=3 out=0 for i in range(n-2): #ΠΏΡ€ΠΎΠ±Π΅ΠΆΠ°Π»ΠΈ Π΄ΠΎ сотСн a=int(s[i]) k = n - i - 1 # ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ Π·Π½Π°ΠΊΠΎΠ² послС i if a>0: if cz==3: out=out+k*(k-1)*(k-2)/6*9*9*9+k*(k-1)/2*9*9+k*9 elif cz==2: out=out+k*(k-1)/2*9*9+k*9+1 elif cz==1: out=out+k*9+1 cz=cz-1 if cz==2: plus=k*(k-1)/2*9*9+k*9+1 elif cz==1: plus=k*9+1 elif cz==0: plus=1 out=out+plus*(a-1) if cz==0: break if cz==0: out=out+1 return out if cz==1: # out=out+1+int(s[n-2]) if int(s[n-2])>0: out=out+int(s[n-2])+10 else: out=out+1+int(s[n-1]) return out out=out+int(s[n-2:])+1 return out for i in range(t): a=int(count(r[i])) b=int(count(l[i]-1)) print(a-b)
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) β€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines β€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import bisect as b from collections import OrderedDict num =[] def Classy (pos, count, current): if pos==18: num.append(current) return Classy(pos+1, count, current*10) if (count < 3 ): for i in range(1,10): Classy(pos+1, count+1, current *10 + i) Classy(0,0,0) num=list(OrderedDict.fromkeys(num)) num.pop(0) num.insert(len(num)+1, 1000000000000000000) T = int(input()) while(0 < T): L, R = [int(x) for x in input().split(' ')] ans = b.bisect_right(num, R, lo=0, hi=len(num)) - b.bisect_left(num, L, lo=0, hi=len(num)) print(int(ans)) T=T-1
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for tcase in range(int(input())): n=int(input()) ls = list(map(int, input().split())) oneneed = 2*(n - ls.count(1)) ldct = {0:0} ctr = 0 eaten = 0 for i in range(n-1,-1,-1): eaten += 1 ctr += (1 if ls[i] == 2 else -1) if ctr not in ldct: ldct[ctr] = eaten rdct = {0:0} ctr = 0 eaten = 0 for i in range(n,2*n): eaten += 1 ctr += (1 if ls[i] == 2 else -1) if ctr not in rdct: rdct[ctr] = eaten #print(oneneed, ldct, rdct) best=99**99 for k in list(rdct.keys()): otk = oneneed - k if otk in ldct: best = min(best, rdct[k]+ldct[otk]) print(best)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def solve(): n = int(input()) a = list(map(int, input().split())) red = a.count(1) blue = 2*n - red s = red-blue if s == 0: print(0) return cur = 0 d = {0:0} for i in range(n, 2*n): if a[i] == 2: cur -= 1 else: cur += 1 if cur not in d: d[cur] = i-n+1 ans = float("inf") need = s cur = 0 if need in d: ans = min(ans, d[need]) for i in reversed(list(range(n))): if a[i] == 2: cur -= 1 else: cur += 1 if need-cur in d: ans = min(ans, d[need-cur]+n-i) print(ans) t = int(input()) for i in range(t): solve()
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a, b = a[n - 1::-1], a[n:] ma, mb = {0: 0}, {0: 0} pa = pb = 0 for i in range(n): pa += 1 if a[i] == 1 else -1 pb += 1 if b[i] == 1 else -1 if pa not in ma: ma[pa] = i + 1 if pb not in mb: mb[pb] = i + 1 total = pa + pb ans = 10 ** 9 for da, va in list(ma.items()): req = total - da if req in mb: ans = min(ans, va + mb[req]) print(ans)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
from math import * for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) z=a.count(1)-a.count(2) c=a[:n] d=a[n:] jk={0:0} b=0 for i in range(n): x=d[i] if x==1: b-=1 else: b+=1 if b not in jk: jk[b]=i+1 ans=1000000 b=0 i=1 if z==0: ans=0 for x in c[::-1]: if x==1: b-=1 else: b+=1 if -z-b in jk: ans = min(ans,i+jk[-z-b]) i+=1 if -z in jk: ans=min(ans,jk[-z]) print(ans)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return list(map(int,minp().split())) def solve(): n = mint() a = list(mints()) c = dict() c[0] = 2*n d = 0 #print(d, end=' ') for i in range(2*n-1,n-1,-1): if a[i] == 1: d += 1 else: d -= 1 #print(d, i-n, end=' ') c[d] = i #print() d = 0 r = 2*n r = min(r, n + c[0] - n) for i in range(n): if a[i] == 1: d += 1 else: d -= 1 #print(d, n-i-1, end=' ') if (-d) in c: r = min(r, n - i - 1 + c[-d] - n) #print() return r for i in range(mint()): print(solve())
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def inp(ai): cur = sum(ai) * 2 - n * 3 yield cur for aii in ai: if aii == 1: cur += 1 else: cur -= 1 yield cur def inp2(ii): ans = {} for i, iii in enumerate(ii): if iii not in ans: ans[iii] = i return ans for _ in range(int(input())): n = int(input()) a = iter(map(int, input().split())) a1 = [next(a) for _ in range(n)] a1.reverse() a2 = list(a) i1 = inp2(inp(a1)) i2 = inp2((-i2i for i2i in inp(a2))) res = n * 2 for k in list(i1.keys()): if k in list(i2.keys()): res = min(res, i1[k] + i2[k]) print(res)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
q = int(input()) for rwier in range(q): n = int(input()) l = list(map(int,input().split())) j = l.count(1) d = l.count(2) pr = [0] * n le = [0] * n pr[0] = (1 if l[n] == 1 else -1) le[0] = (1 if l[n-1] == 1 else -1) for i in range(1, n): pr[i] = pr[i-1] + (1 if l[n+i] == 1 else -1) le[i] = le[i-1] + (1 if l[n-i-1] == 1 else -1) #print(pr,le) if j - d < 0: for i in range(n): pr[i] = -pr[i] le[i] = -le[i] ab = abs(j-d) if ab == 0: print(0) else: #suma = abs najwp = [123456789] * (2*n+1) najwl = [123456789] * (2*n+1) le = [0] + le pr = [0] + pr for i in range(n+1): if pr[i] >= 0 and najwp[pr[i]] == 123456789: najwp[pr[i]] = i if le[i] >= 0 and najwl[le[i]] == 123456789: najwl[le[i]] = i wyn = 41343443143 for i in range(ab+1): if najwp[i] + najwl[ab-i] < wyn: wyn = najwp[i] +najwl[ab-i] print(wyn)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import itertools from collections import defaultdict t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) ones = a.count(1) twos = a.count(2) a1 = a[:n][::-1] a2 = a[n:] target = ones - twos a1 = [[-1,1][x==1]for x in a1] a1 = [0] + list(itertools.accumulate(a1)) a2 = [[-1,1][x==1]for x in a2] a2 = [0] + list(itertools.accumulate(a2)) a2v = defaultdict(lambda: 2*n+1) for i2, x2 in enumerate(a2): a2v[x2] = min(a2v[x2], i2) best = 2*n for i1, x1 in enumerate(a1): best = min(best, i1+a2v[target-x1]) print(best)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys input = sys.stdin.readline t=int(input()) for test in range(t): n=int(input()) A=list(map(int,input().split())) LEFT=[0] RIGHT=[0] for a in A[:n]: if a==1: LEFT.append(LEFT[-1]+1) else: LEFT.append(LEFT[-1]-1) for a in A[n:][::-1]: if a==1: RIGHT.append(RIGHT[-1]+1) else: RIGHT.append(RIGHT[-1]-1) #print(LEFT) #print(RIGHT) #print() MAXLEFT=[-1]*(2*n+1) MAXRIGHT=[-1]*(2*n+1) for i in range(n+1): MAXLEFT[LEFT[i]+n]=i MAXRIGHT[RIGHT[i]+n]=i #print(MAXLEFT) #print(MAXRIGHT) #print() ANS=0 for i in range(2*n+1): if MAXLEFT[i]==-1 or MAXRIGHT[2*n-i]==-1: continue ANS=max(ANS,MAXLEFT[i]+MAXRIGHT[2*n-i]) #print(ANS) print(2*n-ANS)
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys readline = sys.stdin.readline T = int(readline()) Ans = [None]*T inf = 10**9+7 for qu in range(T): N = int(readline()) A = list(map(int, readline().split())) bj = A.count(1) sj = 2*N-bj x = sj-bj A1 = [3-2*a for a in A[:N][::-1]] A2 = [3-2*a for a in A[N:]] for i in range(1, N): A1[i] += A1[i-1] A2[i] += A2[i-1] geta = -min(0, min(A2))+1 mA = max(0, max(A2)) idx = [inf]*(mA+geta+1) idx[geta+0] = 0 for i in range(N): a2 = A2[i] idx[geta+a2] = min(idx[geta+a2], i+1) ans = inf A1 = [0]+A1 for i in range(N+1): a1 = A1[i] if -geta <= -a1-x <= mA: ans = min(ans, i+idx[geta-a1-x]) Ans[qu] = ans print('\n'.join(map(str, Ans)))
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
def main(): t = int(input()) for z in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print('no') continue f = 0 for i in [-1, +1]: for j in [-1, +1]: w = (k - i * d1 - j * d2) if f == 0 and (w % 3 == 0) and (n//3)>=(w//3)>=0 and (n//3)>=(w//3 + i * d1)>=0 and (n//3)>=(w//3 + j * d2)>=0: print('yes') f = 1 if f == 0: print('no') main()
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
import itertools import sys ''' w1 - w2 = d1 w2 - w3 = d2 w1 + w2 + w3 = k w1 = w2 + d1 w3 = w2 - d2 w2 + d1 + w2 + w2 - d2 = k w2 = (k - d1 + d2) / 3 w1 = w2 + d1 w3 = w2 - d2 ''' for _ in range(int(input())): n, k, d1, d2 = list(map(int, str.split(sys.stdin.readline()))) for s1, s2 in itertools.product((1, -1), repeat=2): cd1, cd2 = d1 * s1, d2 * s2 w2 = k - cd1 + cd2 if w2 % 3 != 0: continue w2 //= 3 w1 = w2 + cd1 w3 = w2 - cd2 if w1 >= 0 and w2 >= 0 and w3 >= 0: d = n - k mw = max((w1, w2, w3)) nw = 3 * mw - w1 - w2 - w3 if d >= nw and (d - nw) % 3 == 0: print("yes") break else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
def f1(d1, d2, n, k): a1 = 2 * d1 + d2 + k a2 = -d1 + d2 + k a3 = -d1 - (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f2(d1, d2, n, k): a1 = -2 * d1 + d2 + k a2 = d1 + d2 + k a3 = d1 - (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f3(d1, d2, n, k): a1 = 2 * d1 - d2 + k a2 = -d1 - d2 + k a3 = -d1 + (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f4(d1, d2, n, k): a1 = -2 * d1 - d2 + k a2 = d1 - d2 + k a3 = d1 + (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True a = [] z = int(input()) for i in range(z): n, k, d1, d2 = map(int, input().split()) v1 = f1(d1, d2, n, k) v2 = f2(d1, d2, n, k) v3 = f3(d1, d2, n, k) v4 = f4(d1, d2, n, k) if (v1 or v2 or v3 or v4) and n % 3 == 0: a.append('yes') else: a.append('no') print(*a, sep = '\n')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
q = int(input()) while q > 0: n, k, d1, d2 = list(map(int, input().split())) if d1 > d2: d1, d2 = d2, d1 if k - 2 * d1 - d2 >= 0 and (k - 2 * d1 - d2) % 3 == 0 and \ (n - k) - d1 - 2 * d2 >= 0 and ((n - k) - d1 - 2 * d2) % 3 == 0: print('yes') elif k - 2 * d2 - d1 >= 0 and (k - 2 * d2 - d1) % 3 == 0 and \ (n - k) - d2 - 2 * d1 >= 0 and ((n - k) - d2 - 2 * d1) % 3 == 0: print('yes') elif k - 2 * d2 + d1 >= 0 and (k - 2 * d2 + d1) % 3 == 0 and \ (n - k) - d2 - d1 >= 0 and ((n - k) - d2 - d1) % 3 == 0: print('yes') elif k - d1 - d2 >= 0 and (k - d1 - d2) % 3 == 0 and \ (n - k) - 2 * d2 + d1 >= 0 and ((n - k) - 2 * d2 + d1) % 3 == 0: print('yes') else: print('no') q -= 1
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
t = int(input()) for l in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print("no") continue n = n // 3 ok = False for i in [-1, 1]: for j in [-1, 1]: tmp = k; tmp -= d1 * i tmp -= d1 * i tmp -= d2 * j if tmp % 3 != 0: continue if tmp < 0: continue tmp = tmp // 3 x1 = tmp x2 = x1 + d1 * i x3 = x2 + d2 * j if x1 < 0 or x2 < 0 or x3 < 0: continue if x1 <= n and x2 <= n and x3 <= n: ok = True break if ok: print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
t = int(input()) for _ in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print("no") continue n = n // 3 ok = False for i in [-1, 1]: for j in [-1, 1]: tmp = k; tmp -= d1 * i tmp -= d1 * i tmp -= d2 * j if tmp % 3 != 0: continue if tmp < 0: continue tmp = tmp // 3 x1 = tmp x2 = x1 + d1 * i x3 = x2 + d2 * j if x1 < 0 or x2 < 0 or x3 < 0: continue if x1 <= n and x2 <= n and x3 <= n: ok = True break if ok: print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
def doall(): t = int(input()) def solve(n, k, d1, d2): if n % 3 == 0: r = n - k a = [[0, d1, d1 + d2], [0, d1, d1 - d2], [0, -d1, -d1 + d2], [0, -d1, -d1 - d2]] for now in a: mn = min(now) sumn = sum(now) sumb = sumn - 3 * min(now) if k < sumb or (k - sumb) % 3 != 0: continue w = max(now) tmp = 3 * w - sumn if tmp <= r and (r - tmp) % 3 == 0: return True return False ans = [] for i in range(t): n, k, d1, d2 = list(map(int, input().split())) if solve(n, k, d1, d2): ans.append('yes') else: ans.append('no') print('\n'.join(ans)) doall()
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
def f(x, y, a, b): return x > a or y > b or (a - x) % 3 or (b - y) % 3 def g(x, y, a, b): return f(x, y, a, b) and f(x, y, b, a) for i in range(int(input())): n, u, a, b = map(int, input().split()) v, s, t = n - u, a + b, 2 * b - a if b > a else 2 * a - b print('no' if g(s, t, u, v) and g(s + a, s + b, u, v) else 'yes')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
for i in range(int(input())): n, u, a, b = map(int, input().split()) if n % 3: print('no') else: if a > b: a, b = b, a p, q = a + b, 2 * b - a s, r, v = p + a, p + b, n - u t = [(p, q), (q, p), (s, r), (r, s)] print('no' if all(x > u or (x - u) % 3 or y > v or (y - v) % 3 for x, y in t) else 'yes')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
read = lambda: list(map(int, input().split())) f = lambda x, y, a, b: x > a or y > b or (a - x) % 3 or (b - y) % 3 g = lambda x, y, a, b: f(x, y, a, b) and f(x, y, b, a) t = int(input()) for i in range(t): n, k, d1, d2 = read() r = n - k d = d1 + d2 p = 2 * d2 - d1 if d2 > d1 else 2 * d1 - d2 print('no' if g(d, p, k, r) and g(d + d1, d + d2, k, r) else 'yes')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
t=int(input()) for j in range(t): inp=[int(n) for n in input().split()] n=inp[0] k=inp[1] d1=inp[2] d2=inp[3] if d2<d1: s=d1 d1=d2 d2=s if ((k>=2*d1+d2) and ((k-2*d1-d2)%3==0) and (n-k>=d1+2*d2) and ((n-k-d1-2*d2)%3==0)): print('yes') elif ((k>=2*d2+d1) and ((k-2*d2-d1)%3==0) and (n-k>=d2+2*d1) and ((n-k-d2-2*d1)%3==0)): print('yes') elif ((k>=d1+d2) and ((k-d1-d2)%3==0) and (n-k>=2*d2-d1) and ((n-k-2*d2+d1)%3==0)): print('yes') elif ((k>=2*d2-d1) and ((k-2*d2+d1)%3==0) and (n-k>=d1+d2) and ((n-k-d1-d2)%3==0)): print('yes') else: print('no')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=[[2*d1+d2,2*d2+d1],[2*d2+d1,2*d1+d2],[2*max(d1,d2)-min(d1,d2),d1+d2], [d1+d2,2*max(d1,d2)-min(d1,d2)]] y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if not y: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=((2*d1+d2,2*d2+d1),(2*d2+d1,2*d1+d2),(2*max(d1,d2)-min(d1,d2),d1+d2), (d1+d2,2*max(d1,d2)-min(d1,d2))) y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if not y: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=((2*d1+d2,2*d2+d1),(2*max(d1,d2)-min(d1,d2),d1+d2)) y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if i[1]<=k and i[1]%3==k%3 and n-k-i[0]>=0 and (n-i[0]-k)%3==0: print("yes") y=True break if not y: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=((2*d1+d2,2*d2+d1),(2*max(d1,d2)-min(d1,d2),d1+d2)) y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if i[1]<=k and i[1]%3==k%3 and n-k-i[0]>=0 and (n-i[0]-k)%3==0: print("yes") y=True break if not y: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) if (k-2*d1-d2>=0 and (k-2*d1-d2)%3==0 and n-2*d2-d1-k>=0 and (n-2*d2-d1-k)%3==0) or (k-2*d2-d1>=0 and (k-2*d2-d1)%3==0 and n-2*d1-d2-k>=0 and (n-2*d1-d2-k)%3==0) or (k-d1-d2>=0 and (k-d1-d2)%3==0 and n-2*max(d1,d2)+min(d1,d2)-k>=0 and (n-2*max(d1,d2)+min(d1,d2)-k)%3==0) or (k-2*max(d1,d2)+min(d1,d2)>=0 and (k-2*max(d1,d2)+min(d1,d2))%3==0 and n-d1-d2-k>=0 and (n-d1-d2-k)%3==0): print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) mm=0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) mm=2*max(d1,d2)-min(d1,d2) if (k-2*d1-d2>=0 and (k-2*d1-d2)%3==0 and n-2*d2-d1-k>=0 and (n-2*d2-d1-k)%3==0) or (k-2*d2-d1>=0 and (k-2*d2-d1)%3==0 and n-2*d1-d2-k>=0 and (n-2*d1-d2-k)%3==0) or (k-d1-d2>=0 and (k-d1-d2)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-d1-d2-k>=0 and (n-d1-d2-k)%3==0): print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) mm,mmm,mmmm,m=0,0,0,0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) m=d1+d2 mm=2*max(d1,d2)-min(d1,d2) mmm=2*d1+d2 mmmm=2*d2+d1 if (k-mmm>=0 and (k-mmm)%3==0 and n-mmmm-k>=0 and (n-mmmm-k)%3==0) or (k-mmmm>=0 and (k-mmmm)%3==0 and n-mmm-k>=0 and (n-mmm-k)%3==0) or (k-m>=0 and (k-m)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-m-k>=0 and (n-m-k)%3==0): print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
t=int(input()) for i in range(t): n,k,d1,d2=map(int,input().split()) m=d1+d2 mm=2*max(d1,d2)-min(d1,d2) mmm=2*d1+d2 mmmm=2*d2+d1 if (k-mmm>=0 and (k-mmm)%3==0 and n-mmmm-k>=0 and (n-mmmm-k)%3==0) or (k-mmmm>=0 and (k-mmmm)%3==0 and n-mmm-k>=0 and (n-mmm-k)%3==0) or (k-m>=0 and (k-m)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-m-k>=0 and (n-m-k)%3==0): print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
from sys import * t=int(stdin.readline()) mm=0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) mm=2*max(d1,d2)-min(d1,d2) if (k-2*d1-d2>=0 and (k-2*d1-d2)%3==0 and n-2*d2-d1-k>=0 and (n-2*d2-d1-k)%3==0) or (k-2*d2-d1>=0 and (k-2*d2-d1)%3==0 and n-2*d1-d2-k>=0 and (n-2*d1-d2-k)%3==0) or (k-d1-d2>=0 and (k-d1-d2)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-d1-d2-k>=0 and (n-d1-d2-k)%3==0): print("yes") else: print("no")
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
import sys def check(a, b, c, n, k): need = n // 3 return ((n - k) == (need - a) + (need - b) + (need - c) and a <= need and b <= need and c <= need and a >= 0 and b >= 0 and c >= 0) for tc in range(int(sys.stdin.readline())): n,k,d1,d2 = map(int, sys.stdin.readline().split()) if n % 3 != 0: print('no') continue ans = False #case++ a = k - 2 * d1 - d2 if a % 3 == 0: a //= 3 ans |= check(a, a + d1, a + d1 + d2, n ,k) #case+- a = k + d2 - 2 * d1 if a % 3 == 0: a //= 3 ans |= check(a, a + d1, a + d1 - d2, n, k) #case-- a = k + 2 * d1 + d2 if a % 3 == 0: a //= 3 ans |= check(a, a - d1, a - d1 - d2, n, k) #case-+ a = k - d2 + 2 * d1 if a % 3 == 0: a //= 3 ans |= check(a, a - d1, a - d1 + d2, n, k) print('yes' if ans else 'no')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
for i in range(int(input())): n, u, a, b = list(map(int, input().split())) if n % 3: print('no') else: if a > b: a, b = b, a p, q = a + b, 2 * b - a s, r, v = p + a, p + b, n - u t = [(p, q), (q, p), (s, r), (r, s)] print('no' if all(x > u or (x - u) % 3 or y > v or (y - v) % 3 for x, y in t) else 'yes') # Made By Mostafa_Khaled
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
t = int(input()) ret = [] while t>0: t-=1 n,k,d1,d2 = map(int,input().split()) # ans = [] y1 = (k-(d1-d2))//3 x1 = y1+d1 z1 = y1-d2 # ans = [y1,z1,x1] # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) ans1 = 2*x1-(z1+y1) if x1+y1+z1==k and min(z1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # ans = [] y1 = (k-(d1+d2))//3 x1 = y1+d1 z1 = y1+d2 if d1>=d2: # ans = [y1,z1,x1] ans1 = 2*x1-(y1+z1) else: # ans = [y1,x1,z1] ans1 = 2*z1-(y1+x1) # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) if x1+y1+z1==k and y1>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k-(d2-d1))//3 x1 = y1-d1 z1 = y1+d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*z1-(x1+y1) if x1+y1+z1==k and min(x1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k+(d2+d1))//3 x1 = y1-d1 z1 = y1-d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*y1-(x1+z1) if x1+y1+z1==k and min(x1,z1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # if d1>=d2: # ans.append(2*d1-d2) # ans.append(d2+2*(d1-d2)) # else: # ans.append(2*d2-d1) # ans.append(d1+2*(d2-d1)) # ans+=[d1+2*d2,d2+2*d1,d1+d2] # done = False # print(ans) # for a in ans: # # if (a==0 and (n-k)%3==0) or (a!=0 and (n-k)//a>1 and (n-k)%a==0): # if (a<=n-k) and (n-k-a)%3==0: # print(a) # done = True # break # if done: # print('yes') # else: ret.append('no') print(*ret, sep = '\n')
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≀ n ≀ 10^12;Β 0 ≀ k ≀ n;Β 0 ≀ d_1, d_2 ≀ k) β€” data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
import sys lines = sys.stdin.readlines() ''' (n, p) = map(int, lines[0].strip().split(" ")) ranges = [] for i in range(1, n+1): (l, r) = map(int, lines[i].strip().split(" ")) ranges.append((l,r)) probs = [] for lr in ranges: poss = lr[1]//p - (lr[0]-1)//p probs.append(poss/(lr[1]-lr[0]+1)) res = 0 for i in range(n): res += probs[i] + probs[i-1] - probs[i] * probs[i-1] print(res * 2000) ''' N = int(lines[0].strip()) for i in range(1, 1+N): (n, k, d1, d2) = map(int, lines[i].strip().split(" ")) if n % 3 != 0: print("no"); continue def solve(da, db): tmp = k - da - db if tmp % 3 != 0: return True b = tmp // 3 a = b + da c = b + db if min(a,b,c) < 0: return True if n//3 >= max(a,b,c): return False else: return True cannot = True if cannot: cannot = solve(d1, d2) if cannot: cannot = solve(d1, -d2) if cannot: cannot = solve(-d1, d2) if cannot: cannot = solve(-d1, -d2) if cannot: print("no") else: print("yes")
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n=int(input()) a=sorted(int(input()) for _ in range(n)) print(sum(a[i]*a[-i-1] for i in range(n))%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) A = [0] * n for i in range(n): per = int(input()) A[i] = per A.sort() answer = 0 for i in range(n): answer = (answer + A[i] * A[n - i - 1]) % 10007 print(answer)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) ai = [] num = 0 for i in range(n): ai += [int(input())] ai.sort() for i in range(n//2): num += ai[n-i-1] * ai[i] * 2 if n % 2: num += ai[n//2] ** 2 print(num % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) a =[] b =[] for i in range(n): a.append(int(input())) b.append(a[len(a)-1]) a.sort() b.sort() b.reverse() sum = 0 for i in range(n): sum+=(a[i]*b[i]) sum%=10007 print(sum)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) a = [int(input()) for i in range(n)] a.sort() print(sum(map(lambda x,y: x*y, a, reversed(a))) % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
#import sys #sys.stdin=open('input.txt') #a=map(int,input().split()) n=int(input()) l=[] for i in range(n): l.append(int(input())) d=l[::] l.sort() d.sort(reverse=True) print(sum((l[x]*d[x] for x in range(n)))%10007) #print('right:',input())
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) sp = [] for i in range(n): sp.append(int(input())) sp.sort() s = 0 for i in range(n): s = (s + sp[i] * sp[-i-1]) % 10007 print(s)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
I=input n=int(I()) R=list(range(n)) a=sorted(int(I())for _ in R) k=0 for i in R:k=(k+a[i]*a[n-i-1])%10007 print(k)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n=int(input()) list=[] summ=int(0) for i in range(n): a=int(input()) list.append(a) rev=list[::] list.sort() rev.sort(reverse=True) for x in range(n): summ=(summ+((list[x]*rev[x])%10007)%10007)%10007 print(summ)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) a = sorted([int(input()) for i in range(n)]) time = 0 for i in range(n): time += a[i] * a[n - 1 - i] print(time % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) C = [] A = [int(input()) for i in range(n)] A.sort() B = A for x in range(n): C.append(A[x]*B[n-x-1]) a = sum(C) print(a%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) dt = sorted([int(input()) for i in range(n)]) ans = 0 for i in range(n): ans += dt[i] * dt[-i-1] print(ans%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
#!/usr/bin/env python #-*-coding:utf-8 -*- n=int(input()) A=sorted(int(input())for _ in range(n)) B=10007 s=0 for i in range(n):s=(A[i]*A[-1-i]+s)%B print(s)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) num = [int(input()) for _ in range(n)] num.sort() begin = 0 end = n-1 ans = 0 while begin<=end: if begin == end: ans += (num[begin]*num[end]) ans %= 10007 else: ans += 2*num[begin]*num[end] ans %= 10007 begin+=1 end-=1 print(ans)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) num = [int(input()) for _ in range(n)] num.sort() begin = 0 end = n-1 ans = 0 while begin<=end: if begin == end: ans += (num[begin]*num[end]) ans %= 10007 else: ans += 2*num[begin]*num[end] ans %= 10007 begin+=1 end-=1 print(ans)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) num = sorted([int(input()) for _ in range(n)]) print(sum([x*y for x,y in zip(num, reversed(num))])%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
''' a=input() string=input() n=int(input()) list1=[] score=0 for i in range(n): list1.append((input()).split()) maX=int(input()) list2=[0]*len(string) var = 0 while n>0: times = 0 for i in range (len(string)): if string[i:i+len(list1[var][0])] == list1[var][0] and not maX in list2[i:i+len(list1[var][0])]: score += int(list1[var][1]) for j in range(i, i+len(list1[var][0])): list2[j] += 1 if i+len(list1[var][0]) == len(string): break if times == maX: break var += 1 n -= 1 print(score) ''' final=0 n= int(input()) list1=[] for i in range(n): list1.append(int(input())) list1.sort() for i in range(n): final += list1[i] * list1[n-i-1] print(final%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) a = [] for i in range(n): a.append(int(input())) a.sort() b = []; for i in range(int((n+1)/2)): b.append(a[i]*a[n-1-i]) res = sum(b)*2 if n%2 == 1: res-=b[len(b)-1] print(res % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n=int(input()) a=sorted(int(input()) for _ in range(n)) print(sum(a[i]*a[-i-1] for i in range(n))%10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n=int(input()) l=[] for i in range(n): l.append(int(input())) l.sort() r=l[:]; r.reverse() res=0; for i in range(n): res=(res+l[i]*r[i])%10007 print(res)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
n = int(input()) a=[] for i in range(n): a += [int(input())] a.sort() ans = 0 for i in range(n): ans += a[i] * a[n-i-1] print(ans % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
laziness = [] for i in range(int(input())): laziness.append(int(input())) laziness.sort() j = len(laziness) - 1 sum = 0 for numb in laziness: sum += numb * laziness[j] j -= 1 print(sum % 10007)
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where a_{i} represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 100 000)Β β€” the number of tasks. The next n lines contain exactly one integer number a_{i} (1 ≀ a_{i} ≀ 100 000)Β β€” both the difficulty of the initial task and the laziness of the i-th students. -----Output----- Print the minimum total time to finish all tasks modulo 10 007. -----Example----- Input 2 1 3 Output 6 -----Note----- In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
num= int(input()) work=[] for i in range(num): work.append(int(input())) laziness= work.copy() time= 0 laziness.sort() work.sort() work=work[::-1] for i in range(len(work)): time += work[i] * laziness[i] print(time % (10007))
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline def compress(array): array2 = sorted(set(array)) memo = {value : index for index, value in enumerate(array2)} for i in range(len(array)): array[i] = memo[array[i]] + 1 return array t = int(input()) base = 10 ** 6 for _ in range(t): n, b = list(map(str, input().split())) n = int(n) ans = [0] * n now = base ans[0] = base for i in range(n - 1): if b[i] == ">": now -= base ans[i + 1] = now else: now += 1 ans[i + 1] = now print(*compress(ans)) now = base ans[0] = base for i in range(n - 1): if b[i] == ">": now -= 1 ans[i + 1] = now else: now += base ans[i + 1] = now print(*compress(ans))
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def main(): import sys input = sys.stdin.readline for _ in range(int(input())): line = list(input().split()) N = int(line[0]) S = line[1] inc_num = S.count('<') ans = [0] * N inc = N dec = N - inc_num for i in range(N - 1): if ans[i+1]: continue if S[i] == '<': j = i+1 cnt = 1 while True: if j == N-1: break if S[j] == '<': cnt += 1 else: break j += 1 #print(cnt) for j in range(i+cnt-1, i-1, -1): #print(j) ans[j+1] = inc inc -= 1 for i in range(N): if ans[i] == 0: ans[i] = dec dec -= 1 print(*ans) ans = [0] * N inc = N - inc_num + 1 dec = N - inc_num for i in range(N - 1): if S[i] == '<': ans[i + 1] = inc inc += 1 for i in range(N): if ans[i] == 0: ans[i] = dec dec -= 1 print(*ans) def __starting_point(): main() __starting_point()
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import stdin,stderr def rl(): return [int(w) for w in stdin.readline().split()] def p1(n, s, le): r = [] b = i = 0 while i < n: try: ni = s.index(le, i) + 1 except ValueError: ni = n r += list(range(ni, i, -1)) i = ni return r t, = rl() for _ in range(t): n,s = stdin.readline().split() n = int(n) print(*(n - x + 1 for x in p1(n, s, '>'))) print(*p1(n, s, '<'))
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys Q = int(sys.stdin.readline().strip()) for q in range (0, Q): n, s = sys.stdin.readline().strip().split() n = int(n) U = [1] D = [1] for i in range (0, n-1): if s[i] == "<": U[-1] = U[-1] + 1 D.append(1) else: D[-1] = D[-1] + 1 U.append(1) m = n i = 0 A = [] while m > 0: for j in range (0, U[i]): A.append(str(m-U[i]+j+1)) m = m - U[i] i = i + 1 print(" ".join(A)) m = 0 i = 0 A = [] while i < len(D): for j in range (0, D[i]): A.append(str(m+D[i]-j)) m = m + D[i] i = i + 1 print(" ".join(A))
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def makeRepeat(s): repeat = [[s[0], 0]] for ch in s: if ch == repeat[-1][0]: repeat[-1][1] += 1 else: repeat.append([ch, 1]) return repeat def solve(N, S): assert len(S) == N - 1 curr = 0 repeat = makeRepeat(S) longest = list(range(1, N + 1)) shortest = list(reversed(list(range(1, N + 1)))) for ch, count in repeat: if ch == ">": longest[curr : curr + count + 1] = reversed( longest[curr : curr + count + 1] ) else: assert ch == "<" shortest[curr : curr + count + 1] = reversed( shortest[curr : curr + count + 1] ) curr += count # print(" " + " ".join(S)) return " ".join(map(str, shortest)) + "\n" + " ".join(map(str, longest)) def __starting_point(): T, = list(map(int, input().split())) for t in range(T): N, S = input().split() N = int(N) ans = solve(N, S) print(ans) __starting_point()
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def solve1(n, comps): ans = [] rng = iter(list(range(1, n + 1))) for comp in [len(comp) + 1 for comp in comps.split(">")][::-1]: ans.append([ansi for (_, ansi) in zip(list(range(comp)), rng)]) fin_ans = [] ans.reverse() for ansi in ans: fin_ans.extend(ansi) return fin_ans def solve2(n, comps): ans = [] rng = iter(list(range(1, n + 1))) for comp in [len(comp) + 1 for comp in comps.split("<")]: ans.extend([ansi for (_, ansi) in zip(list(range(comp)), rng)][::-1]) return ans def main(): for _ in range(int(input())): n, comps = input().split() n = int(n) print(*solve1(n, comps)) print(*solve2(n, comps)) main()
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, S = input().split() n = int(n) max_ans = [0] * n i = j = 0 cur = 1 for s in S + '<': if s == '>': j += 1 elif s == '<': for k in range(j, i - 1, -1): max_ans[k] = cur cur += 1 j += 1 i = j min_ans = [0] * n i = j = 0 cur = 1 for s in S[::-1] + '>': if s == '<': j += 1 elif s == '>': for k in range(j, i - 1, -1): min_ans[k] = cur cur += 1 j += 1 i = j print(*min_ans[::-1]) print(*max_ans)
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def getInput(): line = input().split() return int(line[0]), line[1] def sLIS(n, s): ans = list(range(n, 0, -1)) rev = [] i = 0 while i < n-1: if s[i] == '<': j = i+1 while j < n-1 and s[j] == '<': j += 1 rev.append((i, j)) i = j+1 else: i += 1 for r in rev: i, j = r while i <= j: ans[i], ans[j] = ans[j], ans[i] i += 1 j -= 1 return ans def lLIS(n, s): ans = list(range(1, n+1)) rev = [] i = 0 while i < n-1: if s[i] == '>': j = i+1 while j < n-1 and s[j] == '>': j += 1 rev.append((i, j)) i = j+1 else: i += 1 for r in rev: i, j = r while i <= j: ans[i], ans[j] = ans[j], ans[i] i += 1 j -= 1 return ans for _ in range(int(input())): n, s = getInput() """ p = [] c = +1 if s[0] == '<' else -1 for e in s[1:]: if c > 0 and e == '>': p.append(c) c = -1 elif c < 0 and e == '<': p.append(c) c = +1 else: c += +1 if e == '<' else -1 p.append(c) """ print(*sLIS(n, s)) print(*lLIS(n, s))
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
Q = int(input()) total = [] for _ in range(Q): n, s = input().split() n=int(n) seq = [] curlen = 1 for i in range(len(s)): if s[i]=='<': curlen+=1 else: seq.append(curlen) curlen=1 seq.append(curlen) curr = n ans =[] for sq in seq: ans.extend(range(curr-sq+1,curr+1)) curr-=sq total.append(' '.join(map(str,ans))) curr = n ans = ['']*n for i in range(len(s)-1,-1,-1): if s[i]=='<': ans[i+1]=str(curr) curr-=1 for i in range(n): if not ans[i]: ans[i]=str(curr) curr-=1 total.append(' '.join(ans)) print(*total, sep='\n')
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for i in range(int(input())): n=int(input()) s=list(map(int,input().split())) a=0 for i in s: if i<2049:a+=i if a<2048:print("NO") else:print("YES")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
t=int(input()) for i in range(t): n=int(input()) a=list(map(int,input().split())) a.sort(reverse=True) s=0 for j in a: if j<=2048: s+=j if s==2048: print("YES") break else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q = int(input()) for i in range(q): n = int(input()) a = [int(x) for x in input().split()] a.sort() s = 0 for i in a: if i <= 2048: s += i print("YES" if s >= 2048 else "NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] a.sort(reverse=True) t = 2048 for i in a: if t - i >= 0: t -= i print(['NO', 'YES'][t == 0])
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q = int(input()) while q: n = int(input()) a = list(map(int, input().split())) a.sort() i = 0 d = False while i < len(a) - 1: if 2048 in a: d = True break if a[i] == a[i + 1]: a.append(a[i] + a[i + 1]) a.sort() i += 2 else: i += 1 if 2048 in a: d = True if d: print("YES") else: print("NO") q -= 1
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
t=int(input()) for nt in range(t): n=int(input()) l=list(map(int,input().split())) new=[] for i in l: if i<=2048: new.append(i) new.sort() if 2048 in new: print ("YES") else: s=0 flag=0 for i in range(len(new)-1,-1,-1): s+=new[i] if s==2048: print ("YES") flag=1 break if flag==0: print ("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for _ in range(int(input())): n=int(input()) arr=list(map(int,input().split())) s=0 for i in arr: if i<=2048: s+=i if s>=2048: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
import sys from collections import defaultdict from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() def write(*args, sep=" "): for i in args: sys.stdout.write("{}".format(i) + sep) INF = float('inf') MOD = int(1e9 + 7) for t in range(int(input())): n = int(input()) arr = list(map(int, input().split())) d = defaultdict(int) for i in arr: d[i] += 1 for i in range(0, 11): x = d[1 << i] // 2 d[1 << (i + 1)] += x if d[2048]: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q = int(input()) for rew in range(q): n = int(input()) l = list(map(int,input().split())) l.sort() while True: if 2048 in l: print("YES") break if len(l) == 0: print("NO") break if len(l) > 1 and l[0] == l[1]: l = [2*l[0]] + l[2:] else: l = l[1:] l.sort()
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
t = int(input()) for _ in range(t): _ = input() L = [int(x) for x in input().split() if int(x) <= 2048] print("YES" if sum(L) >= 2048 else "NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
import collections def solve(): n = int(input()) a = list(map(int, input().split())) d = collections.defaultdict(int) for i in a: d[i] += 1 for i in range(11): d[2 ** (i + 1)] += d[2 ** i] // 2 if d[2 ** 11]: print('YES') else: print('NO') t = int(input()) for _ in range(t): solve()
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q=int(input()) for t in range(q): n=int(input()) l=list(map(int,input().split())) tot=0 for i in range(n): if(l[i]<=2048): tot+=l[i] if(tot>=2048): print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
tgt = (2048).bit_length() - 1 for _ in range(int(input())): n = int(input()) c = [0] * 31 for x in input().split(): c[int(x).bit_length() - 1] += 1 for i in range(tgt): c[i + 1] += c[i] // 2 print('YES' if c[tgt] else 'NO')
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
T = int(input()) while T > 0: T -= 1 n = int(input()) A = map(int, input().split()) cnt = [0] * 40 for i in A: pw = 0 while i > 1: i //= 2 pw += 1 cnt[pw] += 1 for i in range(12): cnt[i+1] += cnt[i] // 2 if cnt[11] > 0: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q = int(input()) for i in range(q): n = int(input()) s1 = list(map(int,input().split())) s1.sort() num = 0 for i in s1: if i > 2048: break num += i if num >= 2048: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
q = int(input()) for _ in range(q): n = int(input()) s = list(map(int, input().split())) beki = [0]*50 for i in range(n): beki[s[i].bit_length()-1] += 1 for i in range(29): beki[i+1] += beki[i]//2 if beki[11] > 0: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for _ in range(int(input())): n = int(input()) S = [d for d in map(int, input().split()) if d <= 2048] if sum(S) >= 2048: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) β€” the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for _ in range(int(input())): n = int(input()) l = [int(i) if int(i) <= 2048 else 0 for i in input().split()] if(sum(l)>=2048): print("YES") else: print("NO")