input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
from math import floor,ceil H,W = list(map(int,input().split())) if H%3==0 or W%3==0: print((0)) exit() ans = min(H,W) for i in range(1,W): M = max(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i)) m = min(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i)) if M-m<ans: ans = M-m for i in range(1,H): M = max(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i)) m = min(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i)) if M-m<ans: ans = M-m print(ans)
H,W = list(map(int,input().split())) def split2(h,w): if h%2==0 or w%2==0: return h*w//2,h*w//2 else: h,w = max(h,w),min(h,w) return h//2*w,(h+1)//2*w ans = H*W for i in range(1,H): h = H-i w = W s1 = W*i s2,s3 = split2(h,w) ans = min(max(s1,s2,s3)-min(s1,s2,s3),ans) for i in range(1,W): h = H w = W-i s1 = H*i s2,s3 = split2(h,w) ans = min(max(s1,s2,s3)-min(s1,s2,s3),ans) print(ans)
p03713
H, W = list(map( int, input().split())) if H%3 == 0 or W%3 == 0: ans = 0 else: ans = min( max((H//3+1)*W, (H//3)*W)- min((H//3+1)*W, (H//3)*W), max((W//3+1)*H, (W//3)*H)- min((W//3+1)*H, (W//3)*H)) for i in range(H+1): ans = min( ans, max(W*i, (H-i)*(W//2), (H-i)*(W - W//2)) - min(W*i, (H-i)*(W//2), (H-i)*(W - W//2))) for j in range(W+1): ans = min( ans, max(H*j, (W-j)*(H//2), (W-j)*(H - H//2)) - min(H*j, (W-j)*(H//2), (W-j)*(H - H//2))) print(ans)
#import sys #input = sys.stdin.readline def main(): H, W = list(map( int, input().split())) ans = min(H,W) if H%3 == 0 or W%3 == 0: ans = 0 for i in range(1,H): L = [W*i] L.append(W//2*(H-i)) if W%2 == 1: L.append((W//2+1)*(H-i)) if max(L) - min(L) < ans: ans = max(L) - min(L) for i in range(1,W): L = [H*i] L.append(H//2*(W-i)) if H%2 == 1: L.append((H//2+1)*(W-i)) if max(L) - min(L) < ans: ans = max(L) - min(L) print(ans) if __name__ == '__main__': main()
p03713
H, W = list(map(int, input().split())) def get_ans(A, B): ans = A * B for A1 in range(1, A): A2 = (A - A1) // 2 A3 = A - A1 - A2 S = [A1 * B, A2 * B, A3 * B] ans = min(ans, max(S) - min(S)) for A1 in range(1, A): B2 = B // 2 B3 = B - B2 S = [A1 * B, (A - A1) * B2, (A - A1) * B3] ans = min(ans, max(S) - min(S)) return ans print((min(get_ans(H, W), get_ans(W, H))))
H, W = list(map(int, input().split())) def getMinDiff(A, B): if A % 3 == 0: return 0 elif B % 2 == 0: return B // 2 else: ans = B # 横3段に分ける場合の値 # T字型に分ける場合 for i in range(A % 3 + 1): A1 = A // 3 + i # 上半分の縦幅 B1, B2 = B // 2, (B + 1) // 2 # 下半分の左側、右側の横幅 Ss = [A1 * B, (A - A1) * B1, (A - A1) * B2] ans = min(ans, max(Ss) - min(Ss)) return ans print((min(getMinDiff(H, W), getMinDiff(W, H))))
p03713
H, W = list(map(int, input().split())) result = [] for h in range(1, H): Sa = W * h remain = H - h h2 = (remain + 1) // 2 Sb1 = W * h2 Sc1 = W * (remain - h2) result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1)) w = (W + 1) // 2 Sb2 = w * remain Sc2 = (W - w) * remain result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2)) for w in range(1, W): Sa = H * w remain = W - w w3 = (remain + 1) // 2 Sb1 = H * w3 Sc1 = H * (remain - w3) result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1)) h = (H + 1) // 2 Sb2 = h * remain Sc2 = (H - h) * remain result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2)) print((min(result)))
H, W = list(map(int, input().split())) result = [] for h in range(1, H//2+1): Sa = W * h remain = H - h h2 = (remain + 1) // 2 Sb1 = W * h2 Sc1 = W * (remain - h2) result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1)) w = (W + 1) // 2 Sb2 = w * remain Sc2 = (W - w) * remain result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2)) for w in range(1, W//2+1): Sa = H * w remain = W - w w3 = (remain + 1) // 2 Sb1 = H * w3 Sc1 = H * (remain - w3) result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1)) h = (H + 1) // 2 Sb2 = h * remain Sc2 = (H - h) * remain result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2)) print((min(result)))
p03713
H,W = list(map(int,input().split())) #l = sorted([int(input()) for i in range(N)],reverse=True) def three(h,w): ans = h*w for i in range(1,h): A = w*i B = w*((h-i)//2) C = w*((h-i+1)//2) ans = min(ans,max(A,B,C)-min(A,B,C)) return ans def onetwo(h,w): ans = h*w for i in range(1,h): A = w*i B = (h-i)*(w//2) C = (h-i)*((w+1)//2) ans = min(ans,max(A,B,C)-min(A,B,C)) return ans print((min(three(H,W),three(W,H),onetwo(H,W),onetwo(W,H))))
H,W = list(map(int,input().split())) #l = sorted([int(input()) for i in range(N)],reverse=True) def three(h,w): ans = h*w for i in range(h//3,h//3+2): A = w*i B = w*((h-i)//2) C = w*((h-i+1)//2) ans = min(ans,max(A,B,C)-min(A,B,C)) return ans def onetwo(h,w): ans = h*w for i in range(h//3,h//3+2): A = w*i B = (h-i)*(w//2) C = (h-i)*((w+1)//2) ans = min(ans,max(A,B,C)-min(A,B,C)) return ans print((min(three(H,W),three(W,H),onetwo(H,W),onetwo(W,H))))
p03713
def solve(h, w): res = float('inf') for hh in range(h + 1): s1 = hh * w ww = w // 2 s2 = (h - hh) * ww s3 = (h - hh) * (w - ww) res = min(res, max(s1, s2, s3) - min(s1, s2, s3)) return res H, W = list(map(int, input().split())) ans = H * W if H % 3 == 0 or W % 3 == 0: ans = 0 else: ans = min(H, W) ans = min(ans, solve(H, W)) ans = min(ans, solve(W, H)) print(ans)
def calc(h, w): w_1_1 = w // 3 w_1_2 = w_1_1 + 1 ans = inf for w_1 in w_1_1, w_1_2: s_1 = w_1 * h # 縦に割る w_2 = (w - w_1) // 2 s_2 = w_2 * h w_3 = w - w_1 - w_2 s_3 = w_3 * h ss = [s_1, s_2, s_3] # print(h, w, ss) ans = min(ans, max(ss) - min(ss)) # 横に割る h_2 = h // 2 s_2 = h_2 * (w - w_1) h_3 = h - h_2 s_3 = h_3 * (w - w_1) ss = [s_1, s_2, s_3] # print(h, w, ss) ans = min(ans, max(ss) - min(ss)) return ans inf = float('inf') h, w = list(map(int, input().split())) a = calc(h, w) b = calc(w, h) print((min(a, b)))
p03713
def main(): high, width = list(map(int, input().split())) ans = high * width for i in range(1, high): a = i * width b1 = (high - i) * (width // 2) c1 = high * width - a - b1 b2 = ((high - i) // 2) * width c2 = high * width - a - b2 kari = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2)) ans = min(ans, kari) for i in range(1, width): a = i * high b1 = (width - i) * (high // 2) c1 = high * width - a - b1 b2 = ((width - i) // 2) * high c2 = high * width - a - b2 kari = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2)) ans = min(ans, kari) print(ans) if __name__ == '__main__': main()
from collections import Counter, defaultdict import sys sys.setrecursionlimit(10 ** 5 + 10) # input = sys.stdin.readline from math import factorial import heapq, bisect import math import itertools def main(): high, width = list(map(int, input().split())) sum_s = high * width ans = sum_s for i in range(1, high): a = i * width b1 = (high - i) * (width // 2) b2 = ((high - i) // 2) * width c1 = sum_s - (a + b1) c2 = sum_s - (a + b2) ans = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2), ans) for i in range(1, width): a = i * high b1 = (width - i) * (high // 2) b2 = ((width - i) // 2) * high c1 = sum_s - (a + b1) c2 = sum_s - (a + b2) ans = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2), ans) print(ans) if __name__ == '__main__': main()
p03713
# -*- coding: utf-8 -*- H,W = list(map(int, input().split())) # # どちらかの辺が3の倍数なら確実に3等分できる # if H % 3 == 0 or W % 3 == 0: # print(0) # exit() # T字で区切るパターンを縦横それぞれ試す mnval = float("inf") for i in range(1,H): tmpA = i * W tmpB = (H-i) * (W // 2) tmpC = (H-i) * (W // 2 + W % 2) mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) for i in range(1,W): tmpA = i * H tmpB = (W-i) * (H // 2) tmpC = (W-i) * (H // 2 + H % 2) mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) # 縦並び横並びパターンをそれぞれ試す if W >= 3: tmpA = H * (W // 3) tmpB = H * (W // 3 + ((W % 3) // 2)) tmpC = H * (W // 3 + ((W % 3) // 2 + (W % 3) % 2)) mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) if H >= 3: tmpA = W * (H // 3) tmpB = W * (H // 3 + ((H % 3) // 2)) tmpC = W * (H // 3 + ((H % 3) // 2 + (H % 3) % 2)) mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) print(mnval)
# -*- coding: utf-8 -*- H,W = list(map(int, input().split())) # T字で区切るパターンを縦横それぞれ試す mnval = float("inf") for i in range(1,H): # Tの横棒に当たる方 tmpA = i * W # 縦2等分 tmpB = (H-i) * (W // 2) # 残り tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) for i in range(1,W): tmpA = i * H tmpB = (W-i) * (H // 2) tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) # 縦並び横並びパターンをそれぞれ試す if W >= 3: # 3等分 tmpA = H * (W // 3) # 3等分と、その余った部分の半分 tmpB = H * (W // 3 + ((W % 3) // 2)) # 残り tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) if H >= 3: tmpA = W * (H // 3) tmpB = W * (H // 3 + ((H % 3) // 2)) tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) print(mnval)
p03713
# -*- coding: utf-8 -*- H,W = list(map(int, input().split())) # T字で区切るパターンを縦横それぞれ試す mnval = float("inf") for i in range(1,H): # Tの横棒に当たる方 tmpA = i * W # 縦2等分 tmpB = (H-i) * (W // 2) # 残り tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) for i in range(1,W): tmpA = i * H tmpB = (W-i) * (H // 2) tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) # 縦並び横並びパターンをそれぞれ試す if W >= 3: # 3等分 tmpA = H * (W // 3) # 3等分と、その余った部分の半分 tmpB = H * (W // 3 + ((W % 3) // 2)) # 残り tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) if H >= 3: tmpA = W * (H // 3) tmpB = W * (H // 3 + ((H % 3) // 2)) tmpC = H*W - tmpA - tmpB mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC))) print(mnval)
# -*- 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 ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 H, W = MAP() if H%3==0 or W%3==0: print((0)) exit() H, W = max(H, W), min(H, W) a = H // 3 bc = H - a ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W)) a = ceil(H, 3) bc = H - a ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W)) H, W = min(H, W), max(H, W) a = H // 3 bc = H - a ans3 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W)) a = ceil(H, 3) bc = H - a ans4 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W)) ans5 = abs(ceil(W, 3)*H-W//3*H) ans6 = abs(ceil(H, 3)*W-H//3*W) print((min(ans1, ans2, ans3, ans4, ans5, ans6)))
p03713
def calc(H, W, ans): w0 = W // 2 w1 = W - w0 for h0 in range(1, H): Sa = h0 * W h1 = H - h0 h2 = h1 // 2 h3 = h1 - h2 Sb1, Sc1 = h1 * w0, h1 * w1 Sb2, Sc2 = h2 * W, h3 * W m1, _, M1 = sorted([Sa, Sb1, Sc1]) m2, _, M2 = sorted([Sa, Sb2, Sc2]) ans = min(ans, M1 - m1, M2 - m2) return ans H, W = list(map(int, input().split())) print((calc(W, H, calc(H, W, H * W))))
h, w = list(map(int, input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) else: a = (h // 3 + 1) * w - (h - 1 - h // 3) * (w // 2) b = (h - h // 3) * (w - w // 2) - h // 3 * w c = min(h, w) h, w = w, h d = (h // 3 + 1) * w - (h - 1 - h // 3) * (w // 2) e = (h - h // 3) * (w - w // 2) - h // 3 * w print((min(a, b, c, d, e)))
p03713
H,W = [int(i) for i in input().split()] ans = H * W for x in range(1,W): y1 = H // 2 Sa1 = H * x Sb1 = (W - x) * y1 Sc1 = (W - x) * (H - y1) M = max(Sa1,Sb1,Sc1) m = min(Sa1,Sb1,Sc1) y2 = (W - x) // 2 Sa2 = H * x Sb2 = H * y2 Sc2 = H * (W - x - y2) M2 = max(Sa2,Sb2,Sc2) m2 = min(Sa2,Sb2,Sc2) if ans > min(M-m,M2-m2): ans = min(M-m,M2-m2) H,W = W,H for x in range(1,W): y = H // 2 Sa1 = H * x Sb1 = (W - x) * y Sc1 = (W - x) * (H - y) M = max(Sa1,Sb1,Sc1) m = min(Sa1,Sb1,Sc1) y2 = (W - x) // 2 Sa2 = H * x Sb2 = H * y2 Sc2 = H * (W - x - y2) M2 = max(Sa2,Sb2,Sc2) m2 = min(Sa2,Sb2,Sc2) if ans > min(M-m,M2-m2): ans = min(M-m,M2-m2) print(ans)
def answer(H,W,ans_kari): for x in range(1,W): y1 = H // 2 Sa1 = H * x Sb1 = (W - x) * y1 Sc1 = (W - x) * (H - y1) M = max(Sa1,Sb1,Sc1) m = min(Sa1,Sb1,Sc1) y2 = (W - x) // 2 Sa2 = H * x Sb2 = H * y2 Sc2 = H * (W - x - y2) M2 = max(Sa2,Sb2,Sc2) m2 = min(Sa2,Sb2,Sc2) if ans_kari > min(M-m,M2-m2): ans_kari = min(M-m,M2-m2) return ans_kari if __name__ == '__main__': H,W = [int(i) for i in input().split()] ans = H * W ans = answer(H,W,ans) H,W = W,H if answer(H,W,ans) < ans: ans = answer(H,W,ans) print(ans)
p03713
def answer(H,W,ans): for x in range(1,W): y1 = H // 2 Sa1 = H * x Sb1 = (W - x) * y1 Sc1 = (W - x) * (H - y1) M = max(Sa1,Sb1,Sc1) m = min(Sa1,Sb1,Sc1) y2 = (W - x) // 2 Sa2 = H * x Sb2 = H * y2 Sc2 = H * (W - x - y2) M2 = max(Sa2,Sb2,Sc2) m2 = min(Sa2,Sb2,Sc2) if ans > min(M-m,M2-m2): ans = min(M-m,M2-m2) return ans if __name__ == '__main__': H,W = [int(i) for i in input().split()] ans = H * W ans = answer(H,W,ans) H,W = W,H if answer(H,W,ans) < ans: ans = answer(H,W,ans) print(ans)
def answer(H,W,ans): global ans_list for x in range(1,W): y1 = H // 2 S1 = [H * x,(W - x) * y1,(W - x) * (H - y1)] ans_list.append(max(S1)-min(S1)) y2 = (W - x) // 2 S2 = [H * x,H * y2,H * (W - x - y2)] ans_list.append(max(S2)-min(S2)) if __name__ == '__main__': H,W = [int(i) for i in input().split()] ans_list = [] answer(H,W,ans_list) answer(W,H,ans_list) print((min(ans_list)))
p03713
import sys, os, math, bisect, itertools, collections, heapq, queue # from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall from decimal import Decimal from collections import defaultdict, deque sys.setrecursionlimit(10000000) ii = lambda: int(sys.stdin.buffer.readline().rstrip()) il = lambda: list(map(int, sys.stdin.buffer.readline().split())) fl = lambda: list(map(float, sys.stdin.buffer.readline().split())) iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)] iss = lambda: sys.stdin.buffer.readline().decode().rstrip() sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split())) isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)] lcm = lambda x, y: (x * y) // math.gcd(x, y) MOD = 10 ** 9 + 7 MAX = float('inf') def main(): if os.getenv("LOCAL"): sys.stdin = open("input.txt", "r") H, W = il() if W % 3 == 0: print((0)) elif H % 3 == 0: print((0)) else: ret = MAX for h in range(1, H): # 横, 横, 横 分割パターン hh = (H - h) // 2 hmin = min(hh * W, (H - h - hh) * W, h * W) hmax = max(hh * W, (H - h - hh) * W, h * W) # 横, 縦, 縦 分割パターン ww = W // 2 wmin = min((H - h) * (W - ww), (H - h) * ww, h * W) wmax = max((H - h) * (W - ww), (H - h) * ww, h * W) ret = min(ret, abs(hmax - hmin), abs(wmax - wmin)) for w in range(1, W): # 縦, 縦, 縦 分割パターン ww = (W - w) // 2 wmin = min(w * H, (W - w - ww) * H, ww * H) wmax = max(w * H, (W - w - ww) * H, ww * H) # 縦, 横, 横 分割パターン hh = H // 2 hmin = min(w * H, hh * (W - w), (H - hh) * (W - w)) hmax = max(w * H, hh * (W - w), (H - hh) * (W - w)) ret = min(ret, abs(hmax - hmin), abs(wmax - wmin)) print(ret) if __name__ == '__main__': main()
h,w=list(map(int,input().split())) pattern=[h//2+w//3+1,h//3+w//2+1,h,w] if h%3==0 or w%3==0: pattern+=[0] if h%2==0: pattern+=[h//2] if w%2==0: pattern+=[w//2] print((min(pattern)))
p03713
h, w = list(map(int, input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) else: min_diff = h * w h1 = h // 2 h2 = h - h1 for w3 in range(w // 3, w // 3 + 3): s = [h * w3, h1 * (w - w3), h2 * (w - w3)] min_diff = min(min_diff, max(s) - min(s)) w1 = w // 2 w2 = w - w1 for h3 in range(h // 3, h // 3 + 3): s = [w * h3, w1 * (h - h3), w2 * (h - h3)] min_diff = min(min_diff, max(s) - min(s)) h1 = h // 3 h2 = (h - h1) // 2 h3 = h - h1 - h2 s = [w * h1, w * h2, w * h3] min_diff = min(min_diff, max(s) - min(s)) w1 = w // 3 w2 = (w - w1) // 2 w3 = w - w1 - w2 s = [h * w1, h * w2, h * w3] min_diff = min(min_diff, max(s) - min(s)) print(min_diff)
h, w = list(map(int, input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) else: min_diff = h * w h1 = h // 2 h2 = h - h1 for w3 in range(w // 3, w // 3 + 2): s = [h * w3, h1 * (w - w3), h2 * (w - w3)] min_diff = min(min_diff, max(s) - min(s)) w1 = w // 2 w2 = w - w1 for h3 in range(h // 3, h // 3 + 2): s = [w * h3, w1 * (h - h3), w2 * (h - h3)] min_diff = min(min_diff, max(s) - min(s)) h1 = h // 3 h2 = (h - h1) // 2 h3 = h - h1 - h2 s = [w * h1, w * h2, w * h3] min_diff = min(min_diff, max(s) - min(s)) w1 = w // 3 w2 = (w - w1) // 2 w3 = w - w1 - w2 s = [h * w1, h * w2, h * w3] min_diff = min(min_diff, max(s) - min(s)) print(min_diff)
p03713
def main(): a, b = list(map(int, input().split())) if (a % 3 ==0) | (b % 3 ==0): ans = 0 else: anss = [] s1 = (b // 3 + 1) * a s2 = (b - (b // 3 + 1)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3 - 1) * a s2 = (b - (b // 3) + 1) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) // 2 * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3 + 1)) // 2 * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) # b a t = a a = b b = t s1 = (b // 3 + 1) * a s2 = (b - (b // 3 + 1)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3 - 1) * a s2 = (b - (b // 3) + 1) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) // 2 * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3 + 1)) // 2 * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) ans = min(anss) print(ans) main()
a, b = list(map(int, input().split())) if (a % 3 ==0) | (b % 3 ==0): ans = 0 else: anss = [] s1 = (b // 3 + 1) * a s2 = (b - (b // 3 + 1)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3 - 1) * a s2 = (b - (b // 3) + 1) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b // 3) * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b // 3 + 1) * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) # b a t = a a = b b = t s1 = (b // 3 + 1) * a s2 = (b - (b // 3 + 1)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b - (b // 3)) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3 - 1) * a s2 = (b - (b // 3) + 1) * (a // 2) s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b // 3) * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) s1 = (b // 3) * a s2 = (b // 3 + 1) * a s3 = a * b - s1 - s2 anss.append(max(s1, s2, s3) - min(s1, s2, s3)) ans = min(anss) print(ans)
p03713
h, w = list(map(int, input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) else: if h > w: h, w = w, h ans = h for w1 in range(w//3-2, w//3+3): s1 = w1 * h w2 = w-w1 for h1 in range(h//2 + 1): s2 = w2 * h1 s3 = w2 * (h-h1) ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3)) h, w = w, h for w1 in range(w//3-2, w//3+3): s1 = w1 * h w2 = w-w1 for h1 in range(h//2 + 1): s2 = w2 * h1 s3 = w2 * (h-h1) ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3)) print(ans)
h, w = list(map(int, input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) else: ans = min(w, h) for w1 in range(w//3, w//3+3): s1 = w1 * h w2 = w-w1 for h1 in range(h//2, h//2+2): s2 = w2 * h1 s3 = w2 * (h-h1) ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3)) h, w = w, h for w1 in range(w//3, w//3+3): s1 = w1 * h w2 = w-w1 for h1 in range(h//2, h//2+2): s2 = w2 * h1 s3 = w2 * (h-h1) ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3)) print(ans)
p03713
# #    ⋀_⋀  #   (・ω・) # ./ U ∽ U\ # │* 合 *│ # │* 格 *│ # │* 祈 *│ # │* 願 *│ # │*   *│ #  ̄ # import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline from math import floor,ceil,sqrt,factorial,log #log2ないyp from heapq import heappop, heappush, heappushpop from collections import Counter,defaultdict from itertools import accumulate,permutations,combinations,product,combinations_with_replacement from bisect import bisect_left,bisect_right from copy import deepcopy inf=float('inf') mod = 10**9+7 def INT_(n): return int(n)-1 def MI(): return list(map(int,input().split())) def MF(): return list(map(float, input().split())) def MI_(): return list(map(INT_,input().split())) def LI(): return list(MI()) def LI_(): return [int(x) - 1 for x in input().split()] def LF(): return list(MF()) def LIN(n:int): return [I() for _ in range(n)] def LLIN(n: int): return [LI() for _ in range(n)] def LLIN_(n: int): return [LI_() for _ in range(n)] def LLI(): return [list(map(int, l.split() )) for l in eval(input())] def I(): return int(eval(input())) def F(): return float(eval(input())) def ST(): return input().replace('\n', '') def main(): H,W = MI() S = H*W def solve(H,W): ans = inf for h in range(H): if not (H-h)&1 or not W&1: piece = [h*W] piece.append(W*(H-h)/2) ans = min(ans, max(piece)-min(piece)) else: if (H-h)&1: piece = [h*W] piece.append((H-h-1)*W/2) piece.append((H-h+1)*W/2) ans = min(ans, max(piece)-min(piece)) if W&1: piece = [h*W] piece.append((W-1)*(H-h)/2) piece.append((W+1)*(H-h)/2) ans = min(ans, max(piece)-min(piece)) return ans ans1 = solve(H,W) ans2 = solve(W,H) print((int(min(ans1,ans2)))) if __name__ == '__main__': main()
# #    ⋀_⋀  #   (・ω・) # ./ U ∽ U\ # │* 合 *│ # │* 格 *│ # │* 祈 *│ # │* 願 *│ # │*   *│ #  ̄ # import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline from math import floor,ceil,sqrt,factorial,log #log2ないyp from heapq import heappop, heappush, heappushpop from collections import Counter,defaultdict from itertools import accumulate,permutations,combinations,product,combinations_with_replacement from bisect import bisect_left,bisect_right from copy import deepcopy inf=float('inf') mod = 10**9+7 def INT_(n): return int(n)-1 def MI(): return list(map(int,input().split())) def MF(): return list(map(float, input().split())) def MI_(): return list(map(INT_,input().split())) def LI(): return list(MI()) def LI_(): return [int(x) - 1 for x in input().split()] def LF(): return list(MF()) def LIN(n:int): return [I() for _ in range(n)] def LLIN(n: int): return [LI() for _ in range(n)] def LLIN_(n: int): return [LI_() for _ in range(n)] def LLI(): return [list(map(int, l.split() )) for l in eval(input())] def I(): return int(eval(input())) def F(): return float(eval(input())) def ST(): return input().replace('\n', '') def main(): H,W = MI() def solve(H,W): ans = inf for h in range(H): if not (H-h)&1 or not W&1: piece = [h*W, (W*(H-h))//2] ans = min(ans, max(piece)-min(piece)) else: if (H-h)&1: piece = [h*W, ((H-h-1)*W)//2, ((H-h+1)*W)//2] ans = min(ans, max(piece)-min(piece)) if W&1: piece = [h*W, ((W-1)*(H-h))//2, ((W+1)*(H-h))//2] ans = min(ans, max(piece)-min(piece)) return ans ans1 = solve(H,W) ans2 = solve(W,H) print((min(ans1,ans2))) if __name__ == '__main__': main()
p03713
h,w = list(map(int,input().split())) res = float('inf') # H for i in range(1,h): s_h = [i * w] s_w = [i * w] # のこりは(h-i)*w # H s_h.append(((h-i)//2) * w) s_h.append(((h-i)-(h-i)//2) * w) if 0 in ((h-i)//2,((h-i)-(h-i)//2),w): s_h.append(float('inf')) #print((h-i)//2,w) #print(((h-i)-(h-i)//2) , w) #print(s_h) # W s_w.append((h-i) * (w//2)) s_w.append((h-i) * (w - w//2)) if 0 in ((h-i),w//2,(w-(w//2))): s_w.append(float('inf')) #print((h-i) , w//2) #print((h-i) , (w - w//2)) #print(s_w) res_h = max(s_h) - min(s_h) res_w = max(s_w) - min(s_w) res = min(res,res_h) res = min(res,res_w) # W for i in range(1,w): s_h = [h * i] s_w = [h * i] # のこりはh*(w-i) # H s_h.append((h//2) * (w-i)) s_h.append((h - (h//2)) * (w-i)) if 0 in ((h//2),(w-i),(h-(h//2))): s_h.append(float('inf')) # W s_w.append(h * ((w-i)//2)) s_w.append(h * ((w-i) - (w-i)//2)) if 0 in (h,((w-i)//2),((w-i)-(w-i)//2)): s_w.append(float('inf')) res_h = max(s_h) - min(s_h) res_w = max(s_w) - min(s_w) res = min(res,res_h) res = min(res,res_w) print(res)
h,w=list(map(int,input().split())) r=1e9 f=lambda x:max(x)-min(x) for _ in 'aa': for i in range(1,h): p=[j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]] q=[j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]] r=min(min(r,f(p)),f(q)) h,w=w,h print(r)
p03713
h,w=list(map(int,input().split())) r=1e9 f=lambda x:max(x)-min(x) for _ in 'aa': for i in range(1,h): p=[j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]] q=[j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]] r=min(min(r,f(p)),f(q)) h,w=w,h print(r)
h,w=list(map(int,input().split())) r=1e9 f=lambda x:max(x)-min(x) for _ in 'aa': for i in range(1,h):r=min(min(r,f([j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]])),f([j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]])) h,w=w,h print(r)
p03713
h,w=list(map(int,input().split())) r=1e9 f=lambda x:max(x)-min(x) for _ in 'aa': for i in range(1,h):r=min(min(r,f([j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]])),f([j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]])) h,w=w,h print(r)
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]]),f([j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
p03713
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([j if j else -1e9for j in[i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]]),f([j if j else -1e9for j in[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]),f([i*w,(h-i)*(w//2),(h-i)*(w-w//2)]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
p03713
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([i*w,((h-i)//2)*w,((h-i)-(h-i)//2)*w]),f([i*w,(h-i)*(w//2),(h-i)*(w-w//2)]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([i,((h-i)//2),(h-i-(h-i)//2)])*w,f([i*w,(h-i)*(w//2),(h-i)*(w-w//2)]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
p03713
h,w=list(map(int,input().split()));f=lambda x:max(x)-min(x);g=lambda h,w:min([min(f([i,((h-i)//2),(h-i-(h-i)//2)])*w,f([i*w,(h-i)*(w//2),(h-i)*(w-w//2)]))for i in range(1,h)]);print((min(g(h,w),g(w,h))))
h,w = list(map(int,input().split())) if h % 3 == 0 or w % 3 == 0: print((0)) exit() res = 10**1000 for _ in range(2): a,b = h // 3 + 1, h // 3 + 1 c = h - a - b if c < 0: h,w = w,h continue mx = max(a,b,c) mn = min(a,b,c) res = min(res, mx*w-mn*w) h,w = w,h for _ in range(2): a,b = h // 3, h // 3 c = h - a - b mx = max(a,b,c) mn = min(a,b,c) res = min(res, mx*w-mn*w) h,w = w,h for _ in range(2): for i in range(1,h): l = [i * w] j = h - i a = w // 2 l.append(a * j) l.append((w - a) * j) res = min(res, max(l) - min(l)) h,w = w,h print(res)
p03713
H, W = list(map(int, input().split())) if (H%3) * (W%3) == 0: print((0)) quit() ans = H * W w1 = W // 2 w2 = W - w1 S1 = (H - 1) * w1; T1 = W * ((H-1)//2) S2 = (H - 1) * w2; T2 = W * (H-1) - T1 S3 = W h = H - 1 for i in range(H-1): diff1 = max(S1, S2, S3) - min(S1, S2, S3) diff2 = max(T1, T2, S3) - min(T1, T2, S3) ans = min(ans, diff1, diff2) S1 -= w1; S2 -= w2; S3 += W h -= 1 T1 = W * (h//2) T2 = W*h - T1 h1 = H // 2 h2 = H - h1 S1 = (W - 1) * h1; T1 = H * ((W-1)//2) S2 = (W - 1) * h2; T2 = H * (W-1) - T1 S3 = H w = W - 1 for i in range(W-1): diff1 = max(S1, S2, S3) - min(S1, S2, S3) diff2 = max(T1, T2, S3) - min(T1, T2, S3) ans = min(ans, diff1, diff2) S1 -= h1; S2 -= h2; S3 += H w -= 1 T1 = H * (w//2) T2 = H*w - T1 print(ans)
H, W = list(map(int, input().split())) if (H%3) * (W%3) == 0: print((0)) quit() ans = min(H, W) w1 = W // 2 w2 = W - w1 S1 = (H - 1) * w1 S2 = (H - 1) * w2 S3 = W for i in range(H-1): diff = max(S1, S2, S3) - min(S1, S2, S3) ans = min(ans, diff) S1 -= w1; S2 -= w2; S3 += W h1 = H // 2 h2 = H - h1 S1 = (W - 1) * h1 S2 = (W - 1) * h2 S3 = H for i in range(W-1): diff = max(S1, S2, S3) - min(S1, S2, S3) ans = min(ans, diff) S1 -= h1; S2 -= h2; S3 += H print(ans)
p03713
def func(H,W): area = H * W half_H = int(H/2) half_W = int(W/2) res = 10000000 # よこ # T for x in range(1,W): S1 = H * x S2_T = (W - x) * half_H S3_T = area - S1 - S2_T S2_2 = H * int((W - x)/2) S3_2 = area - S1 - S2_2 tmpres_T = max(S1, S2_T, S3_T) - min(S1, S2_T, S3_T) tmpres_2 = max(S1, S2_2, S3_2) - min(S1, S2_2, S3_2) if tmpres_T < res: res = tmpres_T if tmpres_2 < res: res = tmpres_2 # たて # T for y in range(1,H): S1 = W * y S2_T = (H - y) * half_W S3_T = area - S1 - S2_T S2_2 = W * int((H - y)/2) S3_2 = area - S1 - S2_2 tmpres_T = max(S1, S2_T, S3_T) - min(S1, S2_T, S3_T) tmpres_2 = max(S1, S2_2, S3_2) - min(S1, S2_2, S3_2) if tmpres_T < res: res = tmpres_T if tmpres_2 < res: res = tmpres_2 return res H, W = list(map(int, input().split())) print((func(H,W)))
def func(H,W): if H % 3 == 0: return 0 H_3 = int(H/3) S1 = H_3 * W S2 = (H - H_3) * int(W/2) S3 = H * W - S1 - S2 res1 = max(S1, S2, S3) - min(S1, S2, S3) H_3_1 = int(H/3) + 1 S1 = H_3_1 * W S2 = (H - H_3_1) * int(W/2) S3 = H * W - S1 - S2 res2 = max(S1, S2, S3) - min(S1, S2, S3) return min(res1 ,res2) H, W = list(map(int, input().split())) print((min(func(H,W), func(W,H), H, W)))
p03713
def c_ChocolateBar(H, W): ans = float('inf') # for文の順は解説における矩形の分け方の図の左からの順と一致 for h in range(1, H): S_A = h * W hh = (H - h) // 2 S_B = hh * W S_C = (H - h - hh) * W ans = min(ans, max([S_A, S_B, S_C]) - min([S_A, S_B, S_C])) for h in range(1, H): S_A = h * W w = W // 2 S_B = (H - h) * w S_C = (H - h) * (W - w) ans = min(ans, max([S_A, S_B, S_C]) - min([S_A, S_B, S_C])) for w in range(1, W): S_A = H * w ww = (W - w) // 2 S_B = H * ww S_C = H * (W - w - ww) ans = min(ans, max([S_A, S_B, S_C]) - min([S_A, S_B, S_C])) for w in range(1, W): S_A = H * w h = H // 2 S_B = h * (W - w) S_C = (H - h) * (W - w) ans = min(ans, max([S_A, S_B, S_C]) - min([S_A, S_B, S_C])) return ans H,W = [int(i) for i in input().split()] print((c_ChocolateBar(H, W)))
def c_chocolate_bar(H, W): # 変数a, b, cはeditorialの図のA, B, Cに対応する ans = float('inf') for _ in range(2): for i in range(1, H): a = i * W # 領域Aの面積 # 横に2つ切り込みを入れる(図の左から1番目の分け方) b = ((H - i) // 2) * W c = ((H - i + 1) // 2) * W ans = min(ans, abs(max(a, b, c) - min(a, b, c))) # 横縦に1つずつ切り込みを入れる(図の左から2番目の分け方) b = (W // 2) * (H - i) c = ((W + 1) // 2) * (H - i) ans = min(ans, abs(max(a, b, c) - min(a, b, c))) H, W = W, H # H,Wを入れ替えて同じことをする(図の左から3, 4番目の分け方) return ans H, W = [int(i) for i in input().split()] print((c_chocolate_bar(H, W)))
p03713
import sys readline = sys.stdin.readline from math import floor, ceil def main(): H, W = list(map(int, readline().rstrip().split())) ans = H * W if H >= 3: ans = min(ans, abs(W*(H//3) - W*(H-H//3*2))) if W >= 3: ans = min(ans, abs(H*(W//3) - H*(W-W//3*2))) # 縦にスライス + 残りに横にスライス # x1_cand = [floor(W/3), ceil(W/3)] x1_cand = [i for i in range(1, W)] y1 = H for x1 in x1_cand: x2 = W - x1 x3 = x2 y2 = H // 2 y3 = H - y2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) x2 = (W-x1) // 2 x3 = (W-x1-x2) ans = min(ans, max((x1*H), (x2*H), (x3*H)) - min((x1*H), (x2*H), (x3*H))) # 横にスライス + 残りに縦にスライス # y1_cand = [floor(H/3), ceil(H/3)] y1_cand = [i for i in range(1, H)] x1 = W for y1 in y1_cand: y2 = H - y1 y3 = y2 x2 = W // 2 x3 = W - x2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) y2 = (H-y1) // 2 y3 = (H-y1-y2) ans = min(ans, max((W*y1), (W*y2), (W*y3)) - min((W*y1), (W*y2), (W*y3))) print(ans) if __name__ == '__main__': main()
import sys readline = sys.stdin.readline from math import floor, ceil from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN def round(f, r=0): return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP) def main(): H, W = list(map(int, readline().rstrip().split())) ans = H * W if H >= 3: y1 = int(round(H/3)) y2 = (H-y1) // 2 y3 = H - y1 - y2 ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3)) if W >= 3: x1 = int(round(W/3)) x2 = (W-x1) // 2 x3 = W - x1 - x2 ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H)) # 縦にスライス + 残りに横にスライス x1_cand = [floor(W/3), ceil(W/3)] # x1_cand = [i for i in range(1, W)] y1 = H for x1 in x1_cand: x2 = W - x1 x3 = x2 y2 = H // 2 y3 = H - y2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) # 横にスライス + 残りに縦にスライス y1_cand = [floor(H/3), ceil(H/3)] # y1_cand = [i for i in range(1, H)] x1 = W for y1 in y1_cand: y2 = H - y1 y3 = y2 x2 = W // 2 x3 = W - x2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) print(ans) if __name__ == '__main__': main()
p03713
import sys readline = sys.stdin.readline from math import floor, ceil from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN def round(f, r=0): return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP) def main(): H, W = list(map(int, readline().rstrip().split())) ans = H * W if H >= 3: y1 = int(round(H/3)) y2 = (H-y1) // 2 y3 = H - y1 - y2 ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3)) if W >= 3: x1 = int(round(W/3)) x2 = (W-x1) // 2 x3 = W - x1 - x2 ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H)) # 縦にスライス + 残りに横にスライス x1_cand = [floor(W/3), ceil(W/3)] # x1_cand = [i for i in range(1, W)] y1 = H for x1 in x1_cand: x2 = W - x1 x3 = x2 y2 = H // 2 y3 = H - y2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) # 横にスライス + 残りに縦にスライス y1_cand = [floor(H/3), ceil(H/3)] # y1_cand = [i for i in range(1, H)] x1 = W for y1 in y1_cand: y2 = H - y1 y3 = y2 x2 = W // 2 x3 = W - x2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) print(ans) if __name__ == '__main__': main()
import sys readline = sys.stdin.readline from math import floor, ceil from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN def round(f, r=0): return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP) def main(): H, W = list(map(int, readline().rstrip().split())) ans = H * W if H >= 3: y1 = int(round(H/3)) y2 = (H-y1) // 2 y3 = H - y1 - y2 ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3)) if W >= 3: x1 = int(round(W/3)) x2 = (W-x1) // 2 x3 = W - x1 - x2 ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H)) # 縦にスライス + 残りに横にスライス x1_cand = [int(round(W/3))] y1 = H for x1 in x1_cand: x2 = W - x1 x3 = x2 y2 = H // 2 y3 = H - y2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) # 横にスライス + 残りに縦にスライス y1_cand = [int(round(H/3))] x1 = W for y1 in y1_cand: y2 = H - y1 y3 = y2 x2 = W // 2 x3 = W - x2 ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3))) print(ans) if __name__ == '__main__': main()
p03713
h,w=list(map(int,input().split())) if h%3==0 or w%3==0: print((0)) else: x=[] for i in range(1,w): sub=[] sub.append([h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]) sub.append([h*i,h*((w-i)//2+1),h*(w-i)-h*((w-i)//2+1)]) sub.append([h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]) sub.append([h*i,(w-i)*(h//2+1),h*(w-i)-(w-i)*(h//2+1)]) for j in sub: x.append(max(j)-min(j)) for i in range(1,h): sub=[] sub.append([w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]) sub.append([w*i,w*((h-i)//2+1),w*(h-i)-w*((h-i)//2+1)]) sub.append([w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]) sub.append([w*i,(h-i)*(w//2+1),w*(h-i)-(h-i)*(w//2+1)]) for j in sub: x.append(max(j)-min(j)) print((min(x)))
h,w=list(map(int,input().split())) if h%3==0 or w%3==0: print((0)) else: x=[] for i in range(1,w): sub=[] sub.append([h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]) #sub.append([h*i,h*((w-i)//2+1),h*(w-i)-h*((w-i)//2+1)]) sub.append([h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]) #sub.append([h*i,(w-i)*(h//2+1),h*(w-i)-(w-i)*(h//2+1)]) for j in sub: x.append(max(j)-min(j)) for i in range(1,h): sub=[] sub.append([w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]) #sub.append([w*i,w*((h-i)//2+1),w*(h-i)-w*((h-i)//2+1)]) sub.append([w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]) #sub.append([w*i,(h-i)*(w//2+1),w*(h-i)-(h-i)*(w//2+1)]) for j in sub: x.append(max(j)-min(j)) print((min(x)))
p03713
h,w=list(map(int,input().split())) if h%3==0 or w%3==0: print((0)) else: x=[] for i in range(1,w): sub=[] sub.append([h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]) #sub.append([h*i,h*((w-i)//2+1),h*(w-i)-h*((w-i)//2+1)]) sub.append([h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]) #sub.append([h*i,(w-i)*(h//2+1),h*(w-i)-(w-i)*(h//2+1)]) for j in sub: x.append(max(j)-min(j)) for i in range(1,h): sub=[] sub.append([w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]) #sub.append([w*i,w*((h-i)//2+1),w*(h-i)-w*((h-i)//2+1)]) sub.append([w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]) #sub.append([w*i,(h-i)*(w//2+1),w*(h-i)-(h-i)*(w//2+1)]) for j in sub: x.append(max(j)-min(j)) print((min(x)))
h,w=list(map(int,input().split())) x=[] for i in range(1,w): x1=[h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)] x1.sort() x.append(x1[-1]-x1[0]) x2=[h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)] x2.sort() x.append(x2[-1]-x2[0]) for i in range(1,h): x1=[w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)] x1.sort() x.append(x1[-1]-x1[0]) x2=[w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)] x2.sort() x.append(x2[-1]-x2[0]) print((min(x)))
p03713
from collections import defaultdict import sys input = sys.stdin.readline N = int(eval(input())) S = [input().rstrip() for _ in range(N)] S.sort(key=lambda s: -len(s)) dic = [defaultdict(int) for _ in range(26)] cnt1 = [0] * 26 ans = 0 for s in S: if len(s) == 1: ans += cnt1[ord(s[0]) - ord('a')] else: ans += dic[ord(s[0]) - ord('a')][s[1:][::-1]] cnt = [0] * 26 for c in s: cnt[ord(c) - ord('a')] += 1 for c in range(26): if cnt[c] > 0: cnt1[c] += 1 t = '' for i in range(len(s) - 1, -1, -1): t += s[i] cnt[ord(s[i]) - ord('a')] -= 1 for c in range(26): if cnt[c] > 0: dic[c][t] += 1 print(ans)
import sys input = sys.stdin.readline base = 1999849216943526259 mod = (1 << 61) - 1 N = int(eval(input())) S = [input().rstrip()[::-1] for _ in range(N)] S.sort(key=lambda s: -len(s)) cnt = {} ans = 0 one = [0] * 26 for s in S: n = len(s) if n == 1: ans += one[ord(s[0]) - ord('a')] continue hashes = [0] * (n - 1) hashes[0] = ord(s[0]) for i in range(1, n - 1): hashes[i] = (hashes[i-1] * base + ord(s[i])) % mod if hashes[-1] in cnt: ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')] found = [0] * 26 found[ord(s[-1]) - ord('a')] = 1 for i in range(n - 2, 0, -1): found[ord(s[i]) - ord('a')] = 1 if hashes[i-1] not in cnt: cnt[hashes[i-1]] = [0] * 26 for c in range(26): cnt[hashes[i-1]][c] += found[c] found[ord(s[0]) - ord('a')] = 1 for c in range(26): one[c] += found[c] print(ans)
p02589
import sys input = sys.stdin.readline base = 1000 mod = 10**23 + 117 N = int(eval(input())) S = [input().rstrip()[::-1] for _ in range(N)] S.sort(key=lambda s: -len(s)) cnt = {} ans = 0 one = [0] * 26 for s in S: n = len(s) if n == 1: ans += one[ord(s[0]) - ord('a')] continue hashes = [0] * (n - 1) hashes[0] = ord(s[0]) for i in range(1, n - 1): hashes[i] = (hashes[i-1] * base + ord(s[i])) % mod if hashes[-1] in cnt: ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')] found = [0] * 26 found[ord(s[-1]) - ord('a')] = 1 for i in range(n - 2, 0, -1): found[ord(s[i]) - ord('a')] = 1 if hashes[i-1] not in cnt: cnt[hashes[i-1]] = [0] * 26 for c in range(26): cnt[hashes[i-1]][c] += found[c] found[ord(s[0]) - ord('a')] = 1 for c in range(26): one[c] += found[c] print(ans)
import sys input = sys.stdin.readline base = 30 mod = 10**17 + 3 N = int(eval(input())) S = [input().rstrip()[::-1] for _ in range(N)] S.sort(key=lambda s: -len(s)) cnt = {} ans = 0 one = [0] * 26 for s in S: n = len(s) if n == 1: ans += one[ord(s[0]) - ord('a')] continue hashes = [0] * (n - 1) hashes[0] = ord(s[0]) - ord('a') + 1 for i in range(1, n - 1): hashes[i] = (hashes[i-1] * base + ord(s[i]) - ord('a') + 1) % mod if hashes[-1] in cnt: ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')] found = [0] * 26 found[ord(s[-1]) - ord('a')] = 1 for i in range(n - 2, 0, -1): found[ord(s[i]) - ord('a')] = 1 if hashes[i-1] not in cnt: cnt[hashes[i-1]] = [0] * 26 for c in range(26): cnt[hashes[i-1]][c] += found[c] found[ord(s[0]) - ord('a')] = 1 for c in range(26): one[c] += found[c] print(ans)
p02589
import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) def solve(): ordA = ord('a') N = int(eval(input())) Sss = [input().rstrip() for _ in range(N)] Sss.sort(key=lambda x: len(x)) fsss = [] Ass = [] for Ss in Sss: As = [ord(S)-ordA for S in Ss[::-1]] Ass.append(As) fss = [] fs = [0] * 26 for i in reversed(list(range(len(As)))): fs[As[i]] = 1 fss.append(fs[:]) fsss.append(fss[::-1]) ans = 0 nodes = [] nodes.append([-1]*26) nums = [] nums.append([0]*26) numNode = 1 for As, fss in zip(Ass, fsss): lenA = len(As) iNode = 0 for j in range(lenA): A = As[j] fs = fss[j] for c in range(26): if fs[c]: ans += nums[iNode][c] if nodes[iNode][A] == -1: nodes.append([-1]*26) nums.append([0]*26) nodes[iNode][A] = numNode numNode += 1 else: pass if j == lenA-1: nums[iNode][As[-1]] += 1 iNode = nodes[iNode][A] print(ans) solve()
import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) def solve(): ordA = ord('a') N = int(eval(input())) Ass = [] for _ in range(N): Ss = input().rstrip() As = [ord(S)-ordA for S in Ss[::-1]] Ass.append(As) Ass.sort(key=lambda x: len(x)) fsss = [] for As in Ass: fss = [] fs = [0] * 26 for i in reversed(list(range(len(As)))): fs[As[i]] = 1 fss.append(fs[:]) fsss.append(fss[::-1]) ans = 0 nodes = [] nodes.append([-1]*26) nums = [] nums.append([0]*26) numNode = 1 for As, fss in zip(Ass, fsss): lenA = len(As) iNode = 0 for j in range(lenA): A = As[j] fs = fss[j] for c in range(26): if fs[c]: ans += nums[iNode][c] if nodes[iNode][A] == -1: nodes.append([-1]*26) nums.append([0]*26) nodes[iNode][A] = numNode numNode += 1 else: pass if j == lenA-1: nums[iNode][As[-1]] += 1 iNode = nodes[iNode][A] print(ans) solve()
p02589
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from collections import deque, defaultdict #deque(l), pop(), append(x), popleft(), appendleft(x) #q.rotate(n)で → にn回ローテート from collections import Counter#文字列を個数カウント辞書に、 #S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items() from itertools import accumulate,combinations,permutations,product#累積和 #list(accumulate(l)) from heapq import heapify,heappop,heappush #heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone from functools import reduce,lru_cache#pypyでもうごく #@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率 from decimal import Decimal def input(): x=sys.stdin.readline() return x[:-1] if x[-1]=="\n" else x def printe(*x):print("## ",*x,file=sys.stderr) def printl(li): _=print(*li, sep="\n") if li else None def argsort(s, return_sorted=False): inds=sorted(range(len(s)), key=lambda k: s[k]) if return_sorted: return inds, [s[i] for i in inds] return inds def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65 def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65) def matmat(A,B): K,N,M=len(B),len(A),len(B[0]) return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)] def matvec(M,v): N,size=len(v),len(M) return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)] def T(M): n,m=len(M),len(M[0]) return [[M[j][i] for j in range(n)] for i in range(m)] def binr(x): return bin(x)[2:] def bitcount(x): #xは64bit整数 x= x - ((x >> 1) & 0x5555555555555555) x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333) x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) return x & 0x7f import random class rh:#base,max_n=ありうる文字の最大長,convertを指定すること def __init__(self,base,max_n):#baseとmaxnを指定すること。baseは偶数(modが奇数) self.mod=random.randrange(1<<54-1,1<<55-1,2)#奇数、衝突させられないよう乱数で生成 self.base=int(base*0.5+0.5)*2#偶数,英小文字なら26とか self.max_n=max_n self.pows1=[1]*(max_n+1) cur1=1 for i in range(1,max_n+1): cur1=cur1*base%self.mod self.pows1[i]=cur1 def convert(self,c): return alp2num(c,False)#大文字の場合Trueにする def get(self, s):#特定の文字のhash. O(|s|) n=len(s) mod=self.mod si=[self.convert(ss) for ss in s] h1=0 for i in range(n):h1=(h1+si[i]*self.pows1[n-1-i])%mod return h1 def get_roll(self, s, k, uroboros=False):#ローリングハッシュ,sの各文字を始点とした長さkの部分文字のhash値をすべて得る,O(|s|) #uroboros: trueの場合文字sを循環させ、n個のハッシュ値を得る。 falseの場合n-k+1個 n=len(s) maxitr=n-k+1 if uroboros: maxitr=n si=[self.convert(ss) for ss in s] mod=self.mod h1=0 for i in range(k):h1=(h1+si[i%n]*self.pows1[k-1-i])%mod hs=[0]*maxitr hs[0]=h1 mbase1=self.pows1[k] base=self.base for i in range(1,maxitr): front=si[i-1];back=si[(i+k-1)%n] h1=(h1*base-front*mbase1+back)%mod hs[i]=h1 return hs def main(): mod = 1000000007 #w.sort(key=itemgetter(1),reverse=True) #二個目の要素で降順並び替え N = int(input()) mod=1000000007 mod2=999999937 #N, K = map(int, input().split()) #A = tuple(map(int, input().split())) #1行ベクトル L=["" for _ in range(N)] ll=[0]*N for i in range(N): s=input() ls=len(s) ll[i]=ls L[i]=s #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列 inds=argsort(ll) d=Counter() ans=0 base=27 for im in inds: s=L[im] ls=len(s) cur=0 cur2=0 se=Counter(s) #printe(se) for i in range(ls): for a,val in se.items(): if val: ai=alp2num(a) ans+=d[(i+1,(cur*base+ai)%mod,(cur2*base+ai)%mod2)] si=s[-1-i] sin=alp2num(si) cur=(cur*base+sin)%mod cur2=(cur2*base+sin)%mod2 se[si]-=1 d[(ls,cur,cur2)]+=1 print(ans) if __name__ == "__main__": main()
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from collections import deque, defaultdict #deque(l), pop(), append(x), popleft(), appendleft(x) #q.rotate(n)で → にn回ローテート from collections import Counter#文字列を個数カウント辞書に、 #S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items() from itertools import accumulate,combinations,permutations,product#累積和 #list(accumulate(l)) from heapq import heapify,heappop,heappush #heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone from functools import reduce,lru_cache#pypyでもうごく #@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率 from decimal import Decimal def input(): x=sys.stdin.readline() return x[:-1] if x[-1]=="\n" else x def printe(*x):print("## ",*x,file=sys.stderr) def printl(li): _=print(*li, sep="\n") if li else None def argsort(s, return_sorted=False): inds=sorted(range(len(s)), key=lambda k: s[k]) if return_sorted: return inds, [s[i] for i in inds] return inds def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65 def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65) def matmat(A,B): K,N,M=len(B),len(A),len(B[0]) return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)] def matvec(M,v): N,size=len(v),len(M) return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)] def T(M): n,m=len(M),len(M[0]) return [[M[j][i] for j in range(n)] for i in range(m)] def binr(x): return bin(x)[2:] def bitcount(x): #xは64bit整数 x= x - ((x >> 1) & 0x5555555555555555) x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333) x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) return x & 0x7f import random class rh:#base,max_n=ありうる文字の最大長,convertを指定すること def __init__(self,base,max_n):#baseとmaxnを指定すること。baseは偶数(modが奇数) self.mod=random.randrange(1<<54-1,1<<55-1,2)#奇数、衝突させられないよう乱数で生成 self.base=int(base*0.5+0.5)*2#偶数,英小文字なら26とか self.max_n=max_n self.pows1=[1]*(max_n+1) cur1=1 for i in range(1,max_n+1): cur1=cur1*base%self.mod self.pows1[i]=cur1 def convert(self,c): return alp2num(c,False)#大文字の場合Trueにする def get(self, s):#特定の文字のhash. O(|s|) n=len(s) mod=self.mod si=[self.convert(ss) for ss in s] h1=0 for i in range(n):h1=(h1+si[i]*self.pows1[n-1-i])%mod return h1 def get_roll(self, s, k, uroboros=False):#ローリングハッシュ,sの各文字を始点とした長さkの部分文字のhash値をすべて得る,O(|s|) #uroboros: trueの場合文字sを循環させ、n個のハッシュ値を得る。 falseの場合n-k+1個 n=len(s) maxitr=n-k+1 if uroboros: maxitr=n si=[self.convert(ss) for ss in s] mod=self.mod h1=0 for i in range(k):h1=(h1+si[i%n]*self.pows1[k-1-i])%mod hs=[0]*maxitr hs[0]=h1 mbase1=self.pows1[k] base=self.base for i in range(1,maxitr): front=si[i-1];back=si[(i+k-1)%n] h1=(h1*base-front*mbase1+back)%mod hs[i]=h1 return hs def main(): mod = random.randrange(1<<54-1,1<<55-1,2) #w.sort(key=itemgetter(1),reverse=True) #二個目の要素で降順並び替え N = int(input()) #mod=1000000007 mod2=999999937 #N, K = map(int, input().split()) #A = tuple(map(int, input().split())) #1行ベクトル L=["" for _ in range(N)] ll=[0]*N for i in range(N): s=input() ls=len(s) ll[i]=ls L[i]=s #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列 inds=argsort(ll) d=Counter() ans=0 base=26 for im in inds: s=L[im] ls=len(s) cur=0 cur2=0 se=Counter(s) #printe(se) for i in range(ls): for a,val in se.items(): if val: ai=alp2num(a) ans+=d[(i+1,(cur*base+ai)%mod)] si=s[-1-i] sin=alp2num(si) cur=(cur*base+sin)%mod #cur2=(cur2*base+sin)%mod2 se[si]-=1 d[(ls,cur)]+=1 print(ans) if __name__ == "__main__": main()
p02589
# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) # from collections import defaultdict ALPHABET_SIZE = 26 def getlist(): return list(map(int, input().split())) class Node(object): def __init__(self, char, ALPHABET_SIZE): self.char = char self.children = [-1] * ALPHABET_SIZE self.flag = [0] * ALPHABET_SIZE class Trie(object): def __init__(self): self.root = Node(' ', ALPHABET_SIZE) def _convert_char_to_num(self, char): return ord(char) - ord('a') def insert(self, word, tail): res = 0 #合う値 current = self.root currentFlag = [0] * ALPHABET_SIZE for char in word: char_index = self._convert_char_to_num(char) for i in range(ALPHABET_SIZE): currentFlag[i] += current.flag[i] res += currentFlag[char_index] currentFlag[char_index] = 0 if current.children[char_index] != -1: current = current.children[char_index] else: new_node = Node(char, ALPHABET_SIZE) current.children[char_index] = new_node current = new_node char_index = self._convert_char_to_num(tail) res += currentFlag[char_index] current.flag[self._convert_char_to_num(tail)] += 1 return res #処理内容 def main(): N = int(eval(input())) L = [] for i in range(N): S = input()[::-1] L.append([len(S), S]) L.sort(key = lambda x:x[0]) ans = 0 trie = Trie() for val, s in L: s1 = s[:-1] tail = s[-1] ans += trie.insert(s1, tail) # print(ans) print(ans) if __name__ == '__main__': main()
# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) # from collections import defaultdict ALPHABET_SIZE = 26 def getlist(): return list(map(int, input().split())) class Node(object): def __init__(self, char): self.char = char self.children = {} self.flag = [0] * ALPHABET_SIZE class Trie(object): def __init__(self): self.root = Node(' ') def _convert_char_to_num(self, char): return ord(char) - 97 def insert(self, word, tail): res = 0 #合う値 current = self.root currentFlag = [0] * ALPHABET_SIZE for char in word: char_index = self._convert_char_to_num(char) for i in range(ALPHABET_SIZE): currentFlag[i] += current.flag[i] res += currentFlag[char_index] currentFlag[char_index] = 0 if char_index in current.children: current = current.children[char_index] # if current.children[char_index] != -1: # current = current.children[char_index] else: new_node = Node(char) current.children[char_index] = new_node current = new_node char_index = self._convert_char_to_num(tail) res += currentFlag[char_index] current.flag[self._convert_char_to_num(tail)] += 1 return res #処理内容 def main(): N = int(eval(input())) L = [] for i in range(N): S = input()[::-1] L.append([len(S), S]) L.sort(key = lambda x:x[0]) ans = 0 trie = Trie() for val, s in L: s1 = s[:-1] tail = s[-1] ans += trie.insert(s1, tail) # print(ans) print(ans) if __name__ == '__main__': main()
p02589
import sys,queue,math,copy,itertools,bisect,collections,heapq def main(): MOD = 2 ** 61 - 1 NI = lambda : int(sys.stdin.readline()) SI = lambda : sys.stdin.readline().rstrip() N = NI() cnt = collections.defaultdict(int) ans = 0 S = [SI() for _ in range(N)] for s in S: cnt[s[1:]] |= 1<<(ord(s[0])-ord('a')) for s in S: a = [] bit = 0 for x in s: x = ord(x) - ord('a') bit |= 1 << x a.append(bit) for i in range(len(s),1,-1): if i == len(s): d = '' else: d = s[i:] ans += bin(cnt[d] & a[i-1]).count('1') print(ans) if __name__ == '__main__': main()
import sys,queue,math,copy,itertools,bisect,collections,heapq def main(): MOD = 2 ** 61 - 1 NI = lambda : int(sys.stdin.readline()) SI = lambda : sys.stdin.readline().rstrip() N = NI() cnt = collections.defaultdict(int) ans = 0 S = [SI() for _ in range(N)] for s in S: h = 0 for i in range(len(s)-1,0,-1): h = (h * 27 + ord(s[i]) - ord('a') + 1) % MOD cnt[h] |= 1<<(ord(s[0])-ord('a')) for s in S: a = [] bit = 0 for x in s: x = ord(x) - ord('a') bit |= 1 << x a.append(bit) h = 0 for i in range(len(s)-1,0,-1): ans += bin(cnt[h] & a[i]).count('1') h = (h * 27 + ord(s[i]) - ord('a') + 1) % MOD print(ans) if __name__ == '__main__': main()
p02589
import sys input = sys.stdin.readline N = int(eval(input())) S = [input().rstrip()[::-1] for i in range(N)] S.sort(key=lambda x:-len(x)) P = 2**61 - 1 from collections import defaultdict,Counter D = defaultdict(lambda: Counter()) ans = 0 for s in S: sets = [set()] for c in s[::-1]: i = ord(c) - ord('a') sets.append(sets[-1] | {i}) sets.reverse() nxs = [] a = 0 for l,(c,st) in enumerate(zip(s,sets)): for j in st: na = (a*27 + (j+1)) % P nxs.append((na,l)) i = ord(c) - ord('a') + 1 a *= 27 a += i a %= P if a in D[len(s)-1]: ans += D[len(s)-1][a] for na,l in nxs: D[l][na] += 1 print(ans)
import sys input = sys.stdin.readline N = int(eval(input())) S = [input().rstrip()[::-1] for i in range(N)] S.sort(key=len) P = 2**61 - 1 from collections import defaultdict D = defaultdict(lambda: [0]*26) ans = 0 for s in S: l = ord(s[-1]) - ord('a') a = 0 tmp = [0] * 26 for c in s[:-1]: i = ord(c) - ord('a') if a in D: for j in range(26): tmp[j] += D[a][j] ans += tmp[i] tmp[i] = 0 a = (a*27 + i + 1) % P ans += tmp[l] D[a][l] += 1 print(ans)
p02589
import sys input = sys.stdin.readline class RollingHash: def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MOD = (1 << 61) - 1 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = (self.hash[i] + ord(char)) * self.BASE % self.MOD self.pow[i + 1] = (self.pow[i] * self.BASE) % self.MOD def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD return res n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: set_char.add(string[r - 1]) for char in set_char: if char == string[r - 1]: continue h = (rh.hash[r - 1] + ord(char)) * rh.BASE % rh.MOD if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
import sys input = sys.stdin.readline class RollingHash: def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MOD = (1 << 45) - 1 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = (self.hash[i] + ord(char)) * self.BASE % self.MOD self.pow[i + 1] = (self.pow[i] * self.BASE) % self.MOD def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD return res n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: set_char.add(string[r - 1]) for char in set_char: if char == string[r - 1]: continue h = (rh.hash[r - 1] + ord(char)) * rh.BASE % rh.MOD if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
p02589
import sys input = sys.stdin.readline class RollingHash: def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MOD = (1 << 61) - 1 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = (self.hash[i] * self.BASE + ord(char)) % self.MOD self.pow[i + 1] = (self.pow[i] * self.BASE) % self.MOD def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD return res n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: set_char.add(string[r - 1]) for char in set_char: if char == string[r - 1]: continue h = (rh.hash[r - 1] * rh.BASE + ord(char)) % rh.MOD if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
import sys input = sys.stdin.readline class RollingHash: def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MOD = 92709568269121 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = (self.hash[i] * self.BASE + ord(char)) % self.MOD self.pow[i + 1] = (self.pow[i] * self.BASE) % self.MOD def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD return res n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: set_char.add(string[r - 1]) for char in set_char: if char == string[r - 1]: continue h = (rh.hash[r - 1] * rh.BASE + ord(char)) % rh.MOD if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
p02589
import sys input = sys.stdin.readline class RollingHash: def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MASK30 = (1 << 30) - 1 self.MASK31 = (1 << 31) - 1 self.MASK61 = (1 << 61) - 1 self.MOD = self.MASK61 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = self.calc_mod(self.mul(self.hash[i], self.BASE) + ord(char)) self.pow[i + 1] = self.calc_mod(self.mul(self.pow[i], self.BASE)) def calc_mod(self, x): """x mod 2^61-1 を返す。""" xu = x >> 61 xd = x & self.MASK61 x = xu + xd if x >= self.MOD: x -= MASK61 return x def mul(self, a, b): """a*b mod 2^61-1 を返す。""" au = a >> 31 ad = a & self.MASK31 bu = b >> 31 bd = b & self.MASK31 mid = ad * bu + au * bd midu = mid >> 30 midd = mid & self.MASK30 return self.calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd) def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = self.calc_mod(self.hash[r] - self.mul(self.hash[l], self.pow[r - l])) return res n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: set_char.add(string[r - 1]) for char in set_char: if char == string[r - 1]: continue h = rh.calc_mod(rh.mul(rh.hash[r - 1], rh.BASE) + ord(char)) if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
import sys input = sys.stdin.readline class RollingHash: """文字列stringsの部分文字列のハッシュを構築する。O(N)""" def __init__(self, string): self.n = len(string) self.BASE = 1234 self.MASK30 = (1 << 30) - 1 self.MASK31 = (1 << 31) - 1 self.MASK61 = (1 << 61) - 1 self.MOD = self.MASK61 self.hash = [0] * (self.n + 1) self.pow = [1] * (self.n + 1) for i, char in enumerate(string): self.hash[i + 1] = self.calc_mod(self.mul(self.hash[i], self.BASE) + ord(char)) self.pow[i + 1] = self.calc_mod(self.mul(self.pow[i], self.BASE)) def calc_mod(self, x): """x mod 2^61-1 を返す。""" xu = x >> 61 xd = x & self.MASK61 x = xu + xd if x >= self.MOD: x -= MASK61 return x def mul(self, a, b): """a*b mod 2^61-1 を返す。""" au = a >> 31 ad = a & self.MASK31 bu = b >> 31 bd = b & self.MASK31 mid = ad * bu + au * bd midu = mid >> 30 midd = mid & self.MASK30 return self.calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd) def get_hash(self, l, r): """string[l:r]のハッシュ値を返す。O(1)""" res = self.calc_mod(self.hash[r] - self.mul(self.hash[l], self.pow[r - l])) return res def merge(self, h1, h2, length2): """ハッシュ値h1と長さlength2のハッシュ値h2を結合する。O(1)""" return self.calc_mod(self.mul(h1, self.pow[length2]) + h2) def get_lcp(self, l1, r1, l2, r2): """string[l1:r2]とstring[l2:r2]の長共通接頭辞(Longest Common Prefix)の 長さを求める。O(log|string|) """ ng = min(r1 - l1, r2 - l2) + 1 ok = 0 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if self.get_hash(l1, l1 + mid) == self.get_hash(l2, l2 + mid): ok = mid else: ng = mid return ok n = int(eval(input())) s = [input()[:-1] for i in range(n)] s.sort(key=len) str_set = set() cnt = 0 for string in s: string = string[::-1] # stringの後ろから見る n = len(string) rh = RollingHash(string) for r in range(1, n + 1): if rh.get_hash(0, r) in str_set: cnt += 1 set_char = set() for r in range(1, n + 1)[::-1]: h_str = rh.get_hash(0, r - 1) h_r = rh.get_hash(r - 1, r) set_char.add(h_r) for h1 in set_char: if h1 == h_r: continue h = rh.merge(h_str, h1, 1) if h in str_set: cnt += 1 str_set.add(rh.get_hash(0, n)) print(cnt)
p02589
import sys input = lambda: sys.stdin.readline().rstrip() P = 10 ** 40 + 121 N = int(eval(input())) X = sorted([input()[::-1] for _ in range(N)], key = len) D = {} ans = 0 for x in X: L = [0] * 26 T, a = x[:-1], ord(x[-1]) - 97 h = 0 for t in T: if h in D: for i in range(26): L[i] += D[h][i] b = ord(t) - 97 ans += L[b] L[b] = 0 h = (h + b + 135) * 71 % P ans += L[a] if h in D: D[h][a] += 1 else: D[h] = [0] * 26 D[h][a] = 1 print(ans)
import sys input = lambda: sys.stdin.readline().rstrip() m = 2 ** 150 - 1 N = int(eval(input())) X = sorted([input()[::-1] for _ in range(N)], key = len) D = {} ans = 0 for x in X: L = [0] * 26 T, a = x[:-1], ord(x[-1]) - 97 h = 0 for t in T: if h in D: for i in range(26): L[i] += D[h][i] b = ord(t) - 97 ans += L[b] L[b] = 0 h = (h * 71 + b + 135) & m ans += L[a] if h in D: D[h][a] += 1 else: D[h] = [0] * 26 D[h][a] = 1 print(ans)
p02589
mod1 = 1000000007 mod2 = 1000000009 base1 = 2009 base2 = 1007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N = int(eval(input())) S = [] for _ in range(N): s = input().rstrip('\n') S.append(s[::-1]) S.sort(key=lambda s: len(s), reverse=True) cnt = [] for s in S: c = [0] * 26 for ss in s: c[ord(ss) - 97] += 1 cnt.append(c) ans = 0 H1 = [0] * N H2 = [0] * N for L in range(1, 10**6+1): if not S: break M = len(S) key = [] i = M-1 dic_last = {} while i >= 0: if len(S[i]) == L: s = S.pop() cnt.pop() h1 = H1.pop() h2 = H2.pop() if L > 1: last = ord(s[L-2]) - 97 h1 = (h1 * base1 + last)%mod1 h2 = (h2 * base2 + last)%mod2 key.append((s, h1, h2)) else: if L > 1: ss = ord(S[i][L-2]) - 97 cnt[i][ss] -= 1 H1[i] = (H1[i] * base1 + ss)%mod1 H2[i] = (H2[i] * base2 + ss)%mod2 hh = H1[i] << 40 + H2[i] if hh not in dic_last: dic_last[hh] = [0] * 26 for j in range(26): if cnt[i][j] > 0: dic_last[hh][j] += 1 i -= 1 for s, h1, h2 in key: last = ord(s[-1]) - 97 h = h1 << 40 + h2 if L == 1: for i in range(len(cnt)): if cnt[i][last] > 0: ans += 1 else: if h in dic_last: ans += dic_last[h][last] #print(L, S, ans) print(ans) if __name__ == '__main__': main()
mod1 = 1000000007 mod2 = 1000000009 base1 = 2009 base2 = 1007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N = int(eval(input())) S = [] for _ in range(N): s = input().rstrip('\n') S.append(s[::-1]) S.sort(key=lambda s: len(s), reverse=True) cnt = [] for s in S: c = [0] * 26 for ss in s: c[ord(ss) - 97] += 1 cnt.append(c) ans = 0 H1 = [0] * N H2 = [0] * N for L in range(1, 10**6+1): if not S: break M = len(S) key = [] i = M-1 dic_last = {} while i >= 0: if len(S[i]) == L: s = S.pop() cnt.pop() h1 = H1.pop() h2 = H2.pop() if L > 1: last = ord(s[L-2]) - 97 h1 = (h1 * base1 + last)%mod1 h2 = (h2 * base2 + last)%mod2 key.append((s, h1, h2)) else: if L > 1: ss = ord(S[i][L-2]) - 97 cnt[i][ss] -= 1 H1[i] = (H1[i] * base1 + ss)%mod1 H2[i] = (H2[i] * base2 + ss)%mod2 hh = H1[i] * (mod2+1) + H2[i] if hh not in dic_last: dic_last[hh] = [0] * 26 for j in range(26): if cnt[i][j] > 0: dic_last[hh][j] += 1 i -= 1 for s, h1, h2 in key: last = ord(s[-1]) - 97 h = h1 * (mod2+1) + h2 if L == 1: for i in range(len(cnt)): if cnt[i][last] > 0: ans += 1 else: if h in dic_last: ans += dic_last[h][last] #print(L, S, ans) print(ans) if __name__ == '__main__': main()
p02589
import sys;input=sys.stdin.readline from collections import defaultdict N, = list(map(int, input().split())) S = [] for _ in range(N): s = input().strip() S.append(s) S.sort(key=lambda x:len(x)) D = defaultdict(set) R = 0 for s in S: l = len(s) d = defaultdict(int) for c in D[""]: d[c] += 1 for i in range(l-1, -1, -1): R += d[s[i]] d[s[i]] = 0 for c in D[s[i:]]: d[c] += 1 D[s[1:]].add(s[0]) print(R)
import sys;input=sys.stdin.readline N, = list(map(int, input().split())) S = [] for _ in range(N): s = input().strip() S.append(s) S.sort(key=lambda x:len(x)) D = dict() dd = set() R = 0 for s in S: l = len(s) d = [0]*26 for c in dd: d[c] += 1 mm = 0 for i in range(l-1, -1, -1): cc = ord(s[i]) - 97 R += d[cc] d[cc] = 0 if s[i:] in D: for c in D[s[i:]]: d[c] += 1 if i == 0: f = cc mm += 1 if l != 1: if s[1:] not in D: D[s[1:]] = set() D[s[1:]].add(f) else: dd.add(f) print(R)
p02589
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 4 0 1 2 1 2 1 1 3 3 output: 5 """ import sys from collections import deque from math import isinf def generate_adj_table(v_table): for each in v_table: source, target, cost = list(map(int, each)) init_adj_table[source].setdefault(target, cost) init_adj_table[target].setdefault(source, cost) return init_adj_table def graph_bfs(v_init): global distance distance = [float('inf')] * vertices distance[v_init] = 0 queue.appendleft(v_init) while queue: current = queue.popleft() adj_weight = adj_table[current] for adj in list(adj_weight.keys()): if isinf(distance[adj]): distance[adj] = distance[current] + adj_weight[adj] queue.append(adj) return None def solve(): graph_bfs(init_v) diameter, bridge_v = 0, 0 for v in range(vertices): if isinf(distance[v]): continue if diameter < distance[v]: diameter = distance[v] bridge_v = v graph_bfs(bridge_v) diameter = 0 for v in range(vertices): if isinf(distance[v]): continue diameter = max(diameter, distance[v]) return diameter if __name__ == '__main__': _input = sys.stdin.readlines() vertices = int(_input[0]) v_info = [x.split() for x in _input[1:]] queue = deque() distance = [float('inf')] * vertices init_adj_table = tuple(dict() for _ in range(vertices)) adj_table = generate_adj_table(v_info) init_v = 0 print((solve()))
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 4 0 1 2 1 2 1 1 3 3 output: 5 """ import sys from math import isinf from collections import deque def generate_adj_table(v_table, init_adj_table): for each in v_table: source, target, cost = list(map(int, each)) init_adj_table[source][target] = cost init_adj_table[target][source] = cost return init_adj_table def graph_bfs(current, v_num, adj_table): queue = deque() distance = [float('inf')] * v_num distance[current] = 0 queue.appendleft(current) while queue: current = queue.popleft() for adj, cost in list(adj_table[current].items()): if isinf(distance[adj]): distance[adj] = distance[current] + cost queue.append(adj) return distance def calc_tree_diameter(v_num, adj_table): # init x -> bridge vertex init_v = 0 distance_1 = graph_bfs(init_v, v_num, adj_table) diameter, bridge_v = 0, 0 for v, dis_1 in enumerate(distance_1): if isinf(dis_1): continue elif diameter < dis_1: diameter = dis_1 bridge_v = v # bridge vertex -> y distance_2 = graph_bfs(bridge_v, v_num, adj_table) diameter = 0 for dis_2 in distance_2: if isinf(dis_2): continue diameter = max(diameter, dis_2) return diameter def solve(): _input = sys.stdin.readlines() v_num = int(_input[0]) edges = [x.split() for x in _input[1:]] init_adj_table = tuple(dict() for _ in range(v_num)) adj_table = generate_adj_table(edges, init_adj_table) ans = calc_tree_diameter(v_num, adj_table) print(ans) return None if __name__ == '__main__': solve()
p02371
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[None] * n for i in range(n)] for l_i in f_i: s, t, w = list(map(int, l_i.split())) edges[s][t] = w edges[t][s] = w # dfs tree walk 1 import collections path = collections.deque() path.append(0) distance = 0 max_distance = 0 end_point = 0 unvisited = [False] + [True] * (n - 1) while True: u = path[-1] adj_e = edges[u] for v, e in enumerate(adj_e): if unvisited[v] and e != None: path.append(v) distance += e unvisited[v] = False if max_distance < distance: max_distance = distance end_point = v break if u == path[-1]: path.pop() if path: distance -= edges[u][path[-1]] else: break # Measurement of diameter(dfs tree walk 2) path.append(end_point) distance = 0 diameter = 0 unvisited = [True] * n unvisited[end_point] = False while True: u = path[-1] adj_e = edges[u] for v, e in enumerate(adj_e): if unvisited[v] and e != None: path.append(v) distance += e unvisited[v] = False if diameter < distance: diameter = distance end_point = v break if u == path[-1]: path.pop() if path: distance -= edges[u][path[-1]] else: break # output print(diameter)
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[] for i in range(n)] for l_i in f_i: s, t, w = list(map(int, l_i.split())) edges[s].append((t, w)) edges[t].append((s, w)) # Tree Walk function import collections def tree_walk(start, switch = 0): path = collections.deque() path.append(start) weight = collections.deque() weight.append(0) unvisited = [True] * n unvisited[start] = False rest = n - 1 end_point = start diameter = 0 while True: u = path[-1] for v, w in edges[u]: if unvisited[v]: path.append(v) unvisited[v] = False rest -= 1 weight.append(w) break if u == path[-1]: distance = sum(weight) if diameter < distance: diameter = distance end_point = u if rest == 0: break path.pop() weight.pop() if switch: print(diameter) else: return end_point # Output tree_walk(tree_walk(0), 1)
p02371
from heapq import heappop, heappush INF = 10 ** 20 n = int(eval(input())) edges = [[] for _ in range(n)] for _ in range(n - 1): s, t, w = list(map(int, input().split())) edges[s].append((w, t)) edges[t].append((w, s)) cost = [INF] * n que = [(0, 0)] cost[0] = 0 while que: acc, num = heappop(que) for weight, to in edges[num]: if weight + acc < cost[to]: cost[to] = weight + acc heappush(que, (weight + acc, to)) max_ind = cost.index(max(cost)) cost = [INF] * n que = [(0, max_ind)] cost[max_ind] = 0 while que: acc, num = heappop(que) for weight, to in edges[num]: if weight + acc < cost[to]: cost[to] = weight + acc heappush(que, (weight + acc, to)) print((max(cost)))
from heapq import heappop, heappush INF = 10 ** 20 def main(): n = int(eval(input())) edges = [[] for _ in range(n)] for _ in range(n - 1): s, t, w = list(map(int, input().split())) edges[s].append((w, t)) edges[t].append((w, s)) def func(start): cost = [INF] * n que = [(0, start)] cost[start] = 0 while que: acc, num = heappop(que) for weight, to in edges[num]: if weight + acc < cost[to]: cost[to] = weight + acc heappush(que, (weight + acc, to)) return cost cost1 = func(0) max_ind = cost1.index(max(cost1)) cost2 = func(max_ind) print((max(cost2))) main()
p02371
import sys readline = sys.stdin.readline from collections import deque from math import isinf INF = float("inf") sys.setrecursionlimit(200000) n = int(readline()) G = [[] for _ in range(n)] D = [INF] * n for _ in [0] * (n - 1): s, t, w = list(map(int, readline().split())) G[s].append([t, w]) G[t].append([s, w]) def bfs(s, D): dq = deque([s]) D = [INF] * n D[s] = 0 while dq: u = dq.popleft() for i in range(len(G[u])): e = G[u][i] if isinf(D[e[0]]): D[e[0]] = D[u] + e[1] dq.append(e[0]) return D D = bfs(0, D) maxv = 0 tgt = 0 for i in range(n): if isinf(D[i]): continue if maxv < D[i]: maxv = D[i] tgt = i D = bfs(tgt, D) print((max(D[i] for i in range(n) if not isinf(D[i]))))
import sys readline = sys.stdin.readline def dfs(root): visited = [False] * n queue = [(0, root)] longest = (-1, -1) while queue: total_weight, node = queue.pop() if visited[node]: continue visited[node] = True longest = max(longest, (total_weight, node)) for w, t in edges[node]: if not visited[t]: queue.append((total_weight + w, t)) return longest n = int(readline()) edges = [set() for _ in range(n)] for _ in range(n - 1): s, t, w = list(map(int, readline().split())) edges[s].add((w, t)) edges[t].add((w, s)) _, ln = dfs(0) ld, _ = dfs(ln) print(ld)
p02371
# -*- coding: utf-8 -*- from collections import deque if __name__ == '__main__': n = int(eval(input())) A = [[float("inf") for j in range(n)] for i in range(n)] for i in range(n - 1): A[i][i] = 0 s, t, d = list(map(int, input().split())) A[s][t] = d A[t][s] = d Q = deque() def bfs(s): d = [float("inf")] * n Q.append(s) d[s] = 0 while Q: u = Q.popleft() for v in range(n): if d[v] == float("inf"): d[v] = d[u] + A[u][v] Q.append(v) return d d = bfs(0) tgt = d.index(max(d)) d = bfs(tgt) print((max(d)))
# -*- coding: utf-8 -*- from collections import deque if __name__ == '__main__': n = int(eval(input())) E = [set() for _ in range(n)] for _ in range(n - 1): s, t, d = list(map(int, input().split())) E[s].add((t, d)) E[t].add((s, d)) Q = deque() def bfs(s): d = [float("inf")] * n Q.append(s) d[s] = 0 while Q: u = Q.popleft() for v, w in E[u]: if d[v] == float("inf"): d[v] = d[u] + w Q.append(v) return d d = bfs(0) tgt = d.index(max(d)) d = bfs(tgt) print((max(d)))
p02371
while True: a=[int(num)for num in input().split(' ')] n=a[0] if n==0:break hanafuda=[i+1 for i in range(n)] for i in range(a[1]): b=[int(num)for num in input().split(' ')] for j in range(b[1]):hanafuda.append(hanafuda.pop(n-(b[0]+b[1])+1)) print((hanafuda.pop()))
while True: n,r=[int(s)for s in input().split(" ")] if n==r==0:break ops=[[int(s)for s in input().split(" ")]for i in range(r)] cd=[n-i for i in range(n)] for op in ops: p,c=op l=cd[p-1:p+c-1] del cd[p-1:p+c-1] cd=l+cd print((cd[0]))
p00710
cond = [int(n) for n in input().split(" ")] while True: case = [] cards = [n for n in range(1,cond[0] + 1)] cards.sort(reverse = True) for c in range(cond[1]): case = [int(n) for n in input().split(" ")] case[0] -= 1 #print(cards[case[0]:case[0]+case[1]],cards[case[1]:case[0]+case[1]],cards[:case[1]],cards[:case[1]]) disp = cards[case[0]:case[0]+case[1]] cards[case[1]:case[0]+case[1]] = cards[0:case[0]] cards[0:case[1]] = disp else: print((cards[0])) cond = [int(n) for n in input().split(" ")] if cond[0] == 0 and cond[1] == 0: break else: continue
cards,trial = (int(n) for n in input().split(" ")) while True: cards = list(reversed([int(n) for n in range(1,(cards)+1)])) for n in range(trial): num,change = (int(n) for n in input().split(" ")) num -= 1 disp = cards[num:num+change] cards[change:change+num] = cards[:num] cards[:change] = disp print((cards[0])) cards,trial = (int(n) for n in input().split(" ")) if cards == 0 and trial == 0: break
p00710
import sys from collections import Counter from collections import deque import heapq import math import fractions import bisect import itertools def input(): return sys.stdin.readline().strip() def mp(): return list(map(int,input().split())) def lmp(): return list(map(int,input().split())) a,b=mp() print((a*b))
a,b=list(map(int,input().split())) print((a*b))
p02657
num = [int(i) for i in input().split() ] print((num[0] * num[1]))
A,B = [int(i) for i in input().split()] print((A*B))
p02657
a,b = list(map(int, input().split())) ans = a * b print(ans)
import math a,b = input().split() a = int(a) b = float(b) print((int(math.floor(a * b))))
p02657
s = list(map(int, input().split())) print((s[0]*s[1]))
s = list(map(int, input().split())) S = list(s) print((S[0]*S[1]))
p02657
import sys def input(): return sys.stdin.readline().strip() def resolve(): a,b=list(map(int, input().split())) print((a*b)) resolve()
import sys def input(): return sys.stdin.readline().strip() def resolve(): a,b=list(map(float, input().split())) b100=b*100 print((int(a*b100/100))) resolve()
p02657
def iput(): return int(eval(input())) def mput(): return list(map(int, input().split())) def lput(): return list(map(int, input().split())) def solve(): a, b = mput() print((a*b)) return 0 if __name__ == "__main__": solve()
import math def solve(): a, b = input().split() a = int(a) b = float(b) print((int(a*b))) return 0 if __name__ == "__main__": solve()
p02657
# 169A # 値 A × B を整数として出力せよ。 # 1.標準入力をプログラムで扱えるように受け取る a, b = list(map(int, input().split())) # print(a, b) # 2.受け取った入力値を使って、適切に処理(計算)する answer = a * b # 3.計算結果を出力する print(answer)
# A × B を求めてください。 # 制約 # 1 ≤ A ≤ 100 # 1 ≤ B ≤ 100 # 入力は全て整数である。 # 標準入力からA、Bを取得する a, b = list(map(int, input().split())) # A x B を計算し、出力する result = a * b print(result)
p02657
A, B = list(map(int, input().split())) C = A * B print(C)
A, B = input().split() A = int(A) B = float(B) B = int(B * 1000) C = A * B C //= 1000 print(C)
p02657
a,b=list(map(int,input().split())) print((a*b))
def resolve(): a,b=list(map(int,input().split())) print((a*b)) resolve()
p02657
# 169a # A×Bを整数として出力せよ。 # 1. 入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 2. 受け取った入力値を使って、適切に処理(計算)すること answer = a * b # 3. 計算した結果を出力すること print(answer)
A, B = list(map(int, input().split())) answer = A * B print(answer)
p02657
def LI():return list(map(int,input().split())) def yes():return print("Yes") def no():return print("No") from collections import deque, defaultdict, Counter from heapq import heappop, heappush # import math # pi=math.pi # print(pi) # yes() # no() # n=int(input()) # s=input() a,b=LI() print(a*b)
print((eval(input().replace(" ","*"))))
p02657
lis = list(map(int,input().split(" "))) print((lis[0] * lis[1]))
N,M = list(map(int,input().split())) print((N*M))
p02657
a, b = list(map(int, input().split())) print((a*b))
#!/usr/bin/env python3 import sys def solve(A: int, B: int): print((A*B)) return # Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template) def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() A = int(next(tokens)) # type: int B = int(next(tokens)) # type: int solve(A, B) if __name__ == '__main__': main()
p02657
# -*- coding: utf-8 -*- """ Created on Sun May 31 20:59:05 2020 @author: NEC-PCuser """ A, B = list(map(int, input().split())) print((A * B))
A, B = list(map(int, input().split())) print((str(A * B)))
p02657
#169A #A*Bを整数として出力せよ。 #1.入力を受け取ること a,b=list(map(int,input().split())) # print(a, b) #2.受け取った入力値を使って、適切に処理すること answer=a*b #3.計算した結果を出力すること print(answer)
a, b = list(map(int, input().split())) print((a*b))
p02657
A, B = list(map(int, input().split())) print((A*B))
A, B = list(map(float, (input().split()))) print((round(A*B)))
p02657
A,B = list(map(int,input().split(" "))) print((A*B))
if __name__ == "__main__": A,B = list(map(float,input().split(" "))) print((int((int(A)*(int((B)*100)))/100)))
p02657
A,B = list(map(int,input().split())) print((A * B))
A,B = list(map(float,input().split())) A = int(A) print((int(A * B)))
p02657
# 169A # A × B を整数として出力せよ # 1.入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 2.受け取った入力値を使って、適切に処理(計算)すること answer = a * b; # 3.計算した結果を出力すること print(answer)
a, b = list(map(int, input().split())) print((a * b))
p02657
#169A #A*Bを整数として出力せよ #1.入力をプログラムで扱えるように受け取ること a,b = list(map(int, input().split())) #2.受け取った入力値を使って適切に処理(計算)すること print((a * b))
#aとbをスペースを空けて入力 a,b = list(map(int,input().split())) print((a*b))
p02657
#!/usr/bin/env python3 # from typing import * import sys import math input = sys.stdin.readline sys.setrecursionlimit(10**6) def solve(A, B): print((A*B)) def main(): A, B = list(map(int, input().split())) solve(A, B) if __name__ == '__main__': main() ''' cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh test_py.sh cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh etest_py.sh cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh submit_py.sh '''
#!/usr/bin/env python3 # from typing import * import sys import math input = sys.stdin.readline sys.setrecursionlimit(10**6) def solve(A, B): print((A*B)) def main(): A, B = list(map(int, input().split())) solve(A, B) if __name__ == '__main__': main() ''' cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh shellscripts/test_py.sh cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh shellscripts/etest_py.sh cd ~/Desktop/Programming/AtCoder/contest/abc169/abc169_a && sh shellscripts/submit_py.sh '''
p02657
# 169A # A × B を整数として出力せよ # 1. 入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 2. 受け取った入力値を使って、適切に処理(計算)すること answer = a * b # 3. 計算した結果を出力すること print(answer)
a, b = list(map(int, input().split())) answer = a * b print(answer)
p02657
A,B = list(map(int, input().split())) print((A*B))
A,B = input().split() import math A=int(A) B=float(B) print((math.floor(A*B)))
p02657
# Problem A - Multiplication 1 # input a, b = list(map(int, input().split())) # output print((a * b))
# Problem a - Multiplication 1 # input A, B = list(map(int, input().split())) # initialization ans = A * B # output print(ans)
p02657
a,b = list(map(int,input().split())) c = a*b print(c)
a,b = list(map(float,input().split())) A = int(a) B = 100*b c = int(A*B//100) print(c)
p02657
#169A #A×Bを整数として出力せよ。 #やること①入力を受け取ること a, b = list(map(int, input().split())) # print(a, b) #やること②受け取った値を使って、適切に処理する answer = a * b #やること③計算した結果を出力すること print(answer)
A, B = list(map(int, input().split())) print((A * B))
p02657
# 入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 受け取った入力値を使い、適切に処理(計算)する anser = a * b # 計算した結果を出力する print(anser)
a, b = list(map(int, input().split())) anser = a * b print(anser)
p02657
a, b = list(map(int, input().split())) anser = a * b print(anser)
# 入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 受け取った入力値を使い、適切に処理(計算)する answer = a * b # 計算した結果を出力する print(answer)
p02657
# 169A # A × B を整数として出力せよ。 # 1. 入力をプログラムで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 2. 受け取った入力値を使って、適切に処理(計算)すること answer = a * b # 3. 計算した結果を出力すること print(answer)
# 入力 a, b = list(map(int, input().split())) # 計算 answer = a * b # 出力 print(answer)
p02657
A, B = list(map(int, input().split())) print((A * B))
print((eval(input().replace(" ","*"))))
p02657
a, b = list(map(int, input().split())) answer = a * b print(answer)
A, B = list(map(int,input().split())) print((A * B))
p02657
from sys import stdin def main(): read = stdin.readline A, B = list(map(int,read().split(" "))) print((A*B)) if __name__ == "__main__": main()
from sys import stdin def main(): read = stdin.readline A,B = list(read().split(" ")) B = int(B)*100 c = int(A) * B print((c//100)) if __name__ == "__main__": main()
p02657
# 169A # A × B を整数として出力せよ # 1. 入力をプログラミングで扱えるように受け取ること a, b = list(map(int, input().split())) # print(a, b) # 2. 受け取った入力値を使って、適切に処理(計算)すること answer = a * b # 3.計算した結果を出力すること print(answer)
# 169A # A × Bを求めてください。 A, B = list(map(int, input().split())) # print(A, B) answer = A * B print(answer)
p02657
a, b = list(map(int, input().split())) answer = a * b print(answer)
# A-Multiplication 1 # A×Bを求める # 1≤A≤100 # 1≤B≤100 a, b = list(map(int, input().split())) answer = a * b print(answer)
p02657
N, M = list(map(int, input().split())) print((N*M))
a,b = list(map(float,input().split())) a = int(a) b = int(b*1000) print((int(a*b)//1000))
p02657
a, b = list(map(int, input().split())) answer = a * b print(answer)
a, b = list(map (int, input().split())) print((a * b))
p02657
#169A a, b = list(map(int, input().split())) # print(a) # print(b) answer=a*b print(answer)
A,B=input().split() A=int(A) B=int(B) print((A*B))
p02657
a,b=list(map(int,input().split())) print((a*b))
a,b=input().split() a=int(a) b=int(float(b)*100) print((int(a*b/100)))
p02657
a, b = list(map(int, input().split())) answer = a * b print(answer)
a,b = list(map(int,input().split())) print((a * b))
p02657
import math from collections import deque from collections import defaultdict import time #自作関数群 def readInt(): return int(eval(input())) def readInts(): return list(map(int, input().split())) def readChar(): return eval(input()) def readChars(): return input().split() def factorization(n): res = [] if n%2==0: res.append(2) for i in range(3,math.floor(n//2)+1,2): if n%i==0: c = 0 for j in res: if i%j==0: c=1 if c==0: res.append(i) return res def fact2(n): p = factorization(n) res = [] for i in p: c=0 z=n while 1: if z%i==0: c+=1 z/=i else: break res.append([i,c]) return res def fact(n):#階乗 ans = 1 m=n for _i in range(n-1): ans*=m m-=1 return ans def comb(n,r):#コンビネーション if n<r: return 0 l = min(r,n-r) m=n u=1 for _i in range(l): u*=m m-=1 return u//fact(l) def combmod(n,r,mod): return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod def printQueue(q): r=copyQueue(q) ans=[0]*r.qsize() for i in range(r.qsize()-1,-1,-1): ans[i] = r.get() print(ans) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1]*n def find(self, x): # root if self.parents[x]<0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self,x,y): x = self.find(x) y = self.find(y) if x==y: return if self.parents[x]>self.parents[y]: x,y = y,x self.parents[x]+=self.parents[y] self.parents[y]=x def size(self,x): return -1*self.parents[self.find(x)] def same(self,x,y): return self.find(x)==self.find(y) def members(self,x): # much time root = self.find(x) return [i for i in range(self.n) if self.find(i)==root] def roots(self): return [i for i,x in enumerate(self.parents) if x<0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} # 1~n def bitArr(n):#ビット全探索 x = 1 zero = "0"*n ans = [] ans.append([0]*n) for i in range(2**n-1): ans.append(list([int(x) for x in list((zero+bin(x)[2:])[-1*n:])])) x+=1 return ans; def arrsSum(a1,a2): for i in range(len(a1)): a1[i]+=a2[i] return a1 def maxValue(a,b,v): v2 = v for i in range(v2,-1,-1): for j in range(v2//a+1): #j:aの個数 k = i-a*j if k%b==0: return i return -1 def copyQueue(q): nq = queue.Queue() n = q.qsize() for i in range(n): x = q.get() q.put(x) nq.put(x) return nq a,b = readInts() print((a*b))
a,b = list(map(int, input().split())) print((a*b))
p02657
import math from collections import deque from collections import defaultdict import itertools as it #自作関数群 def readInt(): return int(eval(input())) def readInts(): return list(map(int, input().split())) def readChar(): return eval(input()) def readChars(): return input().split() def factorization(n): res = [] if n%2==0: res.append(2) for i in range(3,math.floor(n//2)+1,2): if n%i==0: c = 0 for j in res: if i%j==0: c=1 if c==0: res.append(i) if len(res)==0: res = [n] return res def fact2(n): p = factorization(n) res = [] for i in p: c=0 z=n while 1: if z%i==0: c+=1 z/=i else: break res.append([i,c]) return res def fact(n):#階乗 ans = 1 m=n for _i in range(n-1): ans*=m m-=1 return ans def comb(n,r):#コンビネーション if n<r: return 0 l = min(r,n-r) m=n u=1 for _i in range(l): u*=m m-=1 return u//fact(l) def combmod(n,r,mod): return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod def printQueue(q): r=copyQueue(q) ans=[0]*r.qsize() for i in range(r.qsize()-1,-1,-1): ans[i] = r.get() print(ans) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1]*n def find(self, x): # root if self.parents[x]<0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self,x,y): x = self.find(x) y = self.find(y) if x==y: return if self.parents[x]>self.parents[y]: x,y = y,x self.parents[x]+=self.parents[y] self.parents[y]=x def size(self,x): return -1*self.parents[self.find(x)] def same(self,x,y): return self.find(x)==self.find(y) def members(self,x): # much time root = self.find(x) return [i for i in range(self.n) if self.find(i)==root] def roots(self): return [i for i,x in enumerate(self.parents) if x<0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} # 1~n def bitArr(n):#ビット全探索 x = 1 zero = "0"*n ans = [] ans.append([0]*n) for i in range(2**n-1): ans.append(list([int(x) for x in list((zero+bin(x)[2:])[-1*n:])])) x+=1 return ans; def arrsSum(a1,a2): for i in range(len(a1)): a1[i]+=a2[i] return a1 def maxValue(a,b,v): v2 = v for i in range(v2,-1,-1): for j in range(v2//a+1): #j:aの個数 k = i-a*j if k%b==0: return i return -1 def copyQueue(q): nq = queue.Queue() n = q.qsize() for i in range(n): x = q.get() q.put(x) nq.put(x) return nq def get_sieve_of_eratosthenes(n): data = [2] #data = [0,0,0] for i in range(3,n+1,2): data.append(i) data.append(0) for i in range(len(data)): interval = data[i] if interval!=0: for j in range(i+interval,n-1,interval): data[j] = 0 ans = [x for x in data if x!=0] #ans = data[:] return ans a,b = readInts() #print(a+b) print((a*b))
a,b = list(map(int,input().split())) #print(a+b) print((a*b))
p02657
a,b = list(map(int,input().split())) #print(a+b) print((a*b))
a,b = list(map(int,input().split())) print((a*b))
p02657
""" ~~ Author : Bhaskar ~~ Dated : 31~05~2020 """ import sys from bisect import * from math import floor,sqrt,ceil,factorial as F,gcd,pi from itertools import chain,combinations,permutations,accumulate from collections import Counter,defaultdict,OrderedDict,deque from array import array INT_MAX = sys.maxsize INT_MIN = -(sys.maxsize)-1 mod = 1000000007 lcm = lambda a,b : (a*b)//gcd(a,b) setbit = lambda x : bin(x)[2:].count("1") def solve(): a,b = list(map(int,sys.stdin.readline().split())) print((a*b)) if __name__ == "__main__": solve()
""" ~~ Author : Bhaskar ~~ Dated : 13~06~2020 """ import sys from bisect import * from math import floor, sqrt, ceil, factorial as F, gcd, pi from itertools import chain, combinations, permutations, accumulate from collections import Counter, defaultdict, OrderedDict, deque INT_MAX = sys.maxsize INT_MIN = -(sys.maxsize) - 1 mod = 1000000007 ch = "abcdefghijklmnopqrstuvwxyz" lcm = lambda a, b: (a * b) // gcd(a, b) setbit = lambda x: bin(x)[2:].count("1") INT = lambda type: type(sys.stdin.readline()) if type in [int, float] else type(sys.stdin.readline()).replace("\n", "") ARRAY = lambda type: list(map(type, sys.stdin.readline().split())) NUMS = lambda type: list(map(type, sys.stdin.readline().split())) def solve(): a,b = NUMS(int) print((a*b)) if __name__ == "__main__": try: sys.stdin = open("input.txt", "r") except: pass solve()
p02657
A, B = list(map(int, input().split())) print((A * B))
import math A, B = list(map(float, input().split())) B *= 100 mul = math.floor(A * B) print((int(mul/100)))
p02657
# 1.入力を受け取ること # スペース区切りの入力を整数として受け取れる「2 5」 -> 「a=2, B=5」となる a, b = list(map(int, input().split())) # print(a) # print(b) # 2.受け取った結果を使って目的の計算をすること answer = a * b # 3.出力すること print(answer)
A, B = list(map(int,input().split())) print((A * B))
p02657
a, b = list(map(int, input().split())) print((a * b))
a, b = list(map(str, input().split())) A = int(a) B = round(float(b) * 100) print((A * B // 100))
p02657
def resolve(): A, B = list(map(int, input().split())) print((A*B)) resolve()
A, B = list(map(int, input().split())) print((A*B))
p02657
import sys input = sys.stdin.readline a, b = [int(x) for x in input().split()] print((a*b))
import sys input = lambda: sys.stdin.readline().rstrip() a, b = list(map(int, input().split())) print((a*b))
p02657
A, B = list(map(int, input().split())) print((A*B))
A, B = list(map(float, input().split())) mod = 10000 a = int(A // mod) a2 = int(A % mod) B = int(100 * B) ans = a * B * mod + a2 * B print((ans // 100))
p02657
A,B = list(map(int,input().split())) print((A*B))
A,B = list(map(float,input().split())) tmp = A*B*100 print((int(tmp/100)))
p02657