text
stringlengths
37
1.41M
n = int(input()) ans1 = 2 ans2 = 1 ans3 = 3 for i in range(1,n): ans3 = ans1 + ans2 ans1 = ans2 ans2 = ans3 if n == 1: print(1) else: print(ans3)
import math def insertionsort(A, n, g): cnt = 0 for i in range(g, n): v = A[i] j = i - g while j >= 0 and A[j] > v: A[j + g] = A[j] j = j - g cnt += 1 A[j + g] = v return cnt def shellsort(A, n): G = [] gap = 1 while gap <= math.ceil(n / 3): G.append(gap) gap = gap * 3 + 1 G = G[::-1] m = len(G) print(m) print(*G) cnt = 0 for i in range(m): cnt += insertionsort(A, n, G[i]) print(cnt) n = int(input()) A = [] for i in range(n): A.append(int(input())) shellsort(A, n) for i in range(n): print(A[i])
s=str(input()) n=len(s) if n==2: print(s) else: print(s[::-1])
N = int(input()) if N%3 < 3: print(N//3) elif N<3: print('0') else: print(N//3)
A = list(input()) B = list(input()) C = list(input()) def turn(card): if card == 'a': if A: turn(A.pop(0)) else: print('A') exit(0) elif card == 'b': if B: turn(B.pop(0)) else: print('B') exit(0) else: if C: turn(C.pop(0)) else: print('C') exit(0) turn(A.pop(0))
#1??????????????? num_data = int(input()) for i in range(num_data): input_line = input().split(" ") a = int(input_line[0]) b = int(input_line[1]) c = int(input_line[2]) #?????????????????????????????????????????§ if (pow(a,2) + pow(b,2) == pow(c,2) ) or (pow(a,2) + pow(c,2) == pow(b,2) ) or (pow(b,2) + pow(c,2) == pow(a,2)): print("YES") else: print("NO")
S=input() cnt=0 for c in S: cnt+=c=='o' if cnt+15-len(S)>=8: print('YES') else: print('NO')
# coding: utf-8 # Your code here! n=int(input()) cnt=n//11 cnt*=2 if 0<n%11<=6: cnt+=1 elif n%11>6: cnt+=2 print(cnt)
import math a, b, C = [float(_) for _ in input().split()] S = 0.5 * a * b * math.sin(math.radians(C)) c = math.sqrt(pow(a,2) + pow(b,2) - 2 * a * b * math.cos(math.radians(C))) L = a + b + c h = S / a * 2 print(S) print(L) print(h)
s = input() flag = False for i in s: if i == "C": flag = True elif i == "F": if flag == True: print("Yes") break else: print("No")
S = input() import collections c = collections.Counter(list(S)) ans = "yes" for i in c.values(): if i == 1: pass else: ans = "no" break print(ans)
strings=input() for i in strings: if "a" <= i <= "z": print(i.upper(), end="") elif "A" <= i <= "Z": print(i.lower(), end="") else: print(i, end="") print("")
given = input() target = given*2 keyword = input() if keyword in target: print("Yes") else: print("No")
n=int(input()) s=input() if 'Y' in s:print("Four") else:print("Three")
X = int(input().rstrip()) r = 'Yes' if X >= 30 else 'No' print(r)
import math import collections import fractions import itertools import functools import operator import bisect def solve(): n = int(input()) sushi = ["a"] alt = [chr(ord("a") + i) for i in range(n)] for i in range(n-1): sushi = [ans + x for ans in sushi for x in alt[:len(set(ans)) + 1]] for i in sushi: print(i) return 0 if __name__ == "__main__": solve()
#coding: UTF-8 while True: l = raw_input().split() H = int(l[0]) W = int(l[1]) if H == 0 and W == 0: break else: a = 0 if H % 2 == 0 and W % 2 == 0: while a < H: print "#." * int (W / 2) +"\n" +".#" * int (W / 2) a += 2 print if H % 2 == 0 and W % 2 != 0: while a < H: print "#." * int (W / 2)+"#" +"\n" +".#" * int (W / 2)+"." a += 2 print a = 1 if H % 2 != 0 and W % 2 == 0: while a < H: print "#." * int (W / 2) +"\n" +".#" * int (W / 2) a += 2 print"#." * int(W/2) print if H % 2 != 0 and W % 2 != 0: while a < H: print "#." * int (W / 2)+"#" +"\n" +".#" * int (W / 2)+"." a += 2 print"#." * int(W/2)+"#" print
def main(): s = set() n = int(input()) for _ in range(n): com = input().split() if com[0] == 'insert':s.add(com[1]) else : if com[1] in s:print('yes') else :print('no') if __name__ == '__main__': main()
def main(): n = input().split() a = int(n[0]) b = int(n[1]) c = int(n[2]) if a < b < c: print("Yes") else: print("No") if __name__ == '__main__': main()
H = int(input()) i = 1 ans = 1 while H > 1: H //= 2 ans += 2 ** i i += 1 print(ans)
import sys readline = sys.stdin.buffer.readline def even(n): return 1 if n%2==0 else 0 from collections import defaultdict dic1 = defaultdict(int) for i in range(3): n,m = map(int,readline().split()) dic1[n] += 1 dic1[m] += 1 lst1 = list(dic1.values()) lst1.sort() if lst1 == [1,1,2,2]: print("YES") else: print("NO")
class UnionFind(): def __init__(self, N): self.par = [i for i in range(N)] def root(self, x): if self.par[x] == x: return x else: self.par[x] = self.root(self.par[x]) return self.par[x] def unite(self, x, y): self.par[self.root(y)] = self.root(x) def same(self, x, y): return self.root(x) == self.root(y) N, M = map(int, input().split()) p = list(map(int, input().split())) uf = UnionFind(N) for i in range(M): x, y = map(int, input().split()) uf.unite(x-1, y-1) r = 0 for i in range(N): r += uf.same(i, p[i] - 1) print(r)
while True: x = int(input()) if x == 0: break sum = 0 while x != 0: sum += x%10 x = x//10 print(sum)
import collections #文字列を一文字ずつ取得したいとき def inputStrOnebyOne(): s = list(input()) return s #整数を一つずつリストに入れる def inputOnebyOne_Int(): a = list(int(x) for x in input().split()) return a def main(): a, b, c, d = map(int,input().split()) if a+b > c+d: print("Left") elif a+b < c+d: print("Right") else: print("Balanced") if __name__=='__main__': main()
def make_divisors(n): ans = 10 ** 12 + 10 i = 1 while i*i <= n: if n % i == 0: t = 0 if i != n // i: t = i + (n//i) - 2 else: t = i * 2 - 2 #print(i, n//i, t) if ans > t: ans = t i += 1 return ans n = int(input()) print(make_divisors(n))
S = input().replace('hi','') if len(S) == 0: print('Yes') else: print('No')
x=int(input()) def main(x): if x ==1: return 0 else: return 1 print(main(x))
def main(): N = int(input()) if N <= 1: print(1) return fib = [0]*(N+1) fib[0], fib[1] = 1, 1 for i in range(2, N+1): fib[i] = fib[i-1] + fib[i-2] print(fib[N]) if __name__ == "__main__": main()
seta = set([1,3,5,7,8,10,12]) setb = set([4,6,9,11]) setc = set([2]) a,b = map(int, input().split()) if (a in seta and b in seta) or (a in setb and b in setb) or (a in setc and b in setc): print('Yes') else: print('No')
(a,b)=[int(x) for x in input().split()] if a==1 or b==1: print(1) else: print((a*b+1)//2)
import sys k = int(input()) x=0;y=0 if k%2==0: x=int(k/2);y=int(k/2) else: x=int(k/2)+1;y=int(k/2) print(x*y)
n = int(input()) l = [int(input()) for _ in range(n)] print("second" if all(a % 2 == 0 for a in l) else "first")
#!/usr/bin/env python from __future__ import division, print_function from sys import stdin def bubble(cards): for i in range(len(cards)): for j in range(len(cards)-1, i, -1): if cards[j][1] < cards[j-1][1]: cards[j], cards[j-1] = cards[j-1], cards[j] def selection(cards): for i in range(len(cards)): mini = i for j in range(i, len(cards)): if cards[j][1] < cards[mini][1]: mini = j cards[i], cards[mini] = cards[mini], cards[i] def stable_test(orders, cards): for order in orders: n = -1 for c in order: p = n n = cards.index(c) if p > n: return False return True stdin.readline() data1 = stdin.readline().split() data2 = data1[:] orders = [[c for c in data1 if c.endswith(str(i))] for i in range(1, 10)] orders = [o for o in orders if len(c) > 1] bubble(data1) print(*data1) print('Stable' if stable_test(orders, data1) else 'Not stable') selection(data2) print(*data2) print('Stable' if stable_test(orders, data2) else 'Not stable')
a=int(input()) b=int(input()) if a==3: print(3-b) elif b==3: print(3-a) else: print(3)
import copy def bs(array): for i in range(len(array)-1): for c in range(len(array)-1, i,-1): if array[c][1] < array[c-1][1]: swap = array[c] array[c] = array[c-1] array[c-1] = swap return array def ss(array): for i in range(len(array)): minj = i for c in range(i+1,len(array)): if array[minj][1] > array[c][1]: minj = c swap = array[i] array[i] = array[minj] array[minj] = swap return array def isStable(ar1,ar2): flag = 0 for (x,y) in zip(ar1,ar2): if x != y: flag = 1 break if flag == 0: print('Stable') else: print('Not stable') n = input() arr = [[i[0],int(i[1])] for i in input().split()] arr1 = copy.deepcopy(arr) arr2 = copy.deepcopy(arr) bsarr = bs(arr2) ssarr = ss(arr1) print(' '.join([''.join([ x[0], str(x[1]) ]) for x in bsarr])) print('Stable') print(' '.join([''.join([ x[0], str(x[1]) ]) for x in ssarr])) isStable(bsarr,ssarr)
A = int(input()) p = A % 10 A //= 10 q = A % 10 A //= 10 r = A % 10 s = A // 10 if p == q == r or q == r == s: print("Yes") else: print("No")
from collections import defaultdict from itertools import groupby, accumulate, product, permutations, combinations def solve(): d = defaultdict(lambda: 0) N = int(input()) for i in range(N): S = input() d[S[0]] += 1 s = 'MARCH' s = list(s) cnt = 0 for com in combinations(s,3): prod = 1 for c in com: prod *= d[c] cnt += prod return cnt print(solve())
def make_divisors(n): lower_divisors, upper_divisors = [], [] i = 1 while i*i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n//i) i += 1 return lower_divisors + upper_divisors[::-1] a, b, k = map(int, input().split()) set_a = set(make_divisors(a)) set_b = set(make_divisors(b)) both = list(set_a & set_b) both.sort() print(both[- k])
N = int(input()) counter = 0 for i in range(1, N + 1): if len(str(i)) % 2: counter += 1 print(counter)
import math def isMM(num): if num < 13 and num > 0: return True else: return False def hantei(number): num = math.floor(number/100) ber = number % 100 if isMM(num) is False and isMM(ber) is False: return "NA" elif isMM(num) is True and isMM(ber) is True: return "AMBIGUOUS" elif isMM(num) is True and isMM(ber) is False: return "MMYY" else: return "YYMM" num = int(input()) print(hantei(num))
def gcd(x,y): if x<=y: tmp=x x=y y=tmp r=x%y while(r!=0): x=y y=r r=x%y return y a=input().split() b=[int(i) for i in a] print(gcd(b[0],b[1]))
form = input() form = form.split() form.sort() if form == ["5","5","7"]: print("YES") else: print("NO")
inf = float('INF') class edge: def __init__(self, source, destination, weight): self.from_ = source self.to_ = destination self.weight = weight def values(self): return self.from_, self.to_, self.weight def bellman_ford(edges, start, n): costs = [inf] * n predecessors = [-1] * n costs[start]= 0 for _ in range(n-1): for u in range(n): for v,w in edges[u].items(): if costs[v] > costs[u] + w: costs[v] = costs[u] + w predecessors[v] = u v = n-1 while v > 0: u = predecessors[v] if costs[v] > costs[u] + edges[u][v]: return 'inf' v = u return -costs[n-1] n,m = map(int, input().split()) edges = [{} for _ in range(n)] for _ in range(m): a,b,c = map(int, input().split()) edges[a-1][b-1] = -c print(bellman_ford(edges, 0, n))
for a in range(1,10): for b in range(1,10): print(a,"x",b,"=",a*b,sep="")
s = list(input()) if s[0] == 'S': print("Cloudy") elif s[0] == 'C': print("Rainy") elif s[0] == 'R': print("Sunny")
def get_gcd(a,b): if a > b: a, b = b, a if b % a == 0: return a else: return get_gcd(a, b % a) def get_lcm(a,b,g): return a * b / g while True: try: a,b = map(int, sorted(raw_input().split())) GCD = get_gcd(a,b) LCM = get_lcm(a,b,GCD) print "{} {}".format(GCD, LCM) except EOFError: break
Stack=[] def push(x): Stack.append(x) def pop(): return Stack.pop() def solve(): s_list=raw_input().split() for i in range(len(s_list)): if s_list[i]=='+': b=pop() a=pop() push(a+b) elif s_list[i]=='-': b=pop() a=pop() push(a-b) elif s_list[i]=='*': b=pop() a=pop() push(a*b) else: push(int(s_list[i])) print(pop()) def main(): if __name__=="__main__": solve() main()
def main(): c = input() if c in 'aiueo': print('vowel') else: print('consonant') if __name__ == '__main__': main()
for i in range(int(input())): temp = [int(x) for x in input().split()] stemp = sorted(temp) if stemp[2]**2 == stemp[1]**2 + stemp[0]**2: print('YES') else: print('NO')
S = input() n1 = int(S[:2]) n2 = int(S[2:]) if 1 <= n1 <= 12 and 1 <= n2 <= 12: print('AMBIGUOUS') elif 1 <= n1 <= 12 and (not 1 <= n2 <= 12): print('MMYY') elif (not 1 <= n1 <= 12) and 1 <= n2 <= 12: print('YYMM') else: print('NA')
string = input() if string == "ARC": print("ABC") else: print("ARC")
n = int(input()) a = input() l = int(input()) for i in range(n): if a[l-1] != a[i]: print('*', end='') else: print(a[l-1], end='')
s = raw_input() s += s p = raw_input() if(p in s): print("Yes") else: print("No")
N=input() S=N[::-1] if N==S: print("Yes") else: print("No")
a = list(str(i) for i in input().split()) if a.count("5") == 2 and a.count("7") == 1: print("YES") else: print("NO")
S=input() ans="" for i in range(3): ans=ans+S[i] print(ans)
a = input().split() out = "" for s in a: out += s[0].upper() print(out)
a = int(input()) b = int(input()) print("GREATER"*(a>b) or "LESS"*(a<b) or "EQUAL")
str_list = input().rstrip() str_search = input().rstrip() str_list = str_list + str_list if str_search in str_list: print("Yes") else: print("No")
S=input() a=['Sunny','Cloudy','Rainy'] for i in range(3): if a[i]==S: print(a[(i+1)%3])
string = input() n = int(input()) print_s=[] for i in range(n): order = [i for i in input().split()] if order[0] == "replace": string = list(string) for i in range(len(order[3])): string[int(order[1])+i]=order[3][i] string= "".join(string) elif order[0] == "reverse": string = string.replace(string[int(order[1]):int(order[2])+1], string[int(order[2])-len(string): int(order[1])-len(string)-1:-1]) else: print_s.append(string[int(order[1]):int(order[2])+1]) for i in range(len(print_s)): print(print_s[i])
def main(): k = int(input()) s = str(input()) output = '' if len(s) > k: for i in range(k): output += s[i] output += '...' else: output = s print(output) if __name__ == '__main__': main()
def f(s): return sum(map(int, s)) while True: s = input() if s == "0": break print(f(s))
b = input() if b == 'A': answer = 'T' elif b == 'T': answer = 'A' elif b == 'C': answer = 'G' elif b == 'G': answer = 'C' else: answer = '入力間違い【A】【T】【C】【G】を入力' print(answer)
n = int(input()) print('YES' if n in [3, 5, 7] else 'NO')
N = input() ans = False for i in N: if i is "7": ans = True break else: pass if ans == True: print("Yes") else: print("No")
cnt=0 def merge(A,left,mid,right): global cnt n1=mid-left n2=right-mid L=A[left:mid]+[1000000000] R=A[mid:right]+[1000000000] i=0 j=0 for k in range(left,right): if L[i]<=R[j]: A[k]=L[i] cnt=cnt+1 i+=1 else: A[k]=R[j] j+=1 cnt=cnt+1 def mergeSort(A,left,right): if left+1< right: mid = int((left + right)/2) mergeSort(A, left, mid) mergeSort(A, mid, right) merge(A, left, mid, right) n=int(input()) S=list(map(int,input().split())) mergeSort(S,0,n) print(' '.join(map(str,S))) print(cnt)
# -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 class ModTools: """ 階乗たくさん使う時用のテーブル準備 """ def __init__(self, MAX, MOD): """ MAX:階乗に使う数値の最大以上まで作る """ MAX += 1 self.MAX = MAX self.MOD = MOD # 階乗テーブル factorial = [1] * MAX factorial[0] = factorial[1] = 1 for i in range(2, MAX): factorial[i] = factorial[i-1] * i % MOD # 階乗の逆元テーブル inverse = [1] * MAX # powに第三引数入れると冪乗のmod付計算を高速にやってくれる inverse[MAX-1] = pow(factorial[MAX-1], MOD-2, MOD) for i in range(MAX-2, 0, -1): # 最後から戻っていくこのループならMAX回powするより処理が速い inverse[i] = inverse[i+1] * (i+1) % MOD self.fact = factorial self.inv = inverse def nCr(self, n, r): """ 組み合わせの数 (必要な階乗と逆元のテーブルを事前に作っておく) """ if n < r: return 0 # 10C7 = 10C3 r = min(r, n-r) # 分子の計算 numerator = self.fact[n] # 分母の計算 denominator = self.inv[r] * self.inv[n-r] % self.MOD return numerator * denominator % self.MOD H, W, Y2, X1 = MAP() Y1 = H - Y2 X2 = W - X1 mt = ModTools(H+W, MOD) ans = 0 for x in range(1, X2+1): # 上の方の通り数 cnt1 = mt.nCr(Y1+X1+x-2, Y1-1) # 下の方の通り数 cnt2 = mt.nCr(Y2+X2-x-1, Y2-1) ans += cnt1 * cnt2 ans %= MOD print(ans)
A,B = map(int,input().split()) if A+B>=A-B and A+B>=A*B: print(A+B) elif A-B>=A+B and A-B>=A*B: print(A-B) else: print(A*B)
n = int(input()) result = "" while n>0: if n%26 == 0: result = "z" + result n=n//26-1 else: result= (chr(ord("a")+n%26-1)) + result n=n//26 print(result)
# coding: utf-8 # Your code here! S = input() w = int(input()) if len(S) / w == len(S) // w: length = len(S) // w else: length = len(S) // w + 1 for i in range(length): print(S[w * i], end = "")
S = input() A = 0 for i in range(len(S)): if S[i] == "F": A = i if S.count("C") > 0: if S.index("C") < A: print("Yes") else: print("No") else: print("No")
import math r = raw_input() r = float(r) a = r*r*math.pi b = 2 * r *math.pi print "%.9f %.9f"%(a,b)
a, b, c = sorted(map(int, input().split())) print('Yes' if a < c and (a == b or b == c) else 'No')
def main(): X = int(input()) if X <= 3: print(1) return ans = 0 for n in range(2, X): p = 2 while pow(n, p) <= X: if ans < pow(n, p): ans = pow(n, p) p += 1 print(ans) main()
import math L=int(input()) num=L/3 print('{:.07f}'.format(num**3))
N=int(input()) i=0 while pow(2, i+1)<=N: i+=1 print(pow(2, i))
def resolve(): ''' code here ''' from decimal import Decimal a, b, c = [int(item) for item in input().split()] x = c - a - b y = 2 * Decimal(a*b).sqrt() if y < x: print('Yes') else: print('No') if __name__ == "__main__": resolve()
def f(N): if N>999: return "ABD" else: return "ABC" N=int(input()) print(f(N))
import math n = int(raw_input()) degree = math.pi / 3 def print_point(point): print "%f %f" % (point[0], point[1]) def koch(depth, p1, p2): if depth == 0: return s = ((2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3) t = ((p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3) u = ((t[0] - s[0]) * math.cos(degree) - (t[1] - s[1]) * math.sin(degree) + s[0], (t[0] - s[0]) * math.sin(degree) + (t[1] - s[1]) * math.cos(degree) + s[1]) depth -= 1 koch(depth, p1, s) print_point(s) koch(depth, s, u) print_point(u) koch(depth, u, t) print_point(t) koch(depth, t, p2) start = (0.0, 0.0) end = (100.0, 0.0) print_point(start) koch(n, start, end) print_point(end)
s = input() day = 15 - len(s) count = s.count("o") if count + day >= 8: print("YES") else: print("NO")
arr = [] temp = [] for i in range(3): t = list(map(int, input().split())) arr.append(t) temp.extend(t) n = int(input()) for i in range(n): a = int(input()) if a in temp: for i in range(3): for j in range(3): if arr[i][j] == a: arr[i][j] = 'X' if arr[0][j] == arr[1][j] == arr[2][j] or arr[0][0] == arr[1][1] == arr[2][2] or arr[0][2] == arr[1][1] == arr[2][0] or set(arr[i]) == {'X'}: print('Yes') exit() print('No')
a,b=map(int,input().split()) n=[1,3,5,7,8,10,12] m=[4,6,9,11] o=[2] p=[n,m,o] if (a in n and b in n) or (a in m and b in m) or (a in o and b in o): print('Yes') else: print('No')
X = int(input()) ans = "YES" if X == 7 or X == 5 or X == 3 else "NO" print(ans)
def bubble_sort(A): flag = True swap = 0 while flag: flag = False for j in range(len(A)-1,0,-1): if A[j] < A[j-1]: A[j],A[j-1] = A[j-1],A[j] swap += 1 flag = True return swap if __name__=='__main__': N=int(input()) A=list(map(int,input().split())) swap = bubble_sort(A) print(*A) print(swap)
# 文字の取得 ori_str = str(input()) strnum = len(ori_str) # 奇数文字だけを抜き出して出力 odd_str = "" for cnt in range(0,strnum,2): odd_str += ori_str[cnt] print(odd_str)
N = 10**7 M = 10**5 arr = list(range(M)) def f(x): return x for i in range(N): b = arr[i % M] b = f(b) a = int(input()) print(a + a**2 + a**3)
a=input() if len(a)==3: print(a[::-1]) else: print(a)
def p(x): print('{0:.6f}'.format(x)) from math import sqrt,sin,cos,pi a,b,C = map(float,input().split()) theta = pi*C/180 h,c = 0.0,0.0 if theta<=pi/2: h = b*sin(theta) x = sqrt(b*b-h*h) c = sqrt(h**2+(a-x)**2) else: phi = theta-pi/2 h = b*cos(phi) d = b*sin(phi) e = a*h/(a+d) f = sqrt(d**2+(h-e)**2) g = sqrt(a**2+e**2) c = f+g S = a*h/2 L = a+b+c p(S) p(L) p(h)
def gcd(x, y): '''?????????????????????????????? x>=y?????¨??????gcd(x, y)??¨gcd(y, x%y)?????????????????¨????????¨??????''' if x < y: x, y = y, x while y > 0: x, y = y, x % y return x def test(): '''??? input = 147 105 -> 21''' x, y = map(int, input().split()) result = gcd(x, y) print(result) if __name__ == '__main__': test()
num = int(input()) if num == 1: print(0) elif num == 0: print(1)
def gcd(a,b): if b == 1: return 1 elif b == 0: return a else: return gcd(b,a%b) a,b = map(int, raw_input().split(' ')) if a > b: print gcd(a,b) else: print gcd(b,a)
import itertools as itr if __name__ == '__main__': for i in itr.count(1): # i = 1,2,3,... num = int(input()) if num == 0: break print("Case {0}: {1}".format(i, num))
import numpy as np n = int(input()) a = list(map(int, input().split())) a.sort() def get_nearest_value(arr, num): """ 概要: リストからある値に最も近い値を返却する関数 @param list: データ配列 @param num: 対象値 @return 対象値に最も近い値 """ # リスト要素と対象値の差分を計算し最小値のインデックスを取得 idx = np.abs(np.asarray(arr) - num).argmin() return arr[idx] first = a[-1] second = get_nearest_value(a, first / 2) print(first, second)
apple = list(map(str, input().split())) print("YES" if apple.count("5") == 2 and apple.count("7") == 1 else "NO")
""" ABC146D Diff: 1175 """ # BFSで各親の子供をループして調べていく. # 各ノードの周りにある色を集合で持ったらTLE # -> 色を塗る規則に注意すれば各親に対してintを1つ用意すれば十分 from collections import deque import sys input = sys.stdin.readline def bfs(v, N, G, E): #訪れたかどうか visited = [0]*N queue = deque() K = -1 # 各ノードで使っている色の中で最大の色 node2color = [-1 for _ in range(N)] queue.append(v) visited[v] = 1 while queue: q = queue.popleft() # 使う色(int)を初期化: 0から++していくことで、 # 調べる親qの子に対してできるだけ小さい色を塗れる。 color = 0 for nex in G[q]: if visited[nex]: #すでに訪れていたらcontinue continue visited[nex] = 1 color += 1 if color == node2color[q]: color += 1 node2color[nex] = color # 辞書に記録 E[(min(q, nex), max(q, nex))] = color queue.append(nex) K = max(K, color) return K def main(): N = int(input()) G = [deque() for _ in range(N)] E = dict() # (a, b) -> color for _ in range(N-1): a, b = map(lambda x: int(x)-1, input().split()) G[a].append(b) G[b].append(a) E[(a, b)] = 0 #辺->色の辞書 K = bfs(0, N, G, E) # ans_list = [value for value in E.values()] print(K) for value in E.values(): print(value) main()
from copy import deepcopy from copy import copy class Dice: def __init__(self, labels): self.surface = {x: labels[x-1] for x in range(1, 6+1)} def move_towards(self, direction): if direction == 'N': self._move(1, 2, 6, 5) elif direction == 'S': self._move(1, 5, 6, 2) elif direction == 'E': self._move(1, 4, 6, 3) elif direction == 'W': self._move(1, 3, 6, 4) def get_upper(self): return self.surface[1] def _move(self, i, j, k, l): temp_label = self.surface[i] self.surface[i] = self.surface[j] self.surface[j] = self.surface[k] self.surface[k] = self.surface[l] self.surface[l] = temp_label def find_pattern(self, pattern): dice = deepcopy(self) if dice.surface[1] == pattern[0] and dice.surface[2] == pattern[1]: return dice.surface[3] pattern_tested = {'original': dice.surface} moves = ['N', 'S', 'E', 'W'] def find(dice, moves): next_moves = [] for move in moves: if len(move) == 1: start = 'original' else: start = move[:-1] dice.surface =pattern_tested[start] dice.move_towards(move[-1]) if dice.surface[1] == pattern[0] and dice.surface[2] == pattern[1]: right_label = dice.surface[3] return right_label else: move_tested = move pattern_tested[move_tested] = copy(dice.surface) next_moves.extend([move+'N', move+'S', move+'E', move+'W']) return find(dice, next_moves) return find(dice, moves) labels = list(map(int, input().split())) num_questions = int(input()) questions = [list(map(int, input().split())) for _ in range(num_questions)] dice = Dice(labels) results = [] for question in questions: right_label = dice.find_pattern(question) results.append(right_label) for result in results: print(result)
from typing import List, Tuple def insertion_sort(A: List[int], n: int, g: int) -> int: count: int = 0 for i in range(g, n): j = i - g while j >= 0 and A[j] > A[j + g]: A[j], A[j + g] = A[j + g], A[j] j -= g count += 1 return count def shell_sort(A: List[int], n: int) -> Tuple[int, List[int], int, List[int]]: cnt: int = 0 G: List[int] = [1] h: int = 1 while 3 * h + 1 < n: h = 3 * h + 1 G.append(h) G.reverse() m = len(G) for i in range(m): count = insertion_sort(A, n, G[i]) cnt += count return m, G, cnt, A def main(): n: int = int(input()) A: List[int] = [] for _ in range(n): A.append(int(input())) m, G, cnt, A_sorted = shell_sort(A[:], n) print(m) print(*G, sep=" ") print(cnt) print(*A_sorted, sep="\n") if __name__ == "__main__": main()
s = input() c_idx = len(s) f_idx = -1 for i in range(len(s)): if s[i] == 'C': c_idx = min(c_idx, i) if s[i] == 'F': f_idx = max(f_idx, i) print('Yes' if c_idx < f_idx else 'No')
class UnionFind: def __init__(self, n): self.n = n self.par = [i for i in range(n)] # parent. if par[x]==x: x is root. self.rank = [0 for i in range(n)] # depth of tree def find(self, x): # find root if(self.par[x] == x): return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if(x == y): return if(self.rank[x] < self.rank[y]): self.par[x] = y else: self.par[y] = x if(self.rank[x] == self.rank[y]): self.rank[x] += 1 def same(self, x, y): return self.find(x) == self.find(y) def is_root(self, x): return self.par[x] == x def roots(self): return [i for i in range(self.n) if self.is_root(i)] def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def groups(self): return {r: self.members(r) for r in self.roots()} def main(): N, M = map(int, input().split()) p = list(map(int, input().split())) p = list(map(lambda x: x - 1, p)) UF = UnionFind(N) for i in range(M): x, y = map(int, input().split()) UF.unite(x - 1, y - 1) ans = 0 for i in range(N): if UF.same(i, p[i]): ans += 1 print(ans) if __name__ == '__main__': main()