input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
n = int(eval(input())) a = list(map(int, input().split())) cnt = 0 b = 0 c = [] for i in range(n): b += a[i] c.append(b) if b == 0: cnt += 1 for j in range(i): if b-c[j] == 0: cnt += 1 print(cnt)
n = int(eval(input())) a = list(map(int, input().split())) cnt = 0 b = 0 c = [0] for i in range(n): b += a[i] c.append(b) c.sort() i=0 ncm=0 m = 1 while True: i += 1 if i == n+1: ncm += m*(m-1)//2 break if c[i] == c[i-1]: m += 1 continue else: ncm += m*(m-1)//2 m = 1 print(ncm)
p03363
N = int(eval(input())) A = [int(_) for _ in input().split()] cumsum = [0] for a in A: cumsum += [cumsum[-1] + a] ans = 0 for i in range(N): for j in range(i + 1, N + 1): ans += cumsum[j] - cumsum[i] == 0 print(ans)
import heapq N = int(eval(input())) A = [int(_) for _ in input().split()] cumsum = [0] for a in A: cumsum += [cumsum[-1] + a] heapq.heapify(cumsum) before = 10 ** 10 count = 0 ans = 0 while cumsum: now = heapq.heappop(cumsum) count += before == now if not (before == now and cumsum): ans += count * (count + 1) // 2 count = 0 before = now print(ans)
p03363
import heapq N = int(eval(input())) A = [int(_) for _ in input().split()] cumsum = [0] for a in A: cumsum += [cumsum[-1] + a] heapq.heapify(cumsum) before = 10 ** 10 count = 0 ans = 0 while cumsum: now = heapq.heappop(cumsum) count += before == now if not (before == now and cumsum): ans += count * (count + 1) // 2 count = 0 before = now print(ans)
N = int(eval(input())) A = [int(_) for _ in input().split()] cumsum = [0] for a in A: cumsum += [cumsum[-1] + a] cumsum.sort() ans = 0 count = 0 for i in range(N): if cumsum[i] == cumsum[i + 1]: count += 1 if cumsum[i] != cumsum[i + 1] or i == N - 1: ans += count * (count + 1) // 2 count = 0 print(ans)
p03363
#import numpy as np import itertools N = int(input().strip()) A = list(map(int,input().strip().split(' '))) #Z = np.zeros((N+1,N)) Z = [[0]*int(N+1) for i in range(N) ] #Z[0,:] = np.array(A) result = 0 for k in range(N): Z[0][k] = A[k] if A[k] == 0: result = result+1 for i,j in itertools.combinations_with_replacement(list(range(1,N)),2): tmp = Z[i-1][j-1]+Z[0][j] if tmp ==0: result = result+1 Z[i][j] = tmp print(result)
import itertools N = int(input().strip()) A = list(map(int,input().strip().split(' '))) Z = [[0 for m in range(N+1)] for n in range(N) ] result = 0 for k in range(N): Z[0][k] = A[k] if A[k] == 0: result = result+1 for i,j in itertools.combinations_with_replacement(list(range(1,N)),2): tmp = Z[i-1][j-1]+Z[0][j] if tmp ==0: result = result+1 Z[i][j] = tmp print(result)
p03363
n = int(eval(input())) a = list(map(int,input().split())) b = [0] for i in range(n): b.append(a[i]+b[i]) ans = 0 for i in range(n+1): ans += b.count(b[0])-1 b = b[1:] print(ans)
n = int(eval(input())) a = list(map(int,input().split())) b = {0:0} c = 0 for i in range(n): c = c+a[i] if c in b: b[c] += 1 else: b[c] = 0 d = list(b.values()) ans = 0 for i in d: ans+= ((i+1)*i)//2 print(ans)
p03363
# coding: utf-8 # Your code here! # coding: utf-8 import sys sys.setrecursionlimit(200000000) # my functions here! #入力 def pin(type=int): return list(map(type,input().rstrip().split())) from collections import Counter def resolve(): N,=pin() A=list(pin()) S=[sum(A[:i])for i in range(N+1)] s=list(Counter(S).values()) ans=0 for j in s: ans+=(j*(j-1))//2 print(ans) """ #printデバッグ消した? #前の問題の結果見てないのに次の問題に行くの? """ """ お前カッコ閉じるの忘れてるだろ """ import sys from io import StringIO import unittest import sys from io import StringIO import unittest class TestClass(unittest.TestCase): def assertIO(self, input, output): stdout, stdin = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(input) resolve() sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, stdin self.assertEqual(out, output) def test_入力例_1(self): input = """6 1 3 -4 2 2 -2""" output = """3""" self.assertIO(input, output) def test_入力例_2(self): input = """7 1 -1 1 -1 1 -1 1""" output = """12""" self.assertIO(input, output) def test_入力例_3(self): input = """5 1 -2 3 -4 5""" output = """0""" self.assertIO(input, output) if __name__=="__main__":resolve()
# coding: utf-8 # Your code here! # coding: utf-8 import sys sys.setrecursionlimit(200000000) # my functions here! #入力 def pin(type=int): return list(map(type,input().rstrip().split())) from collections import Counter from itertools import accumulate as acm def resolve(): N,=pin() S=list(acm(pin())) S.insert(0,0) s=list(Counter(S).values()) ans=0 for j in s: ans+=(j*(j-1))//2 print(ans) """ #printデバッグ消した? #前の問題の結果見てないのに次の問題に行くの? """ """ お前カッコ閉じるの忘れてるだろ """ if __name__=="__main__":resolve()
p03363
N = int(eval(input())) A = [i for i in map(int, input().split())] def search(arr): if len(arr) == 1: if arr[0] == 0: return arr, 1 else: return arr, 0 kouho, count = search(arr[1:]) next_arr = [i + arr[0] for i in kouho] next_arr.append(arr[0]) count += next_arr.count(0) return next_arr, count arr, count = search(A) print(count)
N = int(eval(input())) A = [i for i in map(int, input().split())] count = 0 numbers = [] for i in A: numbers = [j + i for j in numbers] numbers.append(i) count += numbers.count(0) print(count)
p03363
n = int(eval(input())) a = list(map(int, input().split())) s = [0] s.extend([sum(a[0:i+1]) for i in range(n)]) s.sort() result = 0 count = 1 for i in range(n): if s[i] == s[i+1]: count += 1 else: result += count * (count - 1) // 2 count = 1 result += count * (count - 1) // 2 print(result)
n = int(eval(input())) a = list(map(int, input().split())) s = [0] for i in range(n): s.append(s[-1] + a[i]) s.sort() result = 0 count = 1 for i in range(n): if s[i] == s[i+1]: count += 1 else: result += count * (count - 1) // 2 count = 1 result += count * (count - 1) // 2 print(result)
p03363
N = int(eval(input())) A = list(map(int, input().split())) A_sum = [0] * (N + 1) for i in range(1, N+1): A_sum[i] = A_sum[i-1] + A[i-1] dict = {} for a in A_sum: if a in list(dict.keys()): dict[a] = dict[a] + 1 else: dict[a] = 1 ans = 0 for num in list(dict.values()): ans += num * (num-1) / 2 print((int(ans)))
N = int(eval(input())) A = list(map(int, input().split())) A_sum = [0] * (N + 1) for i in range(1, N+1): A_sum[i] = A_sum[i-1] + A[i-1] A_sum.sort() ans = 0 add = 0 a_cur = float('inf') for a in A_sum: if a != a_cur: add = 0 a_cur = a else: add += 1 ans += add print(ans)
p03363
n = int(eval(input())) A = [int(i) for i in input().split()] S = [sum(A[:i]) for i in range(n+1)] # num = [S.count(i) for i in set(S)] from collections import Counter def hello(bm): dt = Counter() for i in bm: key = str(i) dt[key] += 1 return dt num = hello(S) out = [i*(i-1)//2 for i in list(num.values()) if i>1] print((sum(out)))
from collections import Counter n = int(eval(input())) A = [int(i) for i in input().split()] S = [0]*(n+1) for i in range(n): S[i+1] = S[i] + A[i] def hello(data): dt = Counter() for i in data: dt[str(i)] += 1 return dt num = hello(S) out = [i*(i-1)//2 for i in list(num.values()) if i>1] print((sum(out)))
p03363
from collections import Counter n = int(eval(input())) A = [int(i) for i in input().split()] S = [0]*(n+1) for i in range(n): S[i+1] = S[i] + A[i] def hello(data): dt = Counter() for i in data: dt[str(i)] += 1 return dt num = hello(S) out = [i*(i-1)//2 for i in list(num.values()) if i>1] print((sum(out)))
n = int(eval(input())) A = [int(i) for i in input().split()] S = [0]*(n+1) num={} for i in range(n): S[i+1] = S[i] + A[i] for i in S: if str(i) in list(num.keys()): num[str(i)] += 1 else: num[str(i)] = 1 out = [i*(i-1)//2 for i in list(num.values()) if i>1] print((sum(out)))
p03363
from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split())) S = [0]*(N+1) dic = defaultdict(int) dic[0] = 1 for i in range(1,N+1): S[i] = S[i-1] + A[i-1] dic[S[i]] += 1 ans = 0 for _,v in list(dic.items()): if v > 0: ans += v*(v-1)//2 print(ans)
from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split())) S = [0]*(N+1) for i in range(1,N+1): S[i] = S[i-1] + A[i-1] ans = 0 dic = defaultdict(int) for i in range(N+1): ans += dic[S[i]] dic[S[i]] += 1 print(ans)
p03363
N = int(eval(input())) A = list(map(int,input().split())) L = [A[0]] for i in range(1,N): L.append(L[i-1]+A[i]) k = 0 for i in range(N): if (L[i] == 0): k += 1 for j in range(i+1,N): if (L[i] - L[j] == 0): k += 1 print(k)
N = int(eval(input())) L = list(map(int,input().split())) S = [0] c = 0 for i in range(N): c = S[i] + L[i] S.append(c) S.sort() S.append(S[N]+1) k = 0 p = 1 ans = 0 while k <= N: if S[k] == S[k+1]: p += 1 k += 1 else: ans += p*(p-1)//2 p = 1 k += 1 print(ans)
p03363
import collections n = int(eval(input())) lis = list(map(int,input().split())) count = 0 countArray = [0]*n calc=0 for i in range(n): calc += lis[i] countArray[i] = calc if calc == 0: count +=1 c = collections.Counter(countArray) for i in range(len(c.most_common())): counts = c.most_common()[i][1] if counts < 2: break count += (counts * (counts - 1)) // 2 print(count)
import collections n = int(eval(input())) lis = list(map(int,input().split())) count = 0 countArray = [0]*n calc=0 for i in range(n): calc += lis[i] countArray[i] = calc if calc == 0: count +=1 c = collections.Counter(countArray) a = c.most_common() for i in range(len(c.most_common())): counts = a[i][1] if counts < 2: break count += (counts * (counts - 1)) // 2 print(count)
p03363
from collections import Counter from operator import mul from functools import reduce def main(): def combinations_count(n, r): r = min(r, n - r) numer = reduce(mul, list(range(n, n - r, -1)), 1) denom = reduce(mul, list(range(1, r + 1)), 1) return numer // denom n = int(eval(input())) A = list(map(int, input().split())) B = [0, ] s = 0 for i in range(n): s += A[i] B.append(s) C = Counter(B).most_common() ans = 0 for c in C: if c[1] >= 2: ans += combinations_count(c[1], 2) else: break print(ans) if __name__ == '__main__': main()
from itertools import accumulate from collections import Counter def main(): n = int(eval(input())) A = list(map(int, input().split())) B = [0, ] + list(accumulate(A)) c = Counter(B) ans = 0 for k, v in list(c.items()): ans += v * (v - 1) // 2 print(ans) if __name__ == '__main__': main()
p03363
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return eval(input()) # Summarize count of factor within list -- START -- def summarize_list(l): sl=sorted(l) a=sl[0] c=1 res=[] for x in sl[1:]: if x==a: c+=1 else: res.append([a,c]) a=x c=1 res.append([a,c]) return res # Summarize count of factor within list --- END --- def main(): n=I() l=LI() for i in range(n-1): l[i+1]+=l[i] l=summarize_list(l) ans=0 for a,b in l: if a==0: ans+=b if b>1: ans+=b*(b-1)//2 else: if b>1: ans+=b*(b-1)//2 return ans # main() print((main()))
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] # def LF(): return [float(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return eval(input()) # Summarize count of factor within list -- START -- def summarizeList(l): sl=sorted(l) a=sl[0] c=1 res=[] for x in sl[1:]: if x==a: c+=1 else: res.append([a,c]) a=x c=1 res.append([a,c]) return res # Summarize count of factor within list --- END --- def main(): n=I() A=LI() A=[0]+A for i in range(n): A[i+1]+=A[i] ans=A.count(0)-1 sl=summarizeList(A) # print(sl) for x,c in sl: if x==0: ans+=(c-1)*(c-2)//2 continue ans+=c*(c-1)//2 return ans # main() print((main()))
p03363
c=0 N=int(eval(input())) A=list(map(int,input().split())) for i in range(N): for j in range(i+1,N): if sum(A[i:j+1])==0: c=c+1 for i in range(N): if A[i]==0: c=c+1 print(c)
N=int(eval(input())) L=list(map(int,input().split())) R={} sums=0 for i in range(N): sums+=L[i] if sums in R: R[sums]+=1 else: R[sums]=1 k=0 if 0 in R: k=R[0] print((sum([i*(i-1)//2 for i in list(R.values())])+k))
p03363
# A - Zero-Sum Ranges from itertools import accumulate from collections import defaultdict as dd n=int(eval(input())) a=[0]+list(map(int,input().split())) acc=accumulate(a) acc=list(acc) acc.sort() # print(acc) d=dd(int) for aa in acc: d[aa]+=1 ans=0 for k,v in list(d.items()): if v>=2: ans+=v*(v-1)//2 print(ans)
n=int(eval(input())) a=[0]+list(map(int,input().split())) from itertools import accumulate a=accumulate(a) a=list(a) a.sort() from collections import Counter c=Counter(a) ans=0 for k,v in list(c.items()): if v>=2: ans+=v*(v-1)//2 print(ans)
p03363
n=int(eval(input())) a=[0]+list(map(int,input().split())) from itertools import accumulate a=accumulate(a) a=list(a) a.sort() from collections import Counter c=Counter(a) ans=0 for k,v in list(c.items()): if v>=2: ans+=v*(v-1)//2 print(ans)
N = list(map(int, input().split())) a = [0] + list(map(int, input().split())) from collections import Counter from itertools import accumulate s = list(accumulate(a)) c = Counter(s) ans = 0 for v in list(c.values()): ans += v*(v-1)//2 print(ans)
p03363
n=int(eval(input())) A=list(map(int,input().split())) s=[0] for i in range(n): s.append(s[-1]+A[i]) cnt=[] S=set(s) for i in S: cnt.append(s.count(i)) ans=0 for i in cnt: if 1<i: ans+=i*(i-1)//2 print(ans)
n=int(eval(input())) A=list(map(int,input().split())) s=[0] for i in range(n): s.append(s[-1]+A[i]) s.sort() ans=0 cnt=1 for i in range(1,n+1): if s[i-1]==s[i]: cnt+=1 else: if 2<=cnt: ans+=(cnt*(cnt-1))//2 cnt=1 else: cnt=1 if 2<=cnt: ans+=(cnt*(cnt-1))//2 print(ans)
p03363
from collections import Counter def comb(n, k): k = min(k, n - k) result = 1 for i in range(n, n - k, -1): result *= i for i in range(1, k + 1): result //= i return result n = int(eval(input())) A = [int(i) for i in input().split()] S = [0] * (n + 1) for i in range(1, n + 1): S[i] = S[i - 1] + A[i - 1] S = Counter(S) ans = 0 for v in list(S.values()): if v > 1: ans += comb(v, 2) print(ans)
from collections import Counter n = int(eval(input())) A = [int(i) for i in input().split()] S = [0] * (n + 1) for i in range(1, n + 1): S[i] = S[i - 1] + A[i - 1] S = Counter(S) ans = 0 for v in list(S.values()): ans += v * (v - 1) // 2 print(ans)
p03363
# -*- coding: utf-8 -*- import collections n = int(eval(input())) a = [int(i) for i in input().split()] #累積和を求める sum_a = [0 for _ in range(n+1)] for i in range(1, n+1): sum_a[i] = sum_a[i-1] + a[i-1] #print(sum_a) ans = 0 c = collections.Counter(sum_a) for i in range(min(sum_a), max(sum_a)+1): tmp = c[i] if tmp >= 2: ans += (tmp * (tmp - 1)) // 2 print(ans)
# -*- coding: utf-8 -*- import collections n = int(eval(input())) a = [int(i) for i in input().split()] #累積和を求める sum_a = [0 for _ in range(n+1)] for i in range(1, n+1): sum_a[i] = sum_a[i-1] + a[i-1] #print(sum_a) ans = 0 c = collections.Counter(sum_a) for i in set(sum_a): tmp = c[i] if tmp >= 2: ans += (tmp * (tmp - 1)) // 2 print(ans)
p03363
from collections import Counter n = int(eval(input())) a = list(map(int, input().split())) cum = [0] * (n + 1) for i in range(n): cum[i + 1] = cum[i] + a[i] c = Counter(cum) ans = 0 for v in list(c.values()): ans += v * (v - 1) // 2 print(ans)
from itertools import accumulate from collections import Counter N = int(eval(input())) A = [0]+list(map(int, input().split())) Acum = list(accumulate(A)) C = Counter(Acum) ans = 0 for _, v in list(C.items()): ans += (v - 1) * v // 2 print(ans)
p03363
A, B = list(map(int, input().split())) if A >= 13: print(B) elif A <=5: print((0)) else: print((int(B/2)))
a,b=list(map(int,input().split())) print((b if a>=13 else b//2 if 6<=a<=12 else 0))
p03035
import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし A,B = MI() if A <= 5: print((0)) elif A >= 13: print(B) else: print((B//2))
import sys def MI(): return list(map(int,sys.stdin.readline().rstrip().split())) A,B = MI() if A <= 5: print((0)) elif A >= 13: print(B) else: print((B//2))
p03035
a, b = list(map(int, input().split())) if a >= 13: print(b) elif 12 >= a >= 6: print((b//2)) else: print((0))
a, b = list(map(int, input().split())) if a >= 13: print(b) elif 6<=a<=12: print((b//2)) else: print((0))
p03035
#!/usr/bin/env python3 # -*- coding: utf-8 -*- A, B = list(map(int, input().split())) if A <= 5: print((0)) elif A <= 12: print((int(B/2))) else: print(B)
a, b = list(map(int, input().strip().split())) if a >= 13: print(b) elif a >= 6: print((b // 2)) else: print((0))
p03035
A, B = list(map(int, input().split())) if A <= 5: print((0)) elif A <= 12: print((B//2)) else: print(B)
A, B = list(map(int, input().split())) if A <= 5: B = 0 elif A <= 12: B //= 2 print(B)
p03035
i=list(map(int,input().split())) if i[0]>=13: print((i[1])) elif i[0]>=6: print((int(i[1]/2))) else: print("0")
a,b=list(map(int,input().split())) if a<6: print((0)) elif a<13: print((b//2)) else: print(b)
p03035
#ABC127A - Ferris Wheel A, B = list(map(int,input().split())) if A >= 13: print(B) if 6 <= A <= 12: print((B//2)) if A <= 5: print((0))
#ABC127A - Ferris Wheel A, B = list(map(int,input().split())) if A >= 13: print(B) elif 6 <= A <= 12: print((B//2)) else: print((0))
p03035
a,b=list(map(int,input().split())) print((b if a>=13 else b//2 if 12>=a>=6 else 0))
a, b = list(map(int, input().split())) ans = b if 12 >= a >= 6: ans = b // 2 if a <= 5: ans = 0 print(ans)
p03035
a, b = list(map(int,input().split())) if a >= 13: print(b) elif a >= 6: b //= 2 print(b) else: print((0))
a, b = list(map(int,input().split())) if a >= 13: print(b) exit() if a >= 6: b //= 2 print(b) exit() print((0))
p03035
# -*- coding: utf-8 -*- a, b = list(map(int, input().split())) if a >= 13: print(b) elif a >= 6 and a <= 12: print((int(b/2))) else: print((0))
# -*- coding: utf-8 -*- def main(): a, b = list(map(int, input().split())) if a >= 13: print(b) elif a <= 12 and a >= 6: print((int(b / 2))) else: print((0)) if __name__ == "__main__": main()
p03035
import sys, math, collections, heapq, itertools F = sys.stdin def single_input(): return F.readline().strip("\n") def line_input(): return F.readline().strip("\n").split() def gcd(a, b): a, b = max(a, b), min(a, b) while a % b > 0: a, b = b, a % b return b def solve(): A, B = list(map(int, line_input())) if A >= 13: print(B) elif A >= 6: print((B//2)) else: print((0)) return 0 if __name__ == "__main__": solve()
import sys def solve(): input = sys.stdin.readline A, B = list(map(int, input().split())) if A <= 5: B = 0 elif A <= 12: B //= 2 print(B) return 0 if __name__ == "__main__": solve()
p03035
A, B = list(map(int, input().split())) if A <= 5: print((0)) exit() if A <= 12: print((int(B/2))) exit() print(B)
A, B = list(map(int, input().split())) if 5 >= A: print((0)) elif 12 >= A: print((int(B/2))) else: print(B)
p03035
a,b=list(map(int,input().split())) if a>=13: print(b) if a>=6 and a<=12: print((b//2)) if a<=5: print((0))
a,b=list(map(int,input().split())) if a<=5: print((0)) elif a<=12: print((b//2)) else: print(b)
p03035
a, b = list(map(int, input().split())) if a >= 13: print(b) elif a <= 5: print((0)) else: print((b//2))
a, b = list(map(int, input().split())) if a <= 5: print((0)) elif a <= 12: print((b//2)) else: print(b)
p03035
a,b = list(map(int,input().split())) print((0 if a<=5 else b if a >= 13 else b//2))
a,b=list(map(int,input().split())) if a<=5: print((0)) exit() elif 6<=a<=12: print((b//2)) exit() else: print(b)
p03035
import os, sys, re, math A,B = [int(s) for s in input().split(' ')] if A < 6: print((0)) elif 6 <= A and A <= 12: print((int(B/2))) else: print(B)
import os, sys, re, math A,B = list(map(int,input().split(' '))) if A >= 13: print(B) elif A >= 6: print((B // 2)) else: print((0))
p03035
age, fee = list(map(int, input().split())) if age >= 13: print(fee) elif 6 <= age and age <= 12: print((fee // 2)) else: print((0))
a, b = list(map(int, input().split())) if a >= 13: print(b) elif a <= 5: print((0)) else: print((int(b/2)))
p03035
A,B = list(map(int,input().split())) if A >= 13: print(B) elif 6<=A<=12: print((B//2)) else: print("0")
#! python3 # solve127A.py age,cost = list(map(int,input().split())) if age <= 5: cost = 0 elif 6 <= age <= 12: cost = cost//2 elif 7 <= age: cost = cost print(cost)
p03035
A,B = list(map(int,input().split())) if 13 <= A: print(B) elif (6 <= A) and (A<=12): print((B//2)) else: print((0))
a,b = list(map(int,input().split())) if a >= 13: print(b) elif (6 <=a<= 12): print((b//2)) else: print("0")
p03035
N, W = list(map(int, input().split())) if N >=13: print(W) elif N >=6: print((W//2)) else: print((0))
A, B = list(map(int, input().split())) if A >= 13: print(B) elif A >= 6: print((B//2)) else: print((0))
p03035
A, B = list(map(int,input().split())) if A < 6: print((0)) elif A < 13: print((int(B / 2))) else: print(B)
A,B=list(map(int,input().split())) if 13<=A: print((int(B))) if 6<=A and A<=12: print((int(B/2))) if A<=5: print((int(0)))
p03035
a,b = list(map(int,input().split())) if a<6: print('0') elif 6<=a<=12: print((int(b/2))) else: print(b)
a,b = list(map(int,input().split())) if a>=13: print(b) elif 6<=a<=12: print((int(b/2))) else: print('0')
p03035
A,B = list(map(int,input().split())) if A <= 5: print((0)) elif 6 <= A <= 12: print((B//2)) else: print(B)
A,B = list(map(int,input().split())) if A >= 13: print(B) elif A >= 6: print((B//2)) else: print((0))
p03035
def bomb(x,y): global M s=M[y+2] R=[-3,-2,-1,1,2,3] if s[x+2]=="0":return M[y+2]=s[:x+2]+"0"+s[x+3:] for d in R: bomb(x+d,y) bomb(x,y+d) return A=list(range(14)) B=list(range(8)) M=["00000000000000" for i in A] z="000" n=eval(input()) for i in range(n): s=input() for j in B: M[j+3]=z+input()+z x=eval(input()) y=eval(input()) bomb(x,y) print("Data %d:" %(i+1)) for j in B: print(M[j+3][3:-3])
def bomb(x,y): global M s=M[y+2] R=[-3,-2,-1,1,2,3] if s[x+2]=="0":return M[y+2]=s[:x+2]+"0"+s[x+3:] for d in R: bomb(x+d,y) bomb(x,y+d) return A=list(range(14)) B=list(range(3,11)) M=["00000000000000" for i in A] z="000" n=eval(input()) for i in range(n): s=input() for j in B: M[j]=z+input()+z x=eval(input()) y=eval(input()) bomb(x,y) print("Data %d:" %(i+1)) for j in B: print(M[j][3:-3])
p00071
def bomb(x,y): s=M[y] if s[x]=="0":return M[y]=s[:x]+"0"+s[x+1:] R=[-3,-2,-1,1,2,3] for e in R: bomb(x+e,y) bomb(x,y+e) return A=list(range(3,11)) M=["00000000000000" for i in range(14)] z="000" n=eval(input()) for i in range(n): s=input() for j in A: M[j]=z+input()+z x=eval(input())+2 y=eval(input())+2 bomb(x,y) print("Data %d:" %(i+1)) for j in A: print(M[j][3:-3])
def bomb(x,y): s=M[y] if s[x]=="0":return M[y]=s[:x]+"0"+s[x+1:] R=[-3,-2,-1,1,2,3] for e in R: bomb(x+e,y) bomb(x,y+e) return A=list(range(14)) B=list(range(3,11)) M=["0"*14 for i in A] z="000" n=eval(input()) for i in range(n): s=input() for j in B: M[j]=z+input()+z x=eval(input())+2 y=eval(input())+2 bomb(x,y) print("Data %d:" %(i+1)) for j in B: print(M[j][3:-3])
p00071
a,b,c=list(map(int, input().split())) d=0 for _ in range(60): if c<=d+a: print(c) break d+=a+b while c<=d:c+=60 else: print((-1))
a,b,c=list(map(int, input().split())) d,e=0,60 for _ in range(e): if c<=d+a: print(c) break d+=a+b c+=(d+e-c)//e*e else: print((-1))
p01751
def solve(a, b, c): l = 0; r = a; for t in range(114514): t = l // 60 p = 60*t + c if l <= p <= r: print(p) exit() l = r + b r = l + a print((-1)) a, b, c = list(map(int, input().split())) solve(a,b,c)
def solve(a, b, c): l = 0; r = a; for t in range(514): t = l // 60 p = 60*t + c if l <= p <= r: print(p) exit() l = r + b r = l + a print((-1)) a, b, c = list(map(int, input().split())) solve(a,b,c)
p01751
import sys INF = (sys.maxsize)/3 def NumberofWindow(a_list, S): i = 0 j = 0 tmpS = a_list[0] res = 0 while j < len(a_list) : while tmpS <= S and i <len(a_list)-1: i += 1 tmpS += a_list[i] if i == len(a_list)-1: if tmpS > S: res += i-j else: res += i-j+1 else: res += i-j tmpS -=a_list[j] j += 1 return res def main(): N, Q = list(map(int, input().split())) a_list = list(map(int, input().split())) x_list = list(map(int, input().split())) for x in x_list: print(NumberofWindow(a_list, x)) if __name__ == '__main__': main()
import sys INF = (sys.maxsize)/3 def NumberofWindow(N, a_list, S): i = 0 j = 0 tmpS = a_list[0] res = 0 while j < N : while tmpS <= S and i <N-1: i += 1 tmpS += a_list[i] if i == N-1: if tmpS > S: res += i-j else: res += i-j+1 else: res += i-j tmpS -=a_list[j] j += 1 return res def main(): N, Q = list(map(int, input().split())) a_list = list(map(int, input().split())) x_list = list(map(int, input().split())) ''' f = open('DSL_3_C-in21.txt', 'r') N, Q = map(int, f.readline().split()) a_list = map(int, f.readline().split()) x_list = map(int, f.readline().split()) ''' for x in x_list: print(NumberofWindow(N, a_list, x)) if __name__ == '__main__': main()
p02356
import sys INF = (sys.maxsize)/3 def NumberofWindow(N, a_list, S): i = 0 j = 0 tmpS = a_list[0] res = 0 while j < N : while tmpS <= S and i <N-1: i += 1 tmpS += a_list[i] if i == N-1: if tmpS > S: res += i-j else: res += i-j+1 else: res += i-j tmpS -=a_list[j] j += 1 return res def main(): N, Q = list(map(int, input().split())) a_list = list(map(int, input().split())) x_list = list(map(int, input().split())) ''' f = open('DSL_3_C-in21.txt', 'r') N, Q = map(int, f.readline().split()) a_list = map(int, f.readline().split()) x_list = map(int, f.readline().split()) ''' for x in x_list: print(NumberofWindow(N, a_list, x)) if __name__ == '__main__': main()
import sys INF = (sys.maxsize)/3 def NumberofWindow(N, a_list, S): i = 0 j = 0 tmpS = a_list[0] res = 0 for j in range(N) : while tmpS <= S and i <N-1: i += 1 tmpS += a_list[i] if i == N-1: if tmpS > S: res += i-j else: res += i-j+1 else: res += i-j tmpS -=a_list[j] return res def main(): N, Q = list(map(int, input().split())) a_list = list(map(int, input().split())) x_list = list(map(int, input().split())) ''' f = open('DSL_3_C-in21.txt', 'r') N, Q = map(int, f.readline().split()) a_list = (map(int, f.readline().split())) x_list = (map(int, f.readline().split())) ''' for x in x_list: print(NumberofWindow(N, a_list, x)) if __name__ == '__main__': main()
p02356
from bisect import bisect from itertools import accumulate n, q = list(map(int, input().split())) ac = list(accumulate(list(map(int, input().split())))) anss = [] for x in map(int, input().split()): ans = 0 prev = 0 for l, a in enumerate(ac): r = bisect(ac, prev + x, l) ans += r - l prev = a anss.append(ans) print(('\n'.join(map(str, anss))))
n, q = list(map(int, input().split())) aa = list(map(int, input().split())) anss = [] for x in map(int, input().split()): l = 0 ans = 0 _sum = 0 for r, a in enumerate(aa): _sum += a while _sum > x: _sum -= aa[l] l += 1 ans += r - l + 1 anss.append(ans) print(('\n'.join(map(str, anss))))
p02356
def N(): return int(input()) def NM():return map(int,input().split()) def L():return list(NM()) def LN(n):return [N() for i in range(n)] def LL(n):return [L() for i in range(n)] def YesNo(x):print("Yes")if x==True else print("No") k=N() s=input() if len(s)<=k: print(s) else: print(s[:k]+"...")
import sys def input():return sys.stdin.readline()[:-1] def N(): return int(input()) def NM():return map(int,input().split()) def L():return list(NM()) def LN(n):return [N() for i in range(n)] def LL(n):return [L() for i in range(n)] def YesNo(x):print("Yes")if x==True else print("No") k=N() s=input() if len(s)<=k: print(s) else: print(s[:k]+"...")
p02676
import bisect,collections,copy,heapq,itertools,math,string import sys sys.setrecursionlimit(1000000) def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) K = I() S = S() if len(S) <= K: print(S) sys.exit() else: li = "".join([S[0:K],"..."]) print(li)
import sys sys.setrecursionlimit(1000000) def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) K = I() S = S() if len(S) <= K: print(S) sys.exit() else: li = "".join([S[0:K],"..."]) print(li)
p02676
import sys input = sys.stdin.readline k = int(eval(input())) s = list(input().rstrip()) n = len(s) if n <= k: print(("".join(s))) else: ans = "".join(s[:k]) + "..." print(ans)
import sys input = sys.stdin.readline k = int(eval(input())) s = input().rstrip() ans = s if len(s) <= k else s[:k] + "..." print(ans)
p02676
MOD = 10**9 + 7 N = int(eval(input())) S = input().rstrip() is_left = [(i%2 == 0) ^ (x == "W") for i, x in enumerate(S)] answer = 1 left = 0 for bl in is_left: if bl: left += 1 else: answer *= left left -= 1 if left != 0: answer = 0 for i in range(1, N+1): answer *= i answer %= MOD print(answer)
MOD = 10**9 + 7 N = int(eval(input())) S = input().rstrip() is_left = [(i%2 == 0) ^ (x == "W") for i, x in enumerate(S)] answer = 1 left = 0 for bl in is_left: if bl: left += 1 else: answer *= left left -= 1 answer %= MOD if left != 0: answer = 0 for i in range(1, N+1): answer *= i answer %= MOD print(answer)
p02929
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import * import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(): return list(map(float, input().split())) def LI_(): return list([int(x)-1 for x in input().split()]) def II(): return int(eval(input())) def IF(): return float(eval(input())) def LS(): return list(map(list, input().split())) def S(): return list(input().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): m, d = LI() ans = 0 for i in range(1, m + 1): for j in range(10, d + 1): if int(str(j)[0]) < 2 or int(str(j)[1]) < 2: continue if i == int(str(j)[0]) * int(str(j)[1]): ans += 1 print(ans) return #B def B(): n, l = LI() a = LI() d0 = defaultdict(int) d1 = defaultdict(int) for i in a: d0[i] += 1 for i in range(n): b = 0 for k in range(i+1, n): if a[i] > a[k]: b += 1 d1[i] = b ans = 0 d2 = list(d0.items()) d2.sort() d3 = defaultdict(int) d = 0 for key, value in d2: d3[key] = d d += value for i in range(n): ans += d3[a[i]] * l * (l-1) // 2 + d1[i] * l ans %= mod print((ans % mod)) return #C def C(): n = II() s = S() lis = [0] for i in range(2 * n - 1): lis.append(lis[-1] ^ (s[i] == s[i+1])) l = [0] * (2 * n) r = [0] * (2 * n) for i in range(2 * n): l[i] += l[i - 1] + (lis[i] == 0) r[i] += r[i - 1] + (lis[i] == 1) ans = 1 if l[-1] != r[-1] or s[0] == "W": print((0)) return for i in range(2 * n): if lis[i]: ans *= l[i] - (r[i] - 1) % mod print((ans * math.factorial(n) % mod)) return #D def D(): return #E def E(): return #F def F(): return #G def G(): return #H def H(): return #Solve if __name__ == '__main__': C()
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import * import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(): return list(map(float, input().split())) def LI_(): return list([int(x)-1 for x in input().split()]) def II(): return int(eval(input())) def IF(): return float(eval(input())) def LS(): return list(map(list, input().split())) def S(): return list(input().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): m, d = LI() ans = 0 for i in range(1, m + 1): for j in range(10, d + 1): if int(str(j)[0]) < 2 or int(str(j)[1]) < 2: continue if i == int(str(j)[0]) * int(str(j)[1]): ans += 1 print(ans) return #B def B(): n, l = LI() a = LI() d0 = defaultdict(int) d1 = defaultdict(int) for i in a: d0[i] += 1 for i in range(n): b = 0 for k in range(i+1, n): if a[i] > a[k]: b += 1 d1[i] = b ans = 0 d2 = list(d0.items()) d2.sort() d3 = defaultdict(int) d = 0 for key, value in d2: d3[key] = d d += value for i in range(n): ans += d3[a[i]] * l * (l-1) // 2 + d1[i] * l ans %= mod print((ans % mod)) return #C def C(): n = II() s = S() lis = [0] for i in range(2 * n - 1): lis.append(lis[-1] ^ (s[i] == s[i+1])) l = [0] * (2 * n) r = [0] * (2 * n) for i in range(2 * n): l[i] += l[i - 1] + (lis[i] == 0) r[i] += r[i - 1] + (lis[i] == 1) ans = 1 if l[-1] != r[-1] or s[0] == "W": print((0)) return for i in range(2 * n): if lis[i]: ans *= l[i] - (r[i] - 1) ans %= mod print((ans * math.factorial(n) % mod)) return #D def D(): return #E def E(): return #F def F(): return #G def G(): return #H def H(): return #Solve if __name__ == '__main__': C()
p02929
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import * import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(): return list(map(float, input().split())) def LI_(): return list([int(x)-1 for x in input().split()]) def II(): return int(eval(input())) def IF(): return float(eval(input())) def LS(): return list(map(list, input().split())) def S(): return list(input().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): m, d = LI() ans = 0 for i in range(1, m + 1): for j in range(10, d + 1): if int(str(j)[0]) < 2 or int(str(j)[1]) < 2: continue if i == int(str(j)[0]) * int(str(j)[1]): ans += 1 print(ans) return #B def B(): n, l = LI() a = LI() d0 = defaultdict(int) d1 = defaultdict(int) for i in a: d0[i] += 1 for i in range(n): b = 0 for k in range(i+1, n): if a[i] > a[k]: b += 1 d1[i] = b ans = 0 d2 = list(d0.items()) d2.sort() d3 = defaultdict(int) d = 0 for key, value in d2: d3[key] = d d += value for i in range(n): ans += d3[a[i]] * l * (l-1) // 2 + d1[i] * l ans %= mod print((ans % mod)) return #C def C(): n = II() s = S() lis = [0] for i in range(2 * n - 1): lis.append(lis[-1] ^ (s[i] == s[i+1])) l = [0] * (2 * n) r = [0] * (2 * n) for i in range(2 * n): l[i] += l[i - 1] + (lis[i] == 0) r[i] += r[i - 1] + (lis[i] == 1) ans = 1 if l[-1] != r[-1] or s[0] == "W": print((0)) return for i in range(2 * n): if lis[i]: ans *= l[i] - (r[i] - 1) ans %= mod print((ans * math.factorial(n) % mod)) return #D def D(): return #E def E(): return #F def F(): return #G def G(): return #H def H(): return #Solve if __name__ == '__main__': C()
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import * import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(): return list(map(float, input().split())) def LI_(): return list([int(x)-1 for x in input().split()]) def II(): return int(eval(input())) def IF(): return float(eval(input())) def LS(): return list(map(list, input().split())) def S(): return list(input().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): m, d = LI() ans = 0 for i in range(1, m + 1): for j in range(10, d + 1): if int(str(j)[0]) < 2 or int(str(j)[1]) < 2: continue if i == int(str(j)[0]) * int(str(j)[1]): ans += 1 print(ans) return #B def B(): n, l = LI() a = LI() d0 = defaultdict(int) d1 = defaultdict(int) for i in a: d0[i] += 1 for i in range(n): b = 0 for k in range(i+1, n): if a[i] > a[k]: b += 1 d1[i] = b ans = 0 d2 = list(d0.items()) d2.sort() d3 = defaultdict(int) d = 0 for key, value in d2: d3[key] = d d += value for i in range(n): ans += d3[a[i]] * l * (l-1) // 2 + d1[i] * l ans %= mod print((ans % mod)) return #C def C(): n = II() s = S() lis = [0] for i in range(2 * n - 1): lis.append(lis[-1] ^ (s[i] == s[i+1])) l = [0] * (2 * n) for i in range(2 * n): l[i] += l[i - 1] + (lis[i] == 0) ans = 1 if l[-1] != n or s[0] == "W": print((0)) return k = 1 for i in range(2 * n): if lis[i]: ans *= l[i] - (k - 1) ans %= mod k += 1 print((ans * math.factorial(n) % mod)) return #D def D(): return #E def E(): return #F def F(): return #G def G(): return #H def H(): return #Solve if __name__ == '__main__': C()
p02929
n,s=open(0) n=int(n) a=l=s[0]<'W'>s[-2] r=f=0 for b,c in zip(s,s[1:-1]):f^=b==c;a=a*(max(1,n))*(l-r)**f%(10**9+7);n-=1;r+=f;l+=f^1 print((a*(l==r)))
n,s=open(0) n=int(n) a=l=s[0]<'W'>s[-2] r=f=0 for b,c in zip(s,s[1:-1]):f^=b==c;a=a*(n<1or n)*(l-r)**f%(10**9+7);n-=1;r+=f;l+=f^1 print((a*(l==r)))
p02929
n,s=open(0) a=i=1 l=0 for c in s:f=(c>'B')^i%2;l+=f;a=a*l**f*(i>int(n)or i)%(10**9+7);l-=f^1;i+=1 print((a*(l==1)))
n,s=open(0) a=i=1 l=0 for c in s:f=(c>'B')^i%2;l+=f or-1;a=a*l**f*(i>int(n)or i)%(10**9+7);i+=1 print((a*(l==1)))
p02929
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: s = {(0, 0, 0)}.union(*({(n // p, p, c), (-(-n // p), p, c)} for p, c in zip(a, b))) m = min(f(x) + abs(x * p - n) * d + c for x, p, c in s) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: m = d * n for p, c in zip(a, b): k, r = divmod(n, p); m = min(m, f(k) + r * d + c, r and f(k + 1) + (p - r) * d + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
p02669
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: m = d * n for p, c in zip(a, b): m = min(m, *(f(k) + abs(p * k - n) * d + c for k in (n // p, -(-n // p)))) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: m = d * n for p, c in zip(a, b): k, r = divmod(n, p); m = min(m, f(k) + r * d + c, r and f(k + 1) + (p - r) * d + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
p02669
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: nd = m = n * d for p, c in zip(a, b): p *= d k, r = divmod(nd, p); m = min(m, f(k) + r + c, r and f(k + 1) + p - r + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: m = d * n for p, c in zip(a, b): k, r = divmod(n, p); m = min(m, f(k) + r * d + c, r and f(k + 1) + (p - r) * d + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
p02669
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: nd = m = n * d for p, c in zip(a, b): p *= d k, r = divmod(nd, p); m = min(m, f(k) + r + c, r and f(k + 1) + p - r + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
a = 2, 3, 5 def f(n): m = memo.get(n) if m is None: m = d * n for p, c in zip(a, b): k, r = divmod(n, p); m = min(m, f(k) + r * d + c, r and f(k + 1) + (p - r) * d + c or m) memo[n] = m return m t = int(eval(input())) for _ in range(t): n, *b, d = list(map(int, input().split())) memo = {0: 0, 1: d} print((f(n)))
p02669
import sys divs = [2, 3, 5] t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) costs = [a, b, c] dp = {} def dfs(x): if x in dp: return dp[x] # print(x, file=sys.stderr) if x == 0: dp[x] = 0 return dp[x] ret = x * d for i in range(-4, 5): if x - i < 0: continue nx = x - i for cost, div in zip(costs, divs): if nx % div != 0 or nx // div >= x: continue ret = min(ret, dfs(nx // div) + cost + abs(i) * d) dp[x] = ret return ret print((dfs(n)))
divs = [2, 3, 5] t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) costs = [a, b, c] dp = {0: 0} def dfs(x): if x in dp: return dp[x] ret = x * d for cost, div in zip(costs, divs): lx = x // div * div ret = min(ret, dfs(lx // div) + cost + (x - lx) * d) if lx == x: continue lx += div if lx // div >= x: continue ret = min(ret, dfs(lx // div) + cost + (lx - x) * d) dp[x] = ret return ret print((dfs(n)))
p02669
t = int(eval(input())) import sys sys.setrecursionlimit(10 ** 6) def calc(x): if x in dp: return dp[x] divs = [5, 3, 2] costs = [c, b, a] res = d * x for div, cost in zip(divs, costs): dived = x//div res = min(res, calc(dived) + cost + abs(x-dived*div) * d) if x > div: dived = x//div+1 res = min(res, calc(dived) + cost + abs(x-dived*div) * d) dp[x] = res return res for _ in range(t): n, a, b, c, d = list(map(int, input().split())) dp = {} dp[0] = 0 dp[1] = d print((calc(n)))
t = int(eval(input())) def calc(x): if x not in dp: divs = [5, 3, 2] costs = [c, b, a] values = lambda div, cost: [calc(x // div) + cost + x % div * d, calc((x + div - 1) // div) + cost + (-x) % div * d] dp[x] = min(x * d, *[value for div, cost in zip(divs, costs) for value in values(div, cost)]) return dp[x] for _ in range(t): n, a, b, c, d = list(map(int, input().split())) dp = {0: 0, 1: d} print((calc(n)))
p02669
# coding: utf-8 from collections import defaultdict def solve(*args: str) -> str: t = int(args[0]) NABCD = [tuple(map(int, nabcd.split())) for nabcd in args[1:]] ret = [] for n, a, b, c, d in NABCD: ABC = ((2, a), (3, b), (5, c)) dp = defaultdict(lambda: n*d) stack = [(n, 0)] while stack: rem, cost = stack.pop() if dp[rem] <= cost: continue dp[rem] = cost dp[0] = min(dp[0], cost+rem*d) if 0 < rem: for x, y in ABC: l, h = rem//x, -(-rem//x) stack.append((l, d*abs(l*x-rem)+cost+y*(0 < l))) stack.append((h, d*abs(h*x-rem)+cost+y)) ret.append(str(dp[0])) return '\n'.join(ret) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
# coding: utf-8 from collections import defaultdict def solve(*args: str) -> str: t = int(args[0]) NABCD = [tuple(map(int, nabcd.split())) for nabcd in args[1:]] ret = [] for n, a, b, c, d in NABCD: ABC = ((2, a), (3, b), (5, c)) dp = defaultdict(lambda: n*d) stack = [(n, 0)] while stack: rem, cost = stack.pop() if min(dp[0], dp[rem]) <= cost: continue dp[rem] = cost dp[0] = min(dp[0], cost+rem*d) if 0 < rem: for x, y in ABC: l, h = rem//x, -(-rem//x) stack.append((l, d*abs(l*x-rem)+cost+y*(0 < l))) stack.append((h, d*abs(h*x-rem)+cost+y)) ret.append(str(dp[0])) return '\n'.join(ret) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
p02669
# coding: utf-8 from collections import defaultdict def solve(*args: str) -> str: t = int(args[0]) NABCD = [tuple(map(int, nabcd.split())) for nabcd in args[1:]] ret = [] for n, a, b, c, d in NABCD: ABC = ((2, a), (3, b), (5, c)) dp = defaultdict(lambda: n*d) stack = [(n, 0)] while stack: rem, cost = stack.pop() if min(dp[0], dp[rem]) <= cost: continue dp[rem] = cost dp[0] = min(dp[0], cost+rem*d) if 0 < rem: for x, y in ABC: l, h = rem//x, -(-rem//x) stack.append((l, d*abs(l*x-rem)+cost+y*(0 < l))) stack.append((h, d*abs(h*x-rem)+cost+y)) ret.append(str(dp[0])) return '\n'.join(ret) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
# coding: utf-8 from collections import defaultdict def solve(*args: str) -> str: t = int(args[0]) NABCD = [tuple(map(int, nabcd.split())) for nabcd in args[1:]] ret = [] for n, a, b, c, d in NABCD: ABC = ((2, a), (3, b), (5, c)) dp = defaultdict(lambda: n*d) stack = [(n, 0)] while stack: rem, cost = stack.pop() if min(dp[0], dp[rem]) <= cost: continue dp[rem] = cost dp[0] = min(dp[0], cost+rem*d) if 0 < rem: for x, y in ABC: div, m = divmod(rem, x) if div: stack.append((div, d*m+cost+y)) if m: stack.append((div+1, d*(x-m)+cost+y)) ret.append(str(dp[0])) return '\n'.join(ret) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
p02669
import sys sys.setrecursionlimit(10**8) T=int(eval(input())) def solve(N): if N==0: return 0 if N==1: return D if N in dic: return dic[N] tmp=N*D if N%2==0: tmp=min(tmp,solve(N//2)+A) else: tmp=min(tmp,solve((N-1)//2)+A+D,solve((N+1)//2)+A+D) if N%3==0: tmp=min(tmp,solve(N//3)+B) else: b=N%3 tmp=min(tmp,solve((N+(3-b))//3)+B+(3-b)*D,solve((N-b)//3)+B+b*D) if N%5==0: tmp=min(tmp,solve(N//5)+C) else: c=N%5 tmp=min(tmp,solve((N-c)//5)+C+D*c,solve((N+(5-c))//5)+C+D*(5-c)) dic[N]=tmp return tmp for _ in range(T): N,A,B,C,D=list(map(int,input().split())) dic={} print((solve(N)))
T=int(eval(input())) from functools import lru_cache for _ in range(T): N,A,B,C,D=list(map(int,input().split())) @lru_cache(maxsize=None) def solve(N): if N==0: return 0 if N==1: return D tmp=N*D if N%2==0: tmp=min(tmp,solve(N//2)+A) else: tmp=min(tmp,solve((N-1)//2)+A+D,solve((N+1)//2)+A+D) if N%3==0: tmp=min(tmp,solve(N//3)+B) else: b=N%3 tmp=min(tmp,solve((N+(3-b))//3)+B+(3-b)*D,solve((N-b)//3)+B+b*D) if N%5==0: tmp=min(tmp,solve(N//5)+C) else: c=N%5 tmp=min(tmp,solve((N-c)//5)+C+D*c,solve((N+(5-c))//5)+C+D*(5-c)) return tmp print((solve(N)))
p02669
import sys input=sys.stdin.readline sys.setrecursionlimit(10 ** 8) from itertools import accumulate from itertools import permutations from itertools import combinations from collections import defaultdict from collections import Counter import fractions import math from collections import deque from bisect import bisect_left from bisect import insort_left import itertools from heapq import heapify from heapq import heappop from heapq import heappush import heapq #import numpy as np INF = float("inf") #d = defaultdict(int) #d = defaultdict(list) #N = int(input()) #A = list(map(int,input().split())) #S = list(input()) #S.remove("\n") #N,M = map(int,input().split()) #S,T = map(str,input().split()) #A = [int(input()) for _ in range(N)] #S = [list(input())[:-1] for _ in range(N)] #A = [list(map(int,input().split())) for _ in range(N)] T = int(eval(input())) for _ in range(T): N,A,B,C,D = list(map(int,input().split())) ch1 = [[[N]*100 for _ in range(100)] for __ in range(100)] ch2 = [[[N]*100 for _ in range(100)] for __ in range(100)] ch1[0][0][0] = 0 ch2[0][0][0] = 0 deq = deque([[N,0,0,0,0]]) MIN = INF while len(deq) != 0: n,a,b,c,d = deq.popleft() am = n % 2 n2 = n // 2 MIN = min(MIN,a*A+b*B+c*C+D*(d+n-1)) if n2 == 0: continue if n2 == 1: MIN = min(MIN,(a+1)*A+b*B+c*C+D*(d+am)) else: if ch1[a+1][b][c] > d+am: ch1[a+1][b][c] = d+am deq.append([n2,a+1,b,c,d+am]) if ch2[a+1][b][c] > d+1: ch2[a+1][b][c] = d+1 deq.append([n2+1,a+1,b,c,d+1]) bm = n % 3 n3 = n // 3 if n3 == 0: continue if n3 == 1: MIN = min(MIN,a*A+(b+1)*B+c*C+D*(d+bm)) else: if ch1[a][b+1][c] > d+bm: ch1[a][b+1][c] = d+bm deq.append([n3,a,b+1,c,d+bm]) if ch2[a][b+1][c] > d+1+bm%2: ch2[a][b+1][c] = d+1+bm%2 deq.append([n3+1,a,b+1,c,d+1+bm%2]) cm = n % 5 n5 = n // 5 if n5 == 0: continue if n5 == 1: if cm == 4: cmm = 2 else: cmm = cm MIN = min(MIN,a*A+b*B+(c+1)*C+D*(d+cmm)) else: if cm <= 2: if ch1[a][b][c+1] > d+cm: ch1[a][b][c+1] = d+cm deq.append([n5,a,b,c+1,d+cm]) if ch2[a][b][c+1] > d+cm+1: ch2[a][b][c+1] = d+cm+1 deq.append([n5,a,b,c+1,d+cm+1]) else: if ch1[a][b][c+1] > d+6-cm: ch1[a][b][c+1] = d+6-cm deq.append([n5,a,b,c+1,d+6-cm]) if ch2[a][b][c+1] > d+5-cm: ch2[a][b][c+1] = d+5-cm deq.append([n5,a,b,c+1,d+5-cm]) print((MIN+D))
import sys input=sys.stdin.readline sys.setrecursionlimit(10 ** 8) from itertools import accumulate from itertools import permutations from itertools import combinations from collections import defaultdict from collections import Counter import fractions import math from collections import deque from bisect import bisect_left from bisect import insort_left import itertools from heapq import heapify from heapq import heappop from heapq import heappush import heapq #import numpy as np INF = float("inf") #d = defaultdict(int) #d = defaultdict(list) #N = int(input()) #A = list(map(int,input().split())) #S = list(input()) #S.remove("\n") #N,M = map(int,input().split()) #S,T = map(str,input().split()) #A = [int(input()) for _ in range(N)] #S = [list(input())[:-1] for _ in range(N)] #A = [list(map(int,input().split())) for _ in range(N)] t = int(eval(input())) for _ in range(t): n, a, b, c, da = list(map(int, input().split())) hq = [(0, n)] d = defaultdict(lambda: 10 ** 25) d[n] = 0 while hq: cs, p = heapq.heappop(hq) if d[p] < cs: continue if d[p // 2] > cs + a + (p % 2) * da: heapq.heappush(hq, (cs + a + (p % 2) * da, p // 2)) d[p // 2] = cs + a + (p % 2) * da if d[p // 2 + 1] > cs + a + da: heapq.heappush(hq, (cs + a + da, p // 2 + 1)) d[p // 2 + 1] = cs + a + da if d[p // 3] > cs + b + (p % 3) * da: heapq.heappush(hq, (cs + b + (p % 3) * da, p // 3)) d[p // 3] = cs + b + (p %3) * da if d[p // 3 + 1] > cs + b + (1+(p%3)%2)* da: heapq.heappush(hq, (cs + b + (1+(p%3)%2) * da, p // 3 + 1)) d[p // 3 + 1] = cs + b + (1+(p%3)%2) * da ccc = p % 5 if ccc <= 2: if d[p//5] > cs+c+ccc*da: heapq.heappush(hq,(cs+c+ccc*da,p//5)) d[p//5] = cs+c+ccc*da if d[p//5 +1] > cs+c+(ccc+1)*da: heapq.heappush(hq,(cs+c+(ccc+1)*da,p//5 +1)) d[p//5 +1] = cs+c+(ccc+1)*da else: if d[p//5] > cs+c+(6-ccc)*da: heapq.heappush(hq,(cs+c+(6-ccc)*da,p//5)) d[p//5] = cs+c+(6-ccc)*da if d[p//5 +1] > cs+c+(5-ccc)*da: heapq.heappush(hq,(cs+c+(5-ccc)*da,p//5 +1)) d[p//5 +1] = cs+c+(5-ccc)*da ans = 10 ** 25 for k, v in list(d.items()): ans = min(ans, k * da + v) print(ans)
p02669
import sys sys.setrecursionlimit(10**6) T = int(eval(input())) for _ in range(T): n, a, b, c, d = list(map(int, input().split())) # ans = 10**20 # for i in range(61): # for j in range(39): # if pow(2, i) * pow(3, j) > 10**19: # continue # for k in range(27): # if pow(2, i) * pow(3, j) * pow(5, k) > 10**19: # continue # for l in range(1, 6): # g = abs(n - l * pow(2, i) * pow(3, j) * pow(5, k)) # res = a*i + b*j + c*k + (g+l) * d # ans = min(ans, res) # print(ans) dp = dict() dp[n] = 0 def add(x, v): if x not in dp: dp[x] = v return True elif dp[x] > v: dp[x] = v return True else: return False def dfs(x): if x == 0: return if x%2 == 0: v = min(a, d*(x//2)) + dp[x] if add(x//2, v): dfs(x//2) else: v = min(a+d, d + d*(x//2)) + dp[x] if add(x//2, v): dfs(x//2) v = min(a+d, d + d*(x//2+1)) + dp[x] if add((x+1)//2, v): dfs((x+1)//2) if x%3 == 0: v = min(b, d*2*(x//3)) + dp[x] if add(x//3, v): dfs(x//3) else: m = x%3 v = min(b+d*m, d*m + d*2*(x//3)) + dp[x] if add(x//3, v): dfs(x//3) v = min(b+d*(3-m), d*(3-m) + d*2*(x//3 + 1)) + dp[x] if add(x//3+1, v): dfs(x//3+1) if x%5 == 0: v = min(c, d*4*(x//5)) + dp[x] if add(x//5, v): dfs(x//5) else: m = x%5 v = min(c+d*m, d*m + d*4*(x//5)) + dp[x] if add(x//5, v): dfs(x//5) v = min(c+d*(5-m), d*(5-m) + d*4*(x//5 + 1)) + dp[x] if add(x//5+1, v): dfs(x//5+1) return dfs(n) print((dp[0]))
import sys from heapq import heappush, heappop T = int(eval(input())) for _ in range(T): n, a, b, c, d = list(map(int, input().split())) dp = dict() dp[n] = 0 q = [(0, n)] def add(x, v): if x not in dp: dp[x] = v return True elif dp[x] > v: dp[x] = v return True else: return False while True: l = heappop(q) if l[1] == 0: break x = l[1] if x%2 == 0: v = min(a, d*(x//2)) + dp[x] if add(x//2, v): heappush(q, (v, x//2)) else: v = min(a+d, d + d*(x//2)) + dp[x] if add(x//2, v): heappush(q, (v, x//2)) v = min(a+d, d + d*(x//2+1)) + dp[x] if add(x//2+1, v): heappush(q, (v, x//2+1)) if x%3 == 0: v = min(b, d*2*(x//3)) + dp[x] if add(x//3, v): heappush(q, (v, x//3)) else: m = x%3 v = min(b+d*m, d*m + d*2*(x//3)) + dp[x] if add(x//3, v): heappush(q, (v, x//3)) v = min(b+d*(3-m), d*(3-m) + d*2*(x//3 + 1)) + dp[x] if add(x//3+1, v): heappush(q, (v, x//3+1)) if x%5 == 0: v = min(c, d*4*(x//5)) + dp[x] if add(x//5, v): heappush(q, (v, x//5)) else: m = x%5 v = min(c+d*m, d*m + d*4*(x//5)) + dp[x] if add(x//5, v): heappush(q, (v, x//5)) v = min(c+d*(5-m), d*(5-m) + d*4*(x//5 + 1)) + dp[x] if add(x//5+1, v): heappush(q, (v, x//5+1)) print((dp[0]))
p02669
def f(k): if k==1:return d if k==0:return 0 if k in m:return m[k] s=k*d s=min(s,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d) m[k]=s return s for _ in range(int(eval(input()))): m={} n,a,b,c,d=list(map(int,input().split())) print((f(n)))
def f(k): if k==1:return d if k==0:return 0 if k in m:return m[k] s=min(k*d,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d) m[k]=s return s for _ in range(int(eval(input()))): m={} n,a,b,c,d=list(map(int,input().split())) print((f(n)))
p02669
def f(k): if k==1:return d if k==0:return 0 if k in m:return m[k] s=min(k*d,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d) m[k]=s return s for _ in range(int(eval(input()))): m={} n,a,b,c,d=list(map(int,input().split())) print((f(n)))
from functools import lru_cache for _ in range(int(eval(input()))): n,a,b,c,d=list(map(int,input().split())) @lru_cache(None) def f(k): if k==0:return 0 if k==1:return d return min(k*d,f(k//5)+c+k%5*d,f(k//3)+b+k%3*d,f(k//2)+a+k%2*d,f((k+4)//5)+c+-k%5*d,f((k+2)//3)+b+-k%3*d,f((k+1)//2)+a+-k%2*d) print((f(n)))
p02669
def dfs(x): m=x//2 if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a: dp[m]=dp[x]+abs(2*m-x)*d+a dfs(m) m=0--x//2 if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a: dp[m]=dp[x]+abs(2*m-x)*d+a dfs(m) m=x//3 if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b: dp[m]=dp[x]+abs(3*m-x)*d+b dfs(m) m=0--x//3 if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b: dp[m]=dp[x]+abs(3*m-x)*d+b dfs(m) m=x//5 if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c: dp[m]=dp[x]+abs(5*m-x)*d+c dfs(m) m=0--x//5 if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c: dp[m]=dp[x]+abs(5*m-x)*d+c dfs(m) def solve(): dp[n]=0 dfs(n) return min(dp[i]+abs(i)*d for i in dp) for _ in range(int(eval(input()))): n,a,b,c,d=list(map(int,input().split())) dp={} print((solve()))
import sys input=sys.stdin.readline def main(): def dfs(x): m=x//2 if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a: dp[m]=dp[x]+abs(2*m-x)*d+a dfs(m) m=0--x//2 if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a: dp[m]=dp[x]+abs(2*m-x)*d+a dfs(m) m=x//3 if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b: dp[m]=dp[x]+abs(3*m-x)*d+b dfs(m) m=0--x//3 if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b: dp[m]=dp[x]+abs(3*m-x)*d+b dfs(m) m=x//5 if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c: dp[m]=dp[x]+abs(5*m-x)*d+c dfs(m) m=0--x//5 if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c: dp[m]=dp[x]+abs(5*m-x)*d+c dfs(m) def solve(): dp[n]=0 dfs(n) return min(dp[i]+abs(i)*d for i in dp) for _ in range(int(eval(input()))): n,a,b,c,d=list(map(int,input().split())) dp={} print((solve())) if __name__ == "__main__": main()
p02669
#from collections import defaultdict #from pprint import pprint #from collections import deque #from collections import Counter #from copy import deepcopy #from itertools #import sys #sys.setrecursionlimit(N) #N回まで再起を許可する。 #import numpy as np #import math # = map(int,input().split()) # = list(map(int,input().split())) # = [list(map(int,input().split())) for _ in range(XXXX)] def func(i): if i == 0: return 0 elif i == 1: return d elif i in memo: return memo[i] else: min_val = int(min( d*i, d*abs(i-i//2*2) + a + func(i//2), d*abs(i-(i+1)//2*2) + a + func((i+1)/2), d*abs(i-i//3*3) + b + func(i//3), d*abs(i-(i+2)//3*3) + b + func((i+2)//3), d*abs(i-i//5*5) + c + func(i//5), d*abs(i-(i+4)//5*5) + c + func((i+4)//5) )) memo[i] = min_val return min_val t = int(eval(input())) nabcd = [list(map(int,input().split())) for _ in range(t)] for i in range(t): n = nabcd[i][0] a = nabcd[i][1] b = nabcd[i][2] c = nabcd[i][3] d = nabcd[i][4] memo = {} print((func(n))) #print(memo)
#from collections import defaultdict #from pprint import pprint #from collections import deque #from collections import Counter #from copy import deepcopy #from itertools #import sys #sys.setrecursionlimit(N) #N回まで再起を許可する。 #import numpy as np #import math # = map(int,input().split()) # = list(map(int,input().split())) # = [list(map(int,input().split())) for _ in range(XXXX)] def func(i): if i == 0: return 0 elif i == 1: return d elif i in memo: return memo[i] else: min_val = int(min( d*i, d*abs(i-i//2*2) + a + func(i//2), d*abs(i-(i+1)//2*2) + a + func((i+1)//2), d*abs(i-i//3*3) + b + func(i//3), d*abs(i-(i+2)//3*3) + b + func((i+2)//3), d*abs(i-i//5*5) + c + func(i//5), d*abs(i-(i+4)//5*5) + c + func((i+4)//5) )) memo[i] = min_val return min_val t = int(eval(input())) nabcd = [list(map(int,input().split())) for _ in range(t)] for i in range(t): n = nabcd[i][0] a = nabcd[i][1] b = nabcd[i][2] c = nabcd[i][3] d = nabcd[i][4] memo = {} print((func(n))) #print(memo)
p02669
DP = {} def solve(n, a, b, c, d): if n <= 0: return 0 if n == 1: return d if n in DP: return DP[n] ans = float('inf') for i in range(-4, 5): num = n + i trash = abs(i)*d if num < 0: continue if num%5 == 0 and num // 5 < n: ans = min(ans, solve(num//5, a, b, c, d) + min(c, d*(num-(num//5))) + trash) if num%3 == 0 and num // 3 < n: ans = min(ans, solve(num//3, a, b, c, d) + min(b, d*(num-(num//3))) + trash) if num%2 == 0 and num // 2 < n: ans = min(ans, solve(num//2, a, b, c, d) + min(a, d*(num-(num//2))) + trash) # print(str(n) + ' ' + str(ans) + ' ' + str(num) + ' ' + str(i)) DP[n] = int(ans) return int(ans) def solve2(): DP.clear() ans = float('inf') n, a, b, c, d = list(map(int, input().split())) # for i in range(0, 65): # for j in range(0, 30): # for k in range(0, 10): # val = pow(2, i)*pow(3, j)*pow(5, k) # ans = min(ans, a*i + b*j + c*k + abs(n - val)*(d)) # print(ans) print((solve(n, a, b, c,d))) t = int(eval(input())) for _ in range(t): solve2()
DP = {} def solve(n, a, b, c, d): if n <= 0: return 0 if n == 1: return d if n in DP: return DP[n] ans = float('inf') for i in range(-3, 4): num = n + i trash = abs(i)*d if num < 0: continue if num%5 == 0 and num // 5 < n: ans = min(ans, solve(num//5, a, b, c, d) + min(c, d*(num-(num//5))) + trash) if num%3 == 0 and num // 3 < n: ans = min(ans, solve(num//3, a, b, c, d) + min(b, d*(num-(num//3))) + trash) if num%2 == 0 and num // 2 < n: ans = min(ans, solve(num//2, a, b, c, d) + min(a, d*(num-(num//2))) + trash) # print(str(n) + ' ' + str(ans) + ' ' + str(num) + ' ' + str(i)) DP[n] = int(ans) return int(ans) def solve2(): DP.clear() ans = float('inf') n, a, b, c, d = list(map(int, input().split())) # for i in range(0, 65): # for j in range(0, 30): # for k in range(0, 10): # val = pow(2, i)*pow(3, j)*pow(5, k) # ans = min(ans, a*i + b*j + c*k + abs(n - val)*(d)) # print(ans) print((solve(n, a, b, c,d))) t = int(eval(input())) for _ in range(t): solve2()
p02669
# -*- coding: utf-8 -*- # import bisect # import heapq # import math # import random # from collections import Counter, defaultdict, deque # from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache # from itertools import combinations, combinations_with_replacement, product, permutations # from operator import add, mul, sub import sys # sys.setrecursionlimit(10**6) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap # @mt def slv(N, A, B, C, D): if N == 1: return D @lru_cache(maxsize=None) def f(n): ans = n*D for i in range(-5, 6 if n > 10 else 1): m = n + i i = abs(i) if m < 0: continue if m == 0: ans = min(ans, D*i) continue if m % 5 == 0: ans = min(ans, f(m//5) + C + D*i) # ans = min(ans, f(m//5) + 4*(m//5)*D + D*i) if m % 3 == 0: ans = min(ans, f(m//3) + B + D*i) # ans = min(ans, f(m//3) + 2*(m//3)*D + D*i) if m % 2 == 0: ans = min(ans, f(m//2) + A + D*i) # ans = min(ans, f(m//2) + (m//2)*D + D*i) return ans return f(N) def main(): T = read_int() for _ in range(T): N, A, B, C ,D = read_int_n() print(slv(N, A, B, C, D)) # N, A, B, C, D = 29384293847243, 454353412, 332423423, 934923490, 1 # print(slv(N, A, B, C, D)) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- from functools import lru_cache import sys # sys.setrecursionlimit(10**6) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap # @mt def slv(N, A, B, C, D): @lru_cache(maxsize=None) def f(n): ans = n*D for i in range(-min(2, n), min(3, n)): m = n + i i = abs(i) if m == 0: continue if m % 5 == 0: ans = min(ans, f(m//5) + C + D*i) if m % 3 == 0: ans = min(ans, f(m//3) + B + D*i) if m % 2 == 0: ans = min(ans, f(m//2) + A + D*i) return ans return f(N) def main(): T = read_int() for _ in range(T): N, A, B, C ,D = read_int_n() print(slv(N, A, B, C, D)) if __name__ == '__main__': main()
p02669
# -*- coding: utf-8 -*- from functools import lru_cache import sys # sys.setrecursionlimit(10**6) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap # @mt def slv(N, A, B, C, D): @lru_cache(maxsize=None) def f(n): ans = n*D for i in range(-min(2, n), min(3, n)): m = n + i i = abs(i) if m == 0: continue if m % 5 == 0: ans = min(ans, f(m//5) + C + D*i) if m % 3 == 0: ans = min(ans, f(m//3) + B + D*i) if m % 2 == 0: ans = min(ans, f(m//2) + A + D*i) return ans return f(N) def main(): T = read_int() for _ in range(T): N, A, B, C ,D = read_int_n() print(slv(N, A, B, C, D)) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- from functools import lru_cache import sys # sys.setrecursionlimit(10) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap # @mt def slv(N, A, B, C, D): @lru_cache(maxsize=None) def f(n): if n == 1: return D if n == 0: return 0 ans = n*D i = (n // 2) * 2 ans = min(ans, f(i//2) + A + D*abs(n-i)) i = -(-n // 2) * 2 ans = min(ans, f(i//2) + A + D*abs(n-i)) i = (n // 3) * 3 ans = min(ans, f(i//3) + B + D*abs(n-i)) i = -(-n // 3) * 3 ans = min(ans, f(i//3) + B + D*abs(n-i)) i = (n // 5) * 5 ans = min(ans, f(i//5) + C + D*abs(n-i)) i = -(-n // 5) * 5 ans = min(ans, f(i//5) + C + D*abs(n-i)) return ans return f(N) def main(): T = read_int() for _ in range(T): N, A, B, C ,D = read_int_n() print(slv(N, A, B, C, D)) if __name__ == '__main__': main()
p02669
#!python3 iim = lambda: map(int, input().rstrip().split()) from heapq import heappush, heappop def calc(N, A, B, C, D): abc = [(3, B), (5, C), (2, A)] ans = N * D ans2 = min(2*D, D+A) dq = [] heappush(dq, (0, N)) seen = set() while dq: cost, rest = heappop(dq) if rest <= 2: if rest == 1: cost += D elif rest == 2: cost += ans2 else: cost += -rest * D ans = min(ans, cost) continue if rest in seen: continue else: seen.add(rest) if cost >= ans: continue for r, co1 in abc: a1, a2 = divmod(rest, r) if a2 == 0: heappush(dq, (cost+co1, a1)) continue if a2 > r - a2: a1 += 1 a2 = r - a2 cd = a2 * D if (rest - a1) * D < co1 + cd: ans = min(ans, rest*D + cost) else: co2 = cost + co1 heappush(dq, (co2 + cd, a1)) if a2 == 1: heappush(dq, (cost+A+D, a1+1)) return ans def resolve(): T = int(input()) S = [list(iim()) for i in range(T)] ans = [] for s in S: ans.append(calc(*s)) print(*ans, sep="\n") if __name__ == "__main__": resolve()
#!python3 iim = lambda: map(int, input().rstrip().split()) from heapq import heappush, heappop def calc(N, A, B, C, D): abc = [(2, A), (3, B), (5, C),] ans = N * D ans2 = min(2*D, D+A) dq = [] heappush(dq, (0, N)) seen = set() while dq: cost, rest = heappop(dq) if rest in seen: continue else: seen.add(rest) if rest <= 2: if rest == 1: cost += D elif rest == 2: cost += ans2 else: cost += -rest * D ans = min(ans, cost) continue if cost >= ans: continue for r, co1 in abc: a1, a2 = divmod(rest, r) if a2 == 0: heappush(dq, (cost+co1, a1)) continue a3 = r - a2 if a2 == a3: heappush(dq, (cost + co1 + a3*D, a1+1)) elif a2 > a3: a1 += 1 a2 = a3 cd = a2 * D if (rest - a1) * D < co1 + cd: ans = min(ans, rest*D + cost) else: co2 = cost + co1 heappush(dq, (co2 + cd, a1)) return ans def resolve(): T = int(input()) S = [list(iim()) for i in range(T)] ans = [] for s in S: ans.append(calc(*s)) print(*ans, sep="\n") if __name__ == "__main__": resolve()
p02669
#!python3 iim = lambda: map(int, input().rstrip().split()) from heapq import heappush, heappop def calc(N, A, B, C, D): abc = [(2, A), (3, B), (5, C),] ans = N * D ans2 = min(2*D, D+A) dq = [] heappush(dq, (0, N)) seen = set() while dq: cost, rest = heappop(dq) if rest in seen: continue else: seen.add(rest) if rest <= 2: if rest == 1: cost += D elif rest == 2: cost += ans2 else: cost += -rest * D ans = min(ans, cost) continue if cost >= ans: continue for r, co1 in abc: a1, a2 = divmod(rest, r) if a2 == 0: heappush(dq, (cost+co1, a1)) continue a3 = r - a2 if a2 == a3: heappush(dq, (cost + co1 + a3*D, a1+1)) elif a2 > a3: a1 += 1 a2 = a3 cd = a2 * D if (rest - a1) * D < co1 + cd: ans = min(ans, rest*D + cost) else: co2 = cost + co1 heappush(dq, (co2 + cd, a1)) return ans def resolve(): T = int(input()) S = [list(iim()) for i in range(T)] ans = [] for s in S: ans.append(calc(*s)) print(*ans, sep="\n") if __name__ == "__main__": resolve()
#!python3 iim = lambda: map(int, input().rstrip().split()) from heapq import heappush, heappop def calc(N, A, B, C, D): abc = list(map(lambda x: (x[0], x[1], x[0]*x[1]/((x[0]-1)*D)) ,[(2, A), (3, B), (5, C),])) ans = N * D ans2 = min(2*D, D+A) dq = [] heappush(dq, (0, N)) seen = set() while dq: cost, rest = heappop(dq) if rest in seen: continue else: seen.add(rest) if rest <= 2: if rest == 1: cost += D elif rest == 2: cost += ans2 else: cost += -rest * D ans = min(ans, cost) continue if cost >= ans: continue for r, co1, n2 in abc: if rest < n2: ans = min(ans, rest*D + cost) continue a1, a2 = divmod(rest, r) if a2 == 0: heappush(dq, (cost+co1, a1)) continue a3 = r - a2 if a2 == a3: heappush(dq, (cost + co1 + a3*D, a1+1)) elif a2 > a3: a1 += 1 a2 = a3 heappush(dq, (cost + co1 + a2*D, a1)) return ans def resolve(): T = int(input()) S = [list(iim()) for i in range(T)] ans = [] for s in S: ans.append(calc(*s)) print(*ans, sep="\n") if __name__ == "__main__": resolve()
p02669
import sys sys.setrecursionlimit(10**7) def dfs(x): if x == 0: return 0 if x == 1: return d if x in dic: return dic[x] l2 = x//2 r2 = (x+1)//2 l3 = x//3 r3 = (x+2)//3 l5 = x//5 r5 = (x+4)//5 count = min(x*d,10**18) count = min(count,abs(l2*2-x)*d+a+dfs(l2)) count = min(count,abs(r2*2-x)*d+a+dfs(r2)) count = min(count,abs(l3*3-x)*d+b+dfs(l3)) count = min(count,abs(r3*3-x)*d+b+dfs(r3)) count = min(count,abs(l5*5-x)*d+c+dfs(l5)) count = min(count,abs(r5*5-x)*d+c+dfs(r5)) dic[x] = count return count t = int(eval(input())) for _ in range(t): n,a,b,c,d = list(map(int,input().split())) dic = {} print((dfs(n)))
import sys from functools import lru_cache sys.setrecursionlimit(10**7) def solve(x,a,b,c,d): @lru_cache(None) def dfs(x): if x == 0: return 0 if x == 1: return d l2 = x//2 r2 = (x+1)//2 l3 = x//3 r3 = (x+2)//3 l5 = x//5 r5 = (x+4)//5 count = x*d count = min(count,abs(l2*2-x)*d+a+dfs(l2)) count = min(count,abs(r2*2-x)*d+a+dfs(r2)) count = min(count,abs(l3*3-x)*d+b+dfs(l3)) count = min(count,abs(r3*3-x)*d+b+dfs(r3)) count = min(count,abs(l5*5-x)*d+c+dfs(l5)) count = min(count,abs(r5*5-x)*d+c+dfs(r5)) return count return dfs(x) t = int(eval(input())) for _ in range(t): n,a,b,c,d = list(map(int,input().split())) print((solve(n,a,b,c,d)))
p02669
from functools import lru_cache import sys sys.setrecursionlimit(10**7) T = int(eval(input())) INF = 10**18 for _ in range(T): N, A, B, C, D = list(map(int, input().split())) @lru_cache(maxsize=None) def search(now): if now == 0: return 0 ret = INF for div, cost in zip((2, 3, 5), (A, B, C)): q, r = divmod(now, div) ret = min( ret, search(q) + cost + r * D, search(q + 1) + cost + (div - r) * D if q + 1 < now else INF, now * D ) return ret print((search(N)))
from functools import lru_cache import sys sys.setrecursionlimit(10**7) T = int(eval(input())) INF = 10**18 for _ in range(T): N, A, B, C, D = list(map(int, input().split())) @lru_cache(maxsize=None) def search(now): if now == 0: return 0 ret = now * D for div, cost in zip((2, 3, 5), (A, B, C)): q, r = divmod(now, div) ret = min( ret, search(q) + cost + r * D, search(q + 1) + cost + (div - r) * D if q + 1 < now else INF, ) return ret print((search(N)))
p02669
from functools import lru_cache import sys sys.setrecursionlimit(10**7) T = int(eval(input())) INF = 10**18 for _ in range(T): N, A, B, C, D = list(map(int, input().split())) @lru_cache(maxsize=None) def search(now): if now == 0: return 0 ret = now * D for div, cost in zip((2, 3, 5), (A, B, C)): q, r = divmod(now, div) ret = min( ret, search(q) + cost + r * D, search(q + 1) + cost + (div - r) * D if q + 1 < now else INF, ) return ret print((search(N)))
from functools import lru_cache import sys sys.setrecursionlimit(10**7) T = int(eval(input())) INF = 10**18 for _ in range(T): N, A, B, C, D = list(map(int, input().split())) DC = tuple(zip((2, 3, 5), (A, B, C))) @lru_cache(maxsize=None) def search(now): if now == 0: return 0 ret = now * D for div, cost in DC: q, r = divmod(now, div) ret = min( ret, search(q) + cost + r * D, search(q + 1) + cost + (div - r) * D if q + 1 < now else INF, ) return ret print((search(N)))
p02669
import sys sys.setrecursionlimit(10**9) def solve(n, a, b, c, d): return memo[0] t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) memo = {} def dfs(m, cost): if m not in memo or memo[m] > cost: memo[m] = cost if m > 0: dfs(m // 2, cost + (m % 2) * d + a) dfs(m // 3, cost + (m % 3) * d + b) dfs(m // 5, cost + (m % 5) * d + c) dfs((m + (2 - m % 2)) // 2, cost + (2 - m % 2) * d + a) dfs((m + (3 - m % 3)) // 3, cost + (3 - m % 3) * d + b) dfs((m + (5 - m % 5)) // 5, cost + (5 - m % 5) * d + c) dfs(0, cost + m * d) dfs(n, 0) print((memo[0]))
import sys sys.setrecursionlimit(10**9) t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) memo = {} def dfs(m, cost): if m not in memo or memo[m] > cost: memo[m] = cost if m > 0: dfs(m // 2, cost + (m % 2) * d + a) dfs(m // 3, cost + (m % 3) * d + b) dfs(m // 5, cost + (m % 5) * d + c) dfs((m + (2 - m % 2)) // 2, cost + (2 - m % 2) * d + a) dfs((m + (3 - m % 3)) // 3, cost + (3 - m % 3) * d + b) dfs((m + (5 - m % 5)) // 5, cost + (5 - m % 5) * d + c) dfs(0, cost + m * d) dfs(n, 0) print((memo[0]))
p02669
import sys sys.setrecursionlimit(10**9) t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) memo = {0: n * d} def dfs(m, cost): if m not in memo or memo[m] > cost: memo[m] = cost memo[0] = min(memo[0], cost + m * d) if m > 0: dfs(m // 2, cost + (m % 2) * d + a) dfs(m // 3, cost + (m % 3) * d + b) dfs(m // 5, cost + (m % 5) * d + c) dfs((m + (2 - m % 2)) // 2, cost + (2 - m % 2) * d + a) dfs((m + (3 - m % 3)) // 3, cost + (3 - m % 3) * d + b) dfs((m + (5 - m % 5)) // 5, cost + (5 - m % 5) * d + c) dfs(n, 0) print((memo[0]))
# import sys # sys.setrecursionlimit(10**9) t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) memo = {0: 0, 1: d} def dfs(m): if m in memo: return memo[m] else: res1 = m * d res2 = (m % 2) * d + a + dfs(m // 2) res3 = (m % 3) * d + b + dfs(m // 3) res4 = (m % 5) * d + c + dfs(m // 5) res5 = ((2 - (m % 2)) % 2) * d + a + dfs( (m + (2 - (m % 2)) % 2) // 2) res6 = ((3 - (m % 3)) % 3) * d + b + dfs( (m + (3 - (m % 3)) % 3) // 3) res7 = ((5 - (m % 5)) % 5) * d + c + dfs( (m + (5 - (m % 5)) % 5) // 5) memo[m] = min(res1, res2, res3, res4, res5, res6, res7) return memo[m] ans = dfs(n) print(ans)
p02669
# https://atcoder.jp/contests/agc044/submissions/13542551 def main(): from heapq import heappush, heappop T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) h = [(0, N)] # cost,value checked = set() ans = N * D + 1 while h: cost, x = heappop(h) if cost >= ans: continue if x in checked: continue checked.add(x) # 0まで1ずつ減らす if ans > cost + x * D: ans = cost + x * D for d, c in ((2, A), (3, B), (5, C)): # すでにheapqからnxがpopしていれば # より低コストでnxを生成できているので # nxの生成は不要 nx, r = divmod(x, d) if nx not in checked: heappush(h, (cost + r * D + c, nx)) if (nx + 1) not in checked: heappush(h, (cost + (d - r) * D + c, nx + 1)) print(ans) if __name__ == '__main__': main()
# https://atcoder.jp/contests/agc044/submissions/13542551 def main(): from heapq import heappush, heappop T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) h = [(0, N)] # cost,value checked = set() ans = N * D + 1 while h: cost, x = heappop(h) if cost >= ans: continue if x in checked: continue checked.add(x) # 0まで1ずつ減らす if ans > cost + x * D: ans = cost + x * D for d, c in ((2, A), (3, B), (5, C)): # すでにheapqからnxがpopしていれば # より低コストでnxを生成できているので # nxの生成は不要 nx, r = divmod(x, d) if nx not in checked: heappush(h, (cost + r * D + c, nx)) if (r > 0) and ((nx + 1) not in checked): heappush(h, (cost + (d - r) * D + c, nx + 1)) print(ans) if __name__ == '__main__': main()
p02669
# heapq -> deque def main(): from collections import defaultdict, deque inf = 10 ** 27 T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) dq = deque() dq.append((0, N)) # cost,value checked = defaultdict(lambda: inf) ans = N * D + 1 while dq: cost, x = dq.popleft() if cost >= ans: continue if cost >= checked[x]: continue checked[x] = cost # 0まで1ずつ減らす ans = min(ans, cost + x * D) for d, c in ((2, A), (3, B), (5, C)): # すでにheapqからnxがpopしていれば # より低コストでnxを生成できているので # nxの生成は不要 nx, r = divmod(x, d) ncost = cost + r * D + c if checked[nx] > ncost: dq.append((ncost, nx)) ncost = cost + (d - r) * D + c if (r > 0) and (checked[nx + 1] > ncost): dq.append((ncost, nx + 1)) print(ans) if __name__ == '__main__': main()
# heapq -> deque def main(): from collections import defaultdict, deque T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) inf = N * D + 1 dq = deque() dq.append((0, N)) # cost,value checked = defaultdict(lambda: inf) checked[N] = 0 ans = inf while dq: cost, x = dq.popleft() if cost >= ans: continue if cost > checked[x]: continue # 0まで1ずつ減らす ans = min(ans, cost + x * D) for d, c in ((2, A), (3, B), (5, C)): nx, r = divmod(x, d) ncost = cost + r * D + c if checked[nx] > ncost: checked[nx] = ncost dq.append((ncost, nx)) if r == 0: continue ncost = cost + (d - r) * D + c if checked[nx + 1] > ncost: checked[nx + 1] = ncost dq.append((ncost, nx + 1)) print(ans) if __name__ == '__main__': main()
p02669
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 def dijkstra(src): from heapq import heappush, heappop que = [(0, src)] dist = {} dist[src] = 0 while que: cost, u = heappop(que) if u >= 5: x = u % 5 v = (u-x) // 5 if v not in dist or cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u % 5 != 0: x = 5 - u % 5 v = (u+x) // 5 if v not in dist or cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u >= 3: x = u % 3 v = (u-x) // 3 if v not in dist or cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u % 3 != 0: x = 3 - u % 3 v = (u+x) // 3 if v not in dist or cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u >= 2: x = u % 2 v = (u-x) // 2 if v not in dist or cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) if u % 2 != 0: x = 2 - u % 2 v = (u+x) // 2 if v not in dist or cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) return dist for _ in range(INT()): N, a, b, c, d = MAP() res = dijkstra(N) ans = INF for u, cost in list(res.items()): ans = min(ans, cost + abs(u) * d) print(ans)
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 def dijkstra(src): from heapq import heappush, heappop que = [(0, src)] dist = {} dist[src] = 0 while que: cost, u = heappop(que) if 0 not in dist or dist[0] > cost+abs(u)*d: dist[0] = cost+abs(u)*d if u >= 5: x = u % 5 v = (u-x) // 5 if v not in dist or cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u % 5 != 0: x = 5 - u % 5 v = (u+x) // 5 if v not in dist or cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u >= 3: x = u % 3 v = (u-x) // 3 if v not in dist or cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u % 3 != 0: x = 3 - u % 3 v = (u+x) // 3 if v not in dist or cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u >= 2: x = u % 2 v = (u-x) // 2 if v not in dist or cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) if u % 2 != 0: x = 2 - u % 2 v = (u+x) // 2 if v not in dist or cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) return dist for _ in range(INT()): N, a, b, c, d = MAP() res = dijkstra(N) ans = res[0] print(ans)
p02669
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 def dijkstra(src): from heapq import heappush, heappop from collections import defaultdict que = [(0, src)] dist = defaultdict(lambda: INF) dist[src] = 0 while que: cost, u = heappop(que) dist[0] = min(dist[0], cost+abs(u)*d) if u >= 5: x = u % 5 v = (u-x) // 5 if cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u % 5 != 0: x = 5 - u % 5 v = (u+x) // 5 if cost+c+d*x < dist[v]: dist[v] = cost+c+d*x heappush(que, (cost+c+d*x, v)) if u >= 3: x = u % 3 v = (u-x) // 3 if cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u % 3 != 0: x = 3 - u % 3 v = (u+x) // 3 if cost+b+d*x < dist[v]: dist[v] = cost+b+d*x heappush(que, (cost+b+d*x, v)) if u >= 2: x = u % 2 v = (u-x) // 2 if cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) if u % 2 != 0: x = 2 - u % 2 v = (u+x) // 2 if cost+a+d*x < dist[v]: dist[v] = cost+a+d*x heappush(que, (cost+a+d*x, v)) return dist for _ in range(INT()): N, a, b, c, d = MAP() res = dijkstra(N) ans = res[0] print(ans)
import sys from collections import defaultdict def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 def rec(u): if dist[u] != INF: return dist[u] if u == 0: return 0 res = abs(u)*d if u >= 5: x = u % 5 v = (u-x) // 5 res = min(res, rec(v) + c+d*x) if u % 5 != 0: x = 5 - u % 5 v = (u+x) // 5 res = min(res, rec(v) + c+d*x) if u >= 3: x = u % 3 v = (u-x) // 3 res = min(res, rec(v) + b+d*x) if u % 3 != 0: x = 3 - u % 3 v = (u+x) // 3 res = min(res, rec(v) + b+d*x) if u >= 2: x = u % 2 v = (u-x) // 2 res = min(res, rec(v) + a+d*x) if u % 2 != 0: x = 2 - u % 2 v = (u+x) // 2 res = min(res, rec(v) + a+d*x) dist[u] = res return res for _ in range(INT()): N, a, b, c, d = MAP() dist = defaultdict(lambda: INF) ans = rec(N) print(ans)
p02669
# coding: utf-8 import sys from functools import lru_cache sys.setrecursionlimit(10 ** 7) sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) # Nから0にする T = ir() @lru_cache(None) def F(x, A, B, C, D): if x == 1: return D elif x <= 0: return INF if x&1: a = min(F(x//2, A, B, C, D) + D + A, F((x+1)//2, A, B, C, D) + D + A) else: a = F(x//2, A, B, C, D) + A cnt2 = 0 y = x while y%3 != 0: y -= 1 cnt2 += 1 b = F(y//3, A, B, C, D) + B + cnt2 * D if (x+1)%3 == 0: t = F((x+1)//3, A, B, C, D) + B + D b = min(b, t) cnt3 = 0 y = x while y%5 != 0: y -= 1 cnt3 += 1 c = min(F(y//5, A, B, C, D) + C + cnt3 * D, F((y+5)//5, A, B, C, D) + C + (5-cnt3) * D) ret = min(a, b, c) if x < 10 ** 12 or D < 10 ** 9: d = x * D ret = min(ret, d) return ret INF = float('inf') for _ in range(T): N, A, B, C, D = lr() answer = F(N, A, B, C, D) print(answer)
# coding: utf-8 import sys from functools import lru_cache sys.setrecursionlimit(10 ** 7) sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) # Nから0にする T = ir() @lru_cache(None) def F(x, A, B, C, D): if x == 1: return D elif x <= 0: return INF if x&1: a = min(F(x//2, A, B, C, D) + D + A, F((x+1)//2, A, B, C, D) + D + A) else: a = F(x//2, A, B, C, D) + A cnt2 = 0 y = x while y%3 != 0: y -= 1 cnt2 += 1 b = F(y//3, A, B, C, D) + B + cnt2 * D if (x+1)%3 == 0: t = F((x+1)//3, A, B, C, D) + B + D b = min(b, t) cnt3 = 0 y = x while y%5 != 0: y -= 1 cnt3 += 1 c = min(F(y//5, A, B, C, D) + C + cnt3 * D, F((y+5)//5, A, B, C, D) + C + (5-cnt3) * D) d = x * D ret = min(a, b, c, d) return ret INF = float('inf') for _ in range(T): N, A, B, C, D = lr() answer = F(N, A, B, C, D) print(answer)
p02669
import math from functools import lru_cache T = int(eval(input())) class Resolver: def __init__(self, a, b, c, d): self.a = a self.b = b self.c = c self.d = d @lru_cache(maxsize=None) def resolve(self, n): if n == 0: return 0 elif n == 1: return self.d else: div5f = n // 5 div5c = (n + 4) // 5 div3f = n // 3 div3c = (n + 2) // 3 div2f = n // 2 div2c = (n + 1) // 2 return min( self.d * n, self.d * abs(n - div5f * 5) + self.c + self.resolve(div5f), self.d * abs(n - div5c * 5) + self.c + self.resolve(div5c), self.d * abs(n - div3f * 3) + self.b + self.resolve(div3f), self.d * abs(n - div3c * 3) + self.b + self.resolve(div3c), self.d * abs(n - div2f * 2) + self.a + self.resolve(div2f), self.d * abs(n - div2c * 2) + self.a + self.resolve(div2c), ) for _ in range(T): N, A, B, C, D = list(map(int, input().split(' '))) resolver = Resolver(A, B, C, D) print((resolver.resolve(N)))
from functools import lru_cache T = int(eval(input())) def resolver(a, b, c, d): @lru_cache(maxsize=None) def _resolver(n): if n == 0: return 0 elif n == 1: return d else: div5f = n // 5 div5c = (n + 4) // 5 div3f = n // 3 div3c = (n + 2) // 3 div2f = n // 2 div2c = (n + 1) // 2 return min( d * n, d * abs(n - div5f * 5) + c + _resolver(div5f), d * abs(n - div5c * 5) + c + _resolver(div5c), d * abs(n - div3f * 3) + b + _resolver(div3f), d * abs(n - div3c * 3) + b + _resolver(div3c), d * abs(n - div2f * 2) + a + _resolver(div2f), d * abs(n - div2c * 2) + a + _resolver(div2c), ) return _resolver for _ in range(T): N, A, B, C, D = list(map(int, input().split(' '))) print((resolver(A, B, C, D)(N)))
p02669
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**7) t = int(eval(input())) for _ in range(t): n, a, b, c, d = list(map(int, input().split())) memo = {0: 0, 1: d} def cost(N): if N in memo: return memo[N] ans = N * d ks = [2, 3, 5] abc = [a, b, c] for i in range(3): k = ks[i] coin = abc[i] div, mod = divmod(N, k) pay = mod * d + coin # D枚コインを払って1だけ増減させる。 # その際に行き着く先として、div(減らす場合) or div+1(増やす場合) # 増やす場合は、現在の状態から、最寄りの倍数まで足す必要があり、これはmodからkまでの距離と等しい。 ans = min(ans, pay + cost(div)) if mod == 0: continue pay = (k - mod) * d + coin ans = min(ans, pay + cost(div + 1)) memo[N] = ans return ans print((cost(n)))
#!/usr/bin/env python3 N = 0 A = 0 B = 0 C = 0 D = 0 cost_result = {} # ある数に達するために必要なコインの枚数 each_cost = {} # 与えられたコスト def cost(target): """ Args: target: 目標とする数字 Returns: result: その数字に達するまでに必要なコスト """ if target in cost_result: return cost_result[target] else: result_dict = {} for i in [2, 3, 5]: if target % i == 0: result_dict[i] = cost(target//i) + each_cost[i] else: result_dict[i] = min( cost(target//i) + each_cost[i] + D * (target % i), cost((target//i)+1) + each_cost[i] + D * (i - target % i) ) simplest = target * D result = min(min(result_dict.values()), simplest) cost_result[target] = result return result if __name__ == "__main__": T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) each_cost[2] = A each_cost[3] = B each_cost[5] = C cost_result[1] = D cost_result = {0:0, 1:D} print((cost(N)))
p02669
import sys input = sys.stdin.readline from functools import lru_cache T=int(eval(input())) for tests in range(T): N,A,B,C,D=list(map(int,input().split())) DP=[N*D]*(10**5) DP[0]=0 for i in range(10**5): for k in range(i-10,i+10): if 0<=k<10**5 and DP[k]>DP[i]+abs(i-k)*D: DP[k]=DP[i]+abs(i-k)*D #if 0<=i+1<10**5 and DP[i+1]>DP[i]+D: # DP[i+1]=DP[i]+D #if 0<=i-1<10**5 and DP[i-1]>DP[i]+D: # DP[i-1]=DP[i]+D if 0<=2*i<10**5 and DP[2*i]>DP[i]+A: DP[2*i]=DP[i]+A if 0<=3*i<10**5 and DP[3*i]>DP[i]+B: DP[3*i]=DP[i]+B if 0<=5*i<10**5 and DP[5*i]>DP[i]+C: DP[5*i]=DP[i]+C @lru_cache(maxsize=None) def calc(x): if x<=10: return DP[x] ANS=x*D two=x//2*2 ANS=min(ANS,abs(two-x)*D+A+calc(two//2)) ANS=min(ANS,abs(two+2-x)*D+A+calc(two//2+1)) three=x//3*3 ANS=min(ANS,abs(three-x)*D+B+calc(three//3)) ANS=min(ANS,abs(three+3-x)*D+B+calc(three//3+1)) five=x//5*5 ANS=min(ANS,abs(five-x)*D+C+calc(five//5)) ANS=min(ANS,abs(five+5-x)*D+C+calc(five//5+1)) return ANS print((calc(N)))
import sys input = sys.stdin.readline from functools import lru_cache T=int(eval(input())) for tests in range(T): N,A,B,C,D=list(map(int,input().split())) @lru_cache(maxsize=None) def calc(x): if x==1: return D ANS=x*D two=x//2*2 if two//2<x: ANS=min(ANS,abs(two-x)*D+A+calc(two//2)) if two//2+1<x: ANS=min(ANS,abs(two+2-x)*D+A+calc(two//2+1)) three=x//3*3 if three//3<x: ANS=min(ANS,abs(three-x)*D+B+calc(three//3)) if three//3+1<x: ANS=min(ANS,abs(three+3-x)*D+B+calc(three//3+1)) five=x//5*5 if five//5<x: ANS=min(ANS,abs(five-x)*D+C+calc(five//5)) if five//5+1<x: ANS=min(ANS,abs(five+5-x)*D+C+calc(five//5+1)) return ANS print((calc(N)))
p02669
import sys sys.setrecursionlimit(10**6) # def dfs(t,ans): # global a,b,c,d fin # if t % 5 == 0: # if c > 4*t//5 * d: # fin = min(fin,ans+4*t//5*d) # else: # dfs(t//5,ans+c) # if t % 3 == 0: # if b > 2*t//3 * d: # fin = min(fin,ans+2*t//3*d) # else: # dfs(t//3,ans+b) # if t % 2 == 0: # if a > t//2 * d: # fin = min(fin,ans+t//2*d) # else: # dfs(t//2,ans+a) def solve(t,ans): global a,b,c,d,fin,al,dic1 # print(t) dic1[t] = ans if t < 5: if t==1: fin = min(fin,ans+d) elif t == 2: fin = min(fin,ans+2*d,ans+a+d,) elif t == 3: fin = min(fin,ans+3*d,ans+b+d,ans+a+2*d) else: fin = min(fin,ans+2*d+c,ans+4*d,ans+2*d+b,ans+2*a+d,ans+a+2*d) else: if t % 5 == 0: if c > 4*t//5 * d: fin = min(fin,ans+t*d) else: if t//5 not in dic1: solve(t//5,ans+c) elif dic1[t//5] > ans+c: solve(t//5,ans+c) if t % 3 == 0: if b > 2*t//3 * d: fin = min(fin,ans+t*d) else: if t//3 not in dic1: solve(t//3,ans+b) elif dic1[t//3] > ans+b: solve(t//3,ans+b) if t % 2 == 0: if a > t//2 * d: fin = min(fin,ans+t*d) else: if t//2 not in dic1: solve(t//2,ans+a) elif dic1[t//2] > ans+a: solve(t//2,ans+a) for i in al: u = t+i ans_temp = ans + d*abs(i) if u % 5 == 0: if c > 4*u//5 * d: fin = min(fin,ans_temp+u*d) else: if u//5 not in dic1: solve(u//5,ans_temp+c) elif dic1[u//5] > ans_temp+c: solve(u//5,ans_temp+c) if u % 3 == 0: if b > 2*u//3 * d: fin = min(fin,ans_temp+u*d) else: if u//3 not in dic1: solve(u//3,ans_temp+b) elif dic1[u//3] > ans_temp+b: solve(u//3,ans_temp+b) if u % 2 == 0: if a > u//2 * d: fin = min(fin,ans_temp+u*d) else: if u//2 not in dic1: solve(u//2,ans_temp+a) elif dic1[u//2] > ans_temp+a: solve(u//2,ans_temp+a) # while fa or fb or fc: # u = t+cnt # ans_temp = ans + d*abs(i) # if u % 5 == 0: # if c > 4*u//5 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//5,ans_temp+c) # if u % 3 == 0: # if b > 2*u//3 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//3,ans_temp+b) # if u % 2 == 0: # if a > u//2 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//2,ans_temp+a) # if u == 1: # fin = min(fin,ans_temp+d) t = int(eval(input())) al = [1,-1,2,-2] for i in range(t): n,a,b,c,d = list(map(int,input().split())) fin = 10**30 dic1 = {} solve(n,0) print(fin)
import sys sys.setrecursionlimit(10**6) def solve(t,ans): global a,b,c,d,fin,al,dic1 if t < 5: if t==1: fin = min(fin,ans+d) elif t == 2: fin = min(fin,ans+2*d,ans+a+d,) elif t == 3: fin = min(fin,ans+3*d,ans+b+d,ans+a+2*d) else: fin = min(fin,ans+2*d+c,ans+4*d,ans+2*d+b,ans+2*a+d,ans+a+2*d) else: for i in al: u = t+i ans_temp = ans + d*abs(i) if u % 5 == 0: if c > 4*u//5 * d: fin = min(fin,ans_temp+u*d) else: if u//5 not in dic1: solve(u//5,ans_temp+c) dic1[u//5] = ans_temp+c elif dic1[u//5] > ans_temp+c: solve(u//5,ans_temp+c) dic1[u//5] = ans_temp+c if u % 3 == 0: if b > 2*u//3 * d: fin = min(fin,ans_temp+u*d) else: if u//3 not in dic1: solve(u//3,ans_temp+b) dic1[u//3] = ans_temp+b elif dic1[u//3] > ans_temp+b: solve(u//3,ans_temp+b) dic1[u//3] = ans_temp+b if u % 2 == 0: if a > u//2 * d: fin = min(fin,ans_temp+u*d) else: if u//2 not in dic1: solve(u//2,ans_temp+a) dic1[u//2] = ans_temp+a elif dic1[u//2] > ans_temp+a: solve(u//2,ans_temp+a) dic1[u//2] = ans_temp+a t = int(eval(input())) al = [0,-1,1,-2,2] for i in range(t): n,a,b,c,d = list(map(int,input().split())) fin = 10**30 dic1 = {} solve(n,0) print(fin)
p02669
import sys sys.setrecursionlimit(10**6) def solve(t,ans): global a,b,c,d,fin,al,dic1 if t < 5: if t==1: fin = min(fin,ans+d) elif t == 2: fin = min(fin,ans+2*d,ans+a+d,) elif t == 3: fin = min(fin,ans+3*d,ans+b+d,ans+a+2*d) else: fin = min(fin,ans+2*d+c,ans+4*d,ans+2*d+b,ans+2*a+d,ans+a+2*d) else: for i in al: u = t+i ans_temp = ans + d*abs(i) if u % 5 == 0: if c > 4*u//5 * d: if u//5 not in dic1: solve(u//5,ans_temp+4*u//5 * d) dic1[u//5] = ans_temp+4*u//5 * d elif dic1[u//5] > ans_temp+4*u//5 * d: solve(u//5,ans_temp+4*u//5 * d) dic1[u//5] = ans_temp+4*u//5 * d else: if u//5 not in dic1: solve(u//5,ans_temp+c) dic1[u//5] = ans_temp+c elif dic1[u//5] > ans_temp+c: solve(u//5,ans_temp+c) dic1[u//5] = ans_temp+c if u % 3 == 0 and -2 < i < 2: if b > 2*u//3 * d: if u//3 not in dic1: solve(u//3,ans_temp+2*u//3 * d) dic1[u//3] = ans_temp+2*u//3 * d elif dic1[u//3] > ans_temp+2*u//3 * d: solve(u//3,ans_temp+2*u//3 * d) dic1[u//3] = ans_temp+2*u//3 * d else: if u//3 not in dic1: solve(u//3,ans_temp+b) dic1[u//3] = ans_temp+b elif dic1[u//3] > ans_temp+b: solve(u//3,ans_temp+b) dic1[u//3] = ans_temp+b if u % 2 == 0 and -2 < i < 2: if a > u//2 * d: if u//2 not in dic1: solve(u//2,ans_temp+u//2 * d) dic1[u//2] = ans_temp+u//2 * d elif dic1[u//2] > ans_temp+u//2 * d: solve(u//2,ans_temp+u//2 * d) dic1[u//2] = ans_temp+u//2 * d else: if u//2 not in dic1: solve(u//2,ans_temp+a) dic1[u//2] = ans_temp+a elif dic1[u//2] > ans_temp+a: solve(u//2,ans_temp+a) dic1[u//2] = ans_temp+a t = int(eval(input())) al = [0,-1,1,-2,2] for i in range(t): n,a,b,c,d = list(map(int,input().split())) fin = 10**30 dic1 = {} solve(n,0) print(fin)
import sys sys.setrecursionlimit(10**6) # def dfs(t,ans): # global a,b,c,d fin # if t % 5 == 0: # if c > 4*t//5 * d: # fin = min(fin,ans+4*t//5*d) # else: # dfs(t//5,ans+c) # if t % 3 == 0: # if b > 2*t//3 * d: # fin = min(fin,ans+2*t//3*d) # else: # dfs(t//3,ans+b) # if t % 2 == 0: # if a > t//2 * d: # fin = min(fin,ans+t//2*d) # else: # dfs(t//2,ans+a) def solve(t,ans): global a,b,c,d,fin,al,dic1 # print(t) dic1[t] = ans if t < 5: if t==1: fin = min(fin,ans+d) elif t == 2: fin = min(fin,ans+2*d,ans+a+d,) elif t == 3: fin = min(fin,ans+3*d,ans+b+d,ans+a+2*d) else: fin = min(fin,ans+2*d+c,ans+4*d,ans+2*d+b,ans+2*a+d,ans+a+2*d) else: fb,fc = 1,1 if t % 5 == 0: fc = 0 if c > 4*t//5 * d: fin = min(fin,ans+t*d) else: if t//5 not in dic1: solve(t//5,ans+c) elif dic1[t//5] > ans+c: solve(t//5,ans+c) if t % 3 == 0: fb = 0 if b > 2*t//3 * d: fin = min(fin,ans+t*d) else: if t//3 not in dic1: solve(t//3,ans+b) elif dic1[t//3] > ans+b: solve(t//3,ans+b) if t % 2 == 0: if a > t//2 * d: fin = min(fin,ans+t*d) else: if t//2 not in dic1: solve(t//2,ans+a) elif dic1[t//2] > ans+a: solve(t//2,ans+a) for i in al: u = t+i ans_temp = ans + d*abs(i) if u % 5 == 0 and fc == 1: fc = 0 if c > 4*u//5 * d: fin = min(fin,ans_temp+u*d) else: if u//5 not in dic1: solve(u//5,ans_temp+c) elif dic1[u//5] > ans_temp+c: solve(u//5,ans_temp+c) if u % 3 == 0 and fb == 1: fb = 0 if b > 2*u//3 * d: fin = min(fin,ans_temp+u*d) else: if u//3 not in dic1: solve(u//3,ans_temp+b) elif dic1[u//3] > ans_temp+b: solve(u//3,ans_temp+b) if u % 2 == 0 and -2<i<2: if a > u//2 * d: fin = min(fin,ans_temp+u*d) else: if u//2 not in dic1: solve(u//2,ans_temp+a) elif dic1[u//2] > ans_temp+a: solve(u//2,ans_temp+a) # while fa or fb or fc: # u = t+cnt # ans_temp = ans + d*abs(i) # if u % 5 == 0: # if c > 4*u//5 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//5,ans_temp+c) # if u % 3 == 0: # if b > 2*u//3 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//3,ans_temp+b) # if u % 2 == 0: # if a > u//2 * d: # fin = min(fin,ans_temp+u*d) # else: # solve(u//2,ans_temp+a) # if u == 1: # fin = min(fin,ans_temp+d) t = int(eval(input())) al = [1,-1,2,-2] for i in range(t): n,a,b,c,d = list(map(int,input().split())) fin = 10**30 dic1 = {} solve(n,0) print(fin)
p02669
import sys from functools import lru_cache sys.setrecursionlimit(10**9) @lru_cache(None) def f(n,a,b,c,d): if n==0:return 0 if n==1:return d if n==2:return min(d*2,a+d) if n==3:return min(d*3,d+a+d,b+d) if n==4:return min(d*4,a+a+d,d+b+d,d+c+d) if n==5:return min(d*5,d+a+a+d,d+d+b+d,d+a+b+d,c+d) if n==6:return min(d*6,a+b+d,a+d+a+d,b+d+b+d,d+c+d) # print(n,a,b,c,d) p=n*d for i in range(-10, 11): nn=n+i if nn<0:continue if nn%2==0and nn//2<n:p=min(p,f(nn//2,a,b,c,d)+abs(i)*d+a) if nn%3==0and nn//3<n:p=min(p,f(nn//3,a,b,c,d)+abs(i)*d+b) if nn%5==0and nn//5<n:p=min(p,f(nn//5,a,b,c,d)+abs(i)*d+c) return p for _ in range(int(eval(input()))): n,a,b,c,d = list(map(int,input().split())) print((f(n,a,b,c,d)))
from functools import lru_cache def solve(n,a,b,c,d): @lru_cache(None) def rec(cur): if cur == 0: return 0 if cur == 1: return d ret = cur * d p,q = divmod(cur, 2) if q == 0: ret = min(ret, rec(p) + a) if q == 1: ret = min(ret, rec(p) + a + d, rec(p + 1) + a + d) p,q = divmod(cur, 3) if q == 0: ret = min(ret, rec(p) + b) if q == 1: ret = min(ret, rec(p) + b + d) if q == 2: ret = min(ret, rec(p) + b + d * 2, rec(p + 1) + b + d) p,q = divmod(cur, 5) if q == 0: ret = min(ret, rec(p) + c) if q == 1: ret = min(ret, rec(p) + c + d) if q == 2: ret = min(ret, rec(p) + c + d * 2) if q == 3: ret = min(ret, rec(p) + c + d * 3, rec(p + 1) + c + d * 2) if q == 4: ret = min(ret, rec(p) + c + d * 4, rec(p + 1) + c + d) return ret return rec(n) for _ in range(int(eval(input()))): n,a,b,c,d = list(map(int,input().split())) print((solve(n,a,b,c,d)))
p02669
from heapq import heappop, heappush T = int(input()) ans = [] for _ in range(T): N, A, B, C, D = map(int, input().split()) Q = [(0, N)] # already processed S = set() an = D * N while Q: cost, num = heappop(Q) if num in S: continue an = min(an, cost + num * D) if num == 1: break S.add(num) q, r = divmod(num, 2) if r < 2: heappush(Q, (cost + A + r * D, q)) if r > 0: heappush(Q, (cost + A + (2 - r) * D, q + 1)) q, r = divmod(num, 3) if r < 2: heappush(Q, (cost + B + r * D, q)) if r > 0: heappush(Q, (cost + B + (3 - r) * D, q + 1)) q, r = divmod(num, 5) if r < 3: heappush(Q, (cost + C + r * D, q)) if r > 0: heappush(Q, (cost + C + (5 - r) * D, q + 1)) ans.append(an) print(*ans, sep="\n")
from heapq import heappop, heappush T = int(input()) ans = [] for _ in range(T): N, A, B, C, D = map(int, input().split()) Q = [(0, N)] # already processed S = set() an = D * N while Q: cost, num = heappop(Q) if num in S: continue an = min(an, cost + num * D) if num == 1: break S.add(num) q, r = divmod(num, 2) if r < 2: heappush(Q, (cost + A + r * D, q)) if r > 0: heappush(Q, (cost + A + (2 - r) * D, q + 1)) q, r = divmod(num, 3) if r < 2: heappush(Q, (cost + B + r * D, q)) if r > 1: heappush(Q, (cost + B + (3 - r) * D, q + 1)) q, r = divmod(num, 5) if r < 3: heappush(Q, (cost + C + r * D, q)) if r > 2: heappush(Q, (cost + C + (5 - r) * D, q + 1)) ans.append(an) print(*ans, sep="\n")
p02669
T = int(eval(input())) while(T > 0): N, A, B, C, D = list(map(int, input().split())) dp = {} def d(n): if n == 0: return 0 if n == 1: return D if n in dp: return dp[n] ret = min( D * n, D * abs(n - (n // 2) * 2) + A + d(n // 2), D * abs(n - ((n+1) // 2) * 2) + A + d((n+1) // 2), D * abs(n - (n // 3) * 3) + B + d(n // 3), D * abs(n - ((n + 2) // 3) * 3) + B + d((n + 2) // 3), D * abs(n - (n // 5) * 5) + C + d(n // 5), D * abs(n - ((n + 4) // 5) * 5) + C + d((n + 4) // 5), ) dp[n] = ret return ret print((d(N))) T -= 1
T = int(eval(input())) while(T > 0): N, A, B, C, D = list(map(int, input().split())) memo = {} def d(n): if n == 0: return 0 if n == 1: return D if n in memo: return memo[n] ret = n * D if (n % 2 == 0): ret = min(ret, A + d(n // 2)) else: ret = min(ret, (n%2)*D + A + d(n // 2), (2-n%2)*D + A + d((n+(2-n%2))//2)) if (n % 3 == 0): ret = min(ret, B + d(n // 3)) else: ret = min(ret, (n%3)*D + B + d(n // 3), (3-n%3)*D + B + d((n+(3-n%3))//3)) if (n % 5 == 0): ret = min(ret, C + d(n // 5)) else: ret = min(ret, (n%5)*D + C + d(n // 5), (5-n%5)*D + C + d((n+(5-n%5))//5)) memo[n] = ret return ret print((d(N))) T -= 1
p02669
from collections import deque from heapq import heapify, heappush as hpush, heappop as hpop def dijkstra(n, E, i0=0): h = [[0, i0]] D = [-1] * n done = [0] * n D[i0] = 0 while h: d, i = hpop(h) done[i] = 1 for j, w in E[i]: nd = d + w if D[j] < 0 or D[j] > nd: if done[j] == 0: hpush(h, [nd, j]) D[j] = nd return D T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) S = set() E = [] Q = deque([N]) while Q: x = deque.popleft(Q) if x in S: continue S.add(x) if x == 0: continue if x <= 10: nx = 0 dd = D * x deque.append(Q, nx) E.append((x, nx, dd)) if x % 2 == 0: nx = x // 2 dd = min(A, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x > 1: nx = x // 2 dd = min(A + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) nx = x // 2 + 1 dd = min(A + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) if x % 3 == 0: nx = x // 3 dd = min(B, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x % 3 == 1: nx = x // 3 dd = min(B + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) else: nx = x // 3 + 1 dd = min(B + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) if x % 5 == 0: nx = x // 5 dd = min(C, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x % 5 == 1: nx = x // 5 dd = min(C + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x % 5 == 2: nx = x // 5 dd = min(C + D * 2, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x % 5 == 3: nx = x // 5 + 1 dd = min(C + D * 2, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) elif x % 5 == 4: nx = x // 5 + 1 dd = min(C + D, (x - nx) * D) deque.append(Q, nx) E.append((x, nx, dd)) # print("E =", len(E), E) A = [] for a, b, d in E: A.append(a) A.append(b) A = sorted(list(set(A))) I = {a: i for i, a in enumerate(A)} E = [(I[a], I[b], d) for a, b, d in E] NN = len(A) # print("E =", len(E), E) EE = [[] for _ in range(NN)] for a, b, d in E: EE[a].append((b, d)) print((dijkstra(NN, EE, I[N])[0])) # print("-----")
from functools import lru_cache def calc(n): @lru_cache(maxsize=None) def rec(n): if n <= 0: return 0 S = [2, 3, 5] mi = D * n for i, a in enumerate(S): for j in range(-a//2, a//2 + 1): if (n + j) % a == 0 and (n + j) // a < n: mi = min(mi, rec((n+j) // a) + X[i] + D * abs(j)) return mi return rec(n) T = int(eval(input())) for _ in range(T): N, A, B, C, D = list(map(int, input().split())) X = (A, B, C) print((calc(N)))
p02669