Problem:


Score : 1000 points

Problem Statement

Rng is baking cookies.

Initially, he can bake one cookie per second.

He can also eat the cookies baked by himself.
When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1.
When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them.
It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.

He wants to give N cookies to Grandma.
Find the shortest time needed to produce at least N cookies not yet eaten.

Constraints

1≦N≦10^{12}

0≦A≦10^{12}

A is an integer.

Partial Score

500 points will be awarded for passing the test set satisfying N≦10^6 and A≦10^6.

Additional 500 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N A

Output

Print the shortest time needed to produce at least N cookies not yet eaten.

Sample Input 1

8 1

Sample Output 1

7

It is possible to produce 8 cookies in 7 seconds, as follows:

After 1 second: 1 cookie is done.

After 2 seconds: 1 more cookie is done, totaling 2. Now, Rng starts eating those 2 cookies.

After 3 seconds: He finishes eating the cookies, and he can now bake 2 cookies per second.

After 4 seconds: 2 cookies are done.

After 5 seconds: 2 more cookies are done, totaling 4.

After 6 seconds: 2 more cookies are done, totaling 6.

After 7 seconds: 2 more cookies are done, totaling 8.

Sample Input 2

1000000000000 1000000000000

Sample Output 2

1000000000000



Sample input:
8 1


Sample output:
7

Submission status: Wrong Answer

Solution code:
n,a=map(int,input().split())
ans=10**18
for k in range(2):
  l=0
  r=n
  while r-l!=1:
    m=(r+l)//2
    if m**(k+1)>=n:
      r=m
    else:
      l=m
  for m in range(k+2):
    if (l**m)*(r**((k+1)-m))>=n:
      ans=min(ans,k*a+l*m+r*((k+1)-m))
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil, floor, log2
import sys

n, a = map(int, next(sys.stdin).strip().split())

t = n
for k in range(1, ceil(log2(n))):
    ds = [n]
    for i in range(k - 1):
        d = ds[-1]
        ds[-1] = floor(d ** (1 / (k - i)))
        ds.append(ceil(d / ds[-1]))
    s = sum(ds) + (k - 1) * a
    if t < s:
        break
    t = s
    k += 1
print(t)

Submission status: Wrong Answer

Solution code:
from math import ceil
import sys

n, a = map(int, next(sys.stdin).strip().split())

cand = [(-a, 1, 2)]
t = n

while len(cand) > 0:
    s, p, k = cand.pop(0)

    if s >= t:
        continue
    if p >= n:
        t = s
    else:
        for d in range(k, min(t - (s + a), 2 + a + 1)):
            cand.append((s + d + a, p * d, d))
print(t)

Submission status: Wrong Answer

Solution code:
from math import ceil
import sys

n, a = map(int, next(sys.stdin).strip().split())

cand = [(-a, 1, 2)]
t = n

while len(cand) > 0:
    s, p, k = cand.pop(0)

    if s >= t:
        continue
    if p >= n:
        t = s
    else:
        for d in range(k, 2 + a + 1):
            cand.append((s + d + a, p * d, d))
print(t)

Submission status: Wrong Answer

Solution code:
n, a = map( long, raw_input().strip().split() )

if n == 1:
	print 1
else:
	s = 1
	x = 2
	ans = 2

	l = 2 * (a+1)
	while x < n:
		ans += 1
		if x <= s:
			x += s
		elif x + l*s >= n:
			ans += ( n - x + s - 1 ) / s - 1
			break
		else :
			s = x
			x = 0

	print ans

Submission status: Time Limit Exceeded

Solution code:
N, A = map(int, raw_input().split())

dp = range(2*N)
dp[0] = 0
for i in range(1, N):
    for j in range(i, 2*N, i):
        dp[j] = min(dp[j], dp[i]+A+j/i)

print(min(dp[N:]))

Submission status: Accepted

Solution code:
n,a=map(int,input().split())
ans=10**18
for k in range(41):
  l=0
  r=n
  while r-l!=1:
    m=(r+l)//2
    if m**(k+1)>=n:
      r=m
    else:
      l=m
  for m in range(k+2):
    if (l**m)*(r**((k+1)-m))>=n:
      ans=min(ans,k*a+l*m+r*((k+1)-m))
print(ans)

Submission status: Accepted

Solution code:
n,a=map(int,raw_input().split())
ans=10**12+5
for eat_num in xrange(45):
    if 2**(eat_num-1)>n:break
    time=eat_num*a
    bake=eat_num+1
    l=0;r=n
    while r-l>1:
        m=(r+l)/2
        if m**bake>=n:r=m
        else:l=m
    for surplus in xrange(1,bake+1):
        if r**(bake-surplus) * (r-1)**surplus < n:break
    surplus-=1
    time+=r*(bake-surplus)+(r-1)*surplus
    ans=min(ans,time)
print ans

Submission status: Accepted

Solution code:
n,a=map(int,raw_input().split())
ans=10**12+5
for eat_num in xrange(45):
    if 2**(eat_num-1)>n:break
    time=eat_num*a
    t=eat_num+1
    l=0;r=n
    while r-l>1:
        m=(r+l)/2
        if m**t>=n:r=m
        else:l=m
    for j in xrange(t+1):
        if r**(t-j) * (r-1)**j < n:break
        surplus=j
    ans=min(ans,time+r*(t-surplus)+(r-1)*surplus)
print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
def pow(a, b):
    if b == 1:return a
    if b%2 == 0: return pow(a*a, int(b/2))
    return a*pow(a*a, int(b/2))
 
def binaryOne(n, multi):
    l, r = 0, n
    mid = (l + r)/2    
    while r - l > 1:
        if pow(mid, multi) >= n:
            r = mid
        else:
            l = mid
        mid = (r + l)/2
    return r;
 
 
def binaryTwo(n, maxi, multi):
    l, r = 0, multi
    mid = (l + r)/2
    while r - l > 1:
        if pow(maxi, (multi - mid))*pow(maxi - 1, mid) >= n:
            l = mid
        else:
            r = mid
        mid = (r + l)/2;
    return l
 
n, a = map(int,raw_input().split(" "))
ans = 1e18
for i in xrange(0, 40):
    maxi = binaryOne(n, i + 1);
    subt = binaryTwo(n, maxi, i + 1)
    ans = min(ans, a*i + maxi*(i + 1) - subt)
    
print ans

Submission status: Accepted

Solution code:
from math import ceil, floor, log2
import sys

n, a = map(int, next(sys.stdin).strip().split())

t = n
for k in range(1, ceil(log2(n))):
    d = ceil(n ** (1 / k))
    l = 0
    r = k
    while l + 1 < r:
        c = (l + r) // 2
        if n <= (d - 1) ** (k - c) * d ** c:
            r = c
        else:
            l = c
    t = min(t, (d - 1) * (k - r) + d * r + (k - 1) * a)
print(t)

Submission status: Runtime Error

Solution code:
n,a=map(int,input().split())
costs=[10**18]*(n+1)
costs[1]=0
times=[0]*(n+1)
for i in range(1,n+1):
  times[i]=costs[i]+(n+i-1)//i
  for j in range(2,n//i+1):
    costs[i*j]=min(costs[i*j],costs[i]+j+a)
print(min(times[1:]))