description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
count = 0
Arr.sort()
total = N * (N - 1) * (N - 2) // 6
count_1 = 0
count_2 = 0
for i in range(N - 1):
l = i + 1
r = N - 1
while l < r:
if Arr[i] + Arr[l] + Arr[r] < L:
count_1 += r - l
l += 1
else:
r -= 1
for i in range(N - 1):
l = i + 1
r = N - 1
while l < r:
if Arr[i] + Arr[l] + Arr[r] > R:
count_1 += r - l
r -= 1
else:
l += 1
return total - (count_1 + count_2)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countvalues(self, arr, n, value):
arr.sort()
j = 0
k = 0
summ = 0
ans = 0
for i in range(0, n - 2):
j = i + 1
k = n - 1
while j != k:
summ = arr[i] + arr[j] + arr[k]
if summ > value:
k = k - 1
else:
ans = ans + k - j
j = j + 1
return ans
def countTriplets(self, Arr, N, L, R):
result = 0
result1 = self.countvalues(Arr, N, R)
result2 = self.countvalues(Arr, N, L - 1)
result = result1 - result2
return result
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
if N < 3:
return 0
Arr.sort()
sum = 0
count = 0
for i in range(2, N):
sp1 = 0
ep1 = i - 1
sp2 = 0
ep2 = i - 1
while sp1 < ep1:
sum_ = Arr[sp1] + Arr[ep1] + Arr[i]
if sum_ < L:
sum += ep1 - sp1
sp1 += 1
else:
ep1 -= 1
while sp2 < ep2:
sum_ = Arr[sp2] + Arr[ep2] + Arr[i]
if sum_ > R:
sum += ep2 - sp2
ep2 -= 1
else:
sp2 += 1
total = N * (N - 1) * (N - 2) // 6
return total - sum
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countlesseq(self, arr, n, target):
count = 0
for i in range(n - 1):
l = i + 1
r = n - 1
while l < r:
total = arr[i] + arr[l] + arr[r]
if total <= target:
count = count + (r - l)
l = l + 1
else:
r = r - 1
return count
def countTriplets(self, Arr, N, L, R):
Arr.sort()
return self.countlesseq(Arr, N, R) - self.countlesseq(Arr, N, L - 1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
if N < 3:
return 0
Arr.sort()
res1 = 0
for i in range(N - 2):
s, e = i + 1, N - 1
while s < e:
sm = Arr[i] + Arr[s] + Arr[e]
if sm <= R:
res1 += e - s
s += 1
else:
e -= 1
res2 = 0
for i in range(N - 2):
s, e = i + 1, N - 1
while s < e:
sm = Arr[i] + Arr[s] + Arr[e]
if sm <= L - 1:
res2 += e - s
s += 1
else:
e -= 1
return abs(res1 - res2)
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, arr, N, L, R):
def countPairs(arr, firstValue, start, target):
left = start
count = 0
right = len(arr) - 1
while left < right:
if firstValue + arr[left] + arr[right] <= target:
count += right - left
left += 1
else:
right -= 1
return count
arr.sort()
countL = 0
countR = 0
count = 0
for i in range(len(arr)):
countL += countPairs(arr, arr[i], i + 1, L - 1)
countR += countPairs(arr, arr[i], i + 1, R)
count += countR - countL
return count
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, arr, N, L, R):
lower = self.getTripletsCount(arr, N, L - 1)
upper = self.getTripletsCount(arr, N, R)
return upper - lower
def getTripletsCount(self, arr, N, target):
count = 0
arr.sort()
for i in range(N - 2):
j = i + 1
k = N - 1
while j < k:
total = arr[i] + arr[j] + arr[k]
if total <= target:
count += k - j
j += 1
else:
k -= 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, arr, n, a, b):
arr.sort()
cnt1 = 0
cnt2 = 0
for i in range(n - 2):
x1 = 0
left1 = i + 1
right1 = n - 1
while left1 < right1:
if arr[left1] + arr[i] + arr[right1] < a:
x1 += right1 - left1
left1 += 1
else:
right1 -= 1
cnt1 += x1
for i in range(n - 2):
x2 = 0
left2, right2 = i + 1, n - 1
while left2 < right2:
if arr[left2] + arr[i] + arr[right2] < b + 1:
x2 += right2 - left2
left2 += 1
else:
right2 -= 1
cnt2 += x2
return cnt2 - cnt1
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, arr, n, L, R):
count1 = 0
arr.sort()
for i in range(len(arr)):
j = i + 1
k = n - 1
while j < k:
if arr[i] + arr[j] + arr[k] < L:
count1 += k - j
j += 1
else:
k -= 1
count2 = 0
for i in range(len(arr)):
j = i + 1
k = n - 1
while j < k:
if arr[i] + arr[j] + arr[k] <= R:
count2 += k - j
j += 1
else:
k -= 1
return abs(count2 - count1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
Arr.sort()
n = N
count1 = 0
for i in range(N):
lower = L - Arr[i] - 1
j = i + 1
k = N - 1
while j < k:
rem = Arr[j] + Arr[k]
if rem <= lower:
count1 += k - j
j += 1
elif rem > lower:
k = k - 1
count2 = 0
for i in range(N):
upper = R - Arr[i]
j = i + 1
k = N - 1
while j < k:
rem = Arr[j] + Arr[k]
if rem <= upper:
count2 += k - j
j += 1
elif rem > upper:
k = k - 1
return count2 - count1
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
Arr.sort()
mins = self.minSum(Arr, N, L)
Arr.sort(reverse=True)
maxs = self.maxSum2(Arr, N, R)
total = int(N * (N - 1) * (N - 2) / 6)
return total - (mins + maxs)
def minSum(self, arr, N, L):
count = 0
for i in range(N - 2):
j = i + 1
k = N - 1
while j < k:
sum = arr[i] + arr[j] + arr[k]
if sum < L:
count += k - j
j = j + 1
else:
k = k - 1
return count
def maxSum(self, arr, N, R):
count = 0
for i in range(N - 2):
j = i + 1
k = j + 1
while k < N:
sum = arr[i] + arr[j] + arr[k]
if sum > R:
count += N - k
j = j + 1
k = j + 1
else:
k = k + 1
return count
def maxSum2(self, arr, N, R):
count = 0
for i in range(N - 2):
j = i + 1
k = N - 1
while j < k:
sum = arr[i] + arr[j] + arr[k]
if sum > R:
count += k - j
j = j + 1
else:
k = k - 1
return count
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def countTriplets(self, Arr, N, L, R):
res = self.countTripletsLessThan(Arr, R) - self.countTripletsLessThan(
Arr, L - 1
)
return res
def countTripletsLessThan(self, Arr, val):
count = 0
Arr.sort()
for i in range(len(Arr) - 2):
j = i + 1
k = len(Arr) - 1
while j < k:
total = Arr[i] + Arr[j] + Arr[k]
if total > val:
k -= 1
else:
count += k - j
j += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array Arr[] of N distinct integers and a range from L to R, the task is to count the number of triplets having a sum in the range [L, R].
Example 1:
Input:
N = 4
Arr = {8 , 3, 5, 2}
L = 7, R = 11
Output: 1
Explaination: There is only one triplet {2, 3, 5}
having sum 10 in range [7, 11].
Example 2:
Input:
N = 5
Arr = {5, 1, 4, 3, 2}
L = 2, R = 7
Output: 2
Explaination: There two triplets having
sum in range [2, 7] are {1,4,2} and {1,3,2}.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countTriplets() which takes the array Arr[] and its size N and L and R as input parameters and returns the count.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{3}
1 β€ Arr[i] β€ 10^{3}
1 β€ L β€ R β€ 10^{9}
|
class Solution:
def smaller(self, arr, n, l, r):
ans = 0
for i in range(n - 1):
j = i + 1
k = n - 1
while j < k:
total = arr[i] + arr[j] + arr[k]
if total < l:
ans += k - j
j += 1
if total >= l:
k -= 1
return ans
def greater(self, arr, n, l, r):
ans = 0
for i in range(n - 1):
j = i + 1
k = n - 1
while j < k:
total = arr[i] + arr[j] + arr[k]
if total > r:
ans += k - j
k -= 1
if total <= r:
j += 1
return ans
def countTriplets(self, arr, n, l, r):
arr.sort()
c1 = self.smaller(arr, n, l, r)
c2 = self.greater(arr, n, l, r)
total = n * (n - 1) * (n - 2) // 6
return total - (c1 + c2)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
[n, H] = map(int, input().strip().split())
def bsearch(f, xmin, xmax):
while xmax - xmin > 1:
xmid = (xmax + xmin) // 2
if f(xmid):
xmax = xmid
else:
xmin = xmid
return xmax
def f1(x):
return 2 * n <= x * (x + 1)
HH = H * (H + 1) // 2
def f2(x):
r = x - H
R0 = r // 2
R1 = (r + 1) // 2
return n <= HH + H * r + R0 * R1
if n <= HH:
x = bsearch(f1, 0, H)
else:
xmin = H
xmax = H
while not f2(xmax):
xmin = xmax
xmax *= 2
x = bsearch(f2, xmin, xmax)
print(x)
|
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def calc(mid):
if mid <= h:
return (1 + mid) * mid // 2
peak = h - 1 + (mid - (h - 1) + 1) // 2
if (mid - (h - 1)) % 2 == 0:
return (h + peak) * (peak - h + 1) // 2 + (1 + peak) * peak // 2
return (h + peak) * (peak - h + 1) // 2 + peak * (peak - 1) // 2
l, r = 0, n + 1
while r - l > 1:
mid = (l + r) // 2
if calc(mid) < n:
l = mid
else:
r = mid
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def check(p):
return p * (p + 1) // 2 >= min(n, H * (H + 1) // 2)
def binSearch(a, b):
left, right = a - 1, b + 1
while right - left > 1:
mid = (left + right) // 2
if check(mid):
right = mid
else:
left = mid
return right
n, H = map(int, input().split())
b = binSearch(1, H)
if n <= H * (H + 1) // 2:
print(b)
exit()
S_0 = H * (H - 1) // 2
def check(p):
return S_0 + (2 * H + p - 1) * p >= n
def binSearch(a, b):
left, right = a - 1, b + 1
while right - left > 1:
mid = (left + right) // 2
if check(mid):
right = mid
else:
left = mid
return right
b_1 = binSearch(1, 10**10)
delta = S_0 + (2 * H + b_1 - 1) * b_1 - n
print(b - 1 + 2 * b_1 - (delta >= b + b_1 - 1))
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = list(map(int, input().split()))
tmp = (H + 1) * H >> 1
if n < tmp:
l, r = 0, 1000000000000000000
while l < r:
mid = l + r >> 1
tmp = (mid + 1) * mid >> 1
if n > tmp:
l = mid + 1
else:
r = mid
print(r)
else:
l, r = H, 1000000000000000000
while l < r:
mid = l + r >> 1
if mid + H & 1:
T = mid + H >> 1
tmp = ((T + H) * (T - H + 1) >> 1) + (T * (T + 1) >> 1)
else:
T = mid + H >> 1
tmp = ((T + H) * (T - H + 1) >> 1) + (T * (T - 1) >> 1)
if n > tmp:
l = mid + 1
else:
r = mid
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
import sys
n, H = map(int, input().split())
ok, ng = 0, n + 1
while abs(ok - ng) > 1:
mid = ok + ng >> 1
if mid * (mid + 1) >> 1 <= n:
ok = mid
else:
ng = mid
if ok <= H:
n -= ok * (ok + 1) >> 1
print(ok + (1 if n else 0))
exit()
ok, ng = H, 10**18
while abs(ok - ng) > 1:
mid = ok + ng >> 1
m = mid * (mid + 1) - ((H - 1) * H >> 1) - mid
if m <= n:
ok = mid
else:
ng = mid
n -= ok * (ok + 1) - ((H - 1) * H >> 1) - ok
print(ok + (ok - H) + (n + ok - 1) // ok)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def judge(mid, n, H):
if mid <= H:
return mid * (mid + 1) // 2
right = mid * (mid + 1) // 2
left = (mid - H + 1) * H + (mid - H + 1) * (mid - H + 1 - 1) // 2
return left + right - mid
def solve(n, H):
low = 1
high = 1000000000000000000
while True:
mid = low + high >> 1
now = judge(mid, n, H)
if low == mid:
if now >= n:
return low + max(0, low - H)
elif now + low >= n:
return low + max(0, low - H) + 1
return high + max(0, high - H)
if now == n:
return mid + max(0, mid - H)
elif now < n:
low = mid
elif now > n:
high = mid
pass
try:
while True:
n, H = input().split()
n = int(n)
H = int(H)
print(solve(n, H))
except EOFError:
pass
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
if H * (H + 1) // 2 >= n:
l, r = 0, 10**18
while r - l > 1:
mid = r + l >> 1
if mid * (mid + 1) // 2 >= n:
r = mid
else:
l = mid
print(r)
else:
n += H * (H - 1) // 2
l, r = 0, 10**18
calc = lambda x: (x + 1) // 2 * (x // 2 + 1)
while r - l > 1:
mid = r + l >> 1
if calc(mid) >= n:
r = mid
else:
l = mid
print(-H + 1 + r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f(mid, h):
if mid <= h:
return mid * (mid + 1) // 2
p = (mid - h + 1) // 2
return (h + h + p - 1) * p // 2 + (mid - p) * (mid - p + 1) // 2
n, h = map(int, input().split())
l, r = 0, 10000000000
while l + 1 < r:
mid = (l + r) // 2
if f(mid, h) >= n:
r = mid
else:
l = mid
print(r)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def cal(x):
return (1 + x) * x // 2
def g(x):
if x % 2 == 0:
return 2 * cal(x // 2)
else:
return cal(x // 2 + 1) + cal(x // 2)
def check2(x, num):
if g(x) >= num:
return 1
return 0
n, h = (int(x) for x in input().split(" "))
if cal(h) >= n:
lo, hi = 1, n
ans = -1
while lo <= hi:
mid = (lo + hi) // 2
if cal(mid) >= n:
ans = mid
hi = mid - 1
else:
lo = mid + 1
print(ans)
else:
h = h - 1
n += cal(h)
lo, hi = 1, n
ans = -1
while lo <= hi:
mid = (lo + hi) // 2
if g(mid) >= n:
ans = mid
hi = mid - 1
else:
lo = mid + 1
print(ans - h)
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def gs(x):
return x * (x + 1) // 2
def gs2(x):
if x % 2 == 0:
cr = 0
else:
cr = x // 2 + 1
cr += h * x
cr += (x // 2 + 1) * (x // 2)
return cr
r = 10**18 + 7
n, h = [int(i) for i in input().strip().split()]
l = 0
r = min(h + 1, r)
while r - l > 1:
m = (l + r) // 2
if gs(m) <= n:
l = m
else:
r = m
tr = l
n -= gs(l)
if n >= h:
n -= h
tr += 1
l = 0
r = 10**18 + 7
while r - l > 1:
m = (l + r) // 2
if gs2(m) <= n:
l = m
else:
r = m
tr += l
n -= gs2(l)
if n > 0:
tr += 1
print(tr)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def max_number_of_packs(number_of_spots, fence_height):
if number_of_spots > fence_height:
k = (number_of_spots - fence_height) // 2 + fence_height
l = number_of_spots - k
return k * (k + 1) // 2 + l * (l + 1) // 2 + l * (fence_height - 1)
else:
return number_of_spots * (number_of_spots + 1) // 2
def binary_search(left, right, number_of_packs, fence_height):
if left >= right - 1:
return right
middle = (left + right) // 2
max_packs = max_number_of_packs(middle, fence_height)
if max_packs < number_of_packs:
return binary_search(middle, right, number_of_packs, fence_height)
else:
return binary_search(left, middle, number_of_packs, fence_height)
n, h = map(int, input().split())
print(binary_search(0, 10**18, n, h))
|
FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def main():
n, h = map(int, input().split())
l = -1
r = 10**18
while r - l != 1:
m = (r + l) // 2
t = m + h - 1
if m > h:
if t % 2 == 1:
j = t // 2 + 1 - h + 1
s = j * (j + 1) // 2 + j * (h - 1) + (m - j) * (m - j + 1) // 2
if s >= n:
r = m
else:
l = m
else:
j = t // 2 - h + 1
s = j * (j + 1) // 2 + j * (h - 1) + (m - j) * (m - j + 1) // 2
if s >= n:
r = m
else:
l = m
else:
s = m * (m + 1) // 2
if s >= n:
r = m
else:
l = m
print(r)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
if n > h * (h + 1) / 2:
l = 1
r = 2000000000000000000
h0 = 2000000000000000000
while l <= r:
mid = (l + r) // 2
if mid * mid - h * (h - 1) // 2 <= n:
h0 = mid
l = mid + 1
else:
r = mid - 1
ans = 2 * h0 - h
n = n - h0 * h0 + h * (h - 1) // 2
ans = ans + (n + h0 - 1) // h0
print(ans)
else:
l = 1
r = h
ans = h
while l <= r:
mid = (l + r) // 2
if mid * (mid + 1) // 2 <= n:
ans = mid
l = mid + 1
else:
r = mid - 1
n = n - ans * (ans + 1) // 2
ans = ans + (n + ans - 1) // ans
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def getSum1(x):
return (x + 1) * x // 2
def getSum(l, r):
return getSum1(r) - getSum1(l - 1)
n, h = map(int, input().split())
l = 1
r = n
while l != r:
m = l + r + 1 >> 1
cnt = 0
if m > h:
cnt += getSum(h, m - 1)
cnt += getSum1(m)
if cnt > n:
r = m - 1
else:
l = m
cnt = 0
ans = 0
if l > h:
cnt += getSum(h, l - 1)
ans += l - h
cnt += getSum1(l)
ans += l
n -= cnt
ans += n // l
if n % l != 0:
ans += 1
print(ans)
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
l = 0
r = n + 1
while r - l > 1:
mid = (l + r) // 2
t = min(mid, H) * (min(mid, H) + 1) // 2
if mid > H:
tmid = mid - H
t += tmid * H
s = tmid // 2
t += s * (s + 1)
if tmid % 2 == 0:
t -= s
if t >= n:
r = mid
else:
l = mid
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = input().split()
n = int(n)
h = int(h)
if n > (1 + h) * h // 2:
l = h
r = n + 1
def check(v):
if (h + v) * (v - h + 1) // 2 + (1 + v) * v // 2 - v > n:
return False
return True
while l < r:
mid = (l + r) // 2 + 1
if check(mid):
l = mid
else:
r = mid - 1
n -= (h + l) * (l - h + 1) // 2
ans = l - h + 1
n -= (1 + l) * l // 2 - l
ans += l - 1
ans += (n + l - 1) // l
print(ans)
else:
l = 1
r = h
def check(mid):
return (1 + mid) * mid // 2 >= n
while l < r:
mid = (l + r) // 2
if check(mid):
r = mid
else:
l = mid + 1
print(l)
|
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = list(map(int, input().split()))
def p(k):
return k * (k + 1) // 2
def pp(k):
x = 0
if k >= 2:
x = k * (k - 1) // 2
return int(x + h * k + (h + k) * (h + k + 1) // 2)
temp = h * (h + 1) // 2
c = 0
if n - temp >= 0:
lb = 0
ub = 100000000000000000000
while lb < ub:
mid = (lb + ub + 1) // 2
if n < pp(mid):
ub = mid - 1
else:
lb = mid
sk = pp(lb)
k = lb
if sk == n:
print(h + 2 * k)
elif n <= sk + k + h:
print(h + 2 * k + 1)
else:
print(h + 2 * k + 2)
else:
lb = 1
ub = h
while lb < ub:
mid = (lb + ub + 1) // 2
if p(mid) <= n:
lb = mid
else:
ub = mid - 1
if p(lb) == n:
print(lb)
else:
print(lb + 1)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f(k, n, h):
if k <= h:
return k * (k + 1) // 2 >= n
m = (h + k) // 2
return 2 * m * (m + 1) // 2 - (h - 1) * h // 2 - m * (2 * m - k == h) >= n
s = input().split()
n = int(s[0])
h = int(s[1])
b = 1
e = n
while b < e:
m = (b + e) // 2
if f(m, n, h):
e = m
else:
b = m + 1
print(b)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = [int(i) for i in input().split()]
start = 0
end = n
while start != end - 1:
mid = (start + end) // 2
h = min(H, mid)
result = mid**2 - h * (h - 1) // 2
if result <= n:
start = mid
else:
end = mid
ans = 0
if H >= start:
l1 = start
first = start * (start + 1) // 2
tomake = n - first
if start != 0:
add1 = tomake // start
else:
add1 = tomake
if start != 0:
if tomake % start != 0:
add1 += 1
print(l1 + add1)
else:
l1 = start + (start - H)
first = start * (start + 1) // 2
second = (H + start - 1) * (start - H) // 2
full = first + second
tomake = n - full
add1 = tomake // start
if tomake % start != 0:
add1 += 1
print(l1 + add1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
left = 0
right = 10**18
while left + 1 != right:
mid = (left + right) // 2
if (mid + min(mid, h)) % 2 == 0:
ans = (mid + min(h, mid)) // 2 * ((mid + min(mid, h)) // 2) - min(mid, h) * (
min(mid, h) - 1
) // 2
if ans >= n:
right = mid
else:
left = mid
else:
ans = ((mid + min(mid, h)) // 2 + 1) * ((mid + min(mid, h)) // 2) - min(
mid, h
) * (min(mid, h) - 1) // 2
if ans >= n:
right = mid
else:
left = mid
print(right)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
def f(x):
if x <= H:
return x * (x + 1) // 2
if (x + H) % 2 == 0:
M = (x + H) // 2
return M * M - H * (H - 1) // 2
else:
M = (x + H - 1) // 2
return M * (M + 1) - H * (H - 1) // 2
lo, hi = -1, n + 1
while hi - lo > 1:
mid = (lo + hi) // 2
if f(mid) >= n:
hi = mid
else:
lo = mid
print(hi)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def ok(val):
x = val - h
res = get(val) + (h + val - 1) * x // 2
return res
def ok2(val):
res = get(val) + (h + val) * (val - h + 1) // 2
return res
def get(val):
return (1 + val) * val // 2
line = input().split(" ")
n = line[0]
h = line[1]
n = int(n)
h = int(h)
ans = -1
if get(h) >= n:
l = 1
r = n
while l <= r:
mid = (l + r) // 2
if get(mid) >= n:
ans = mid
r = mid - 1
else:
l = mid + 1
print(ans)
else:
l = h
r = 10**12
while l <= r:
mid = (l + r) // 2
if ok(mid) >= n:
ans1 = mid
r = mid - 1
else:
l = mid + 1
ans1 = ans1 + ans1 - h
l = h
r = 10**12
ans2 = -1
while l <= r:
mid = (l + r) // 2
if ok2(mid) >= n:
ans2 = mid
r = mid - 1
else:
l = mid + 1
ans2 = ans2 + ans2 - h + 1
if ans1 > ans2:
print(ans2)
else:
print(ans1)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().strip().split())
def c(x):
if x > H:
tmp = (H + x) * (x - H + 1) / 2 + x * (x - 1) / 2
else:
tmp = (1 + x) * x / 2
if tmp <= n:
return True
else:
return False
l, r = 0, n
while l <= r:
mid = int((l + r) / 2)
if c(mid):
ans = mid
l = mid + 1
else:
r = mid - 1
if ans > H:
tmp = n * 2 - ((H + ans) * (ans - H + 1) + ans * (ans - 1))
tmp = int(tmp / 2)
if tmp > ans:
print(ans * 2 - H + 2)
elif tmp > 0:
print(ans * 2 - H + 1)
else:
print(ans * 2 - H)
else:
tmp = n * 2 - (1 + ans) * ans
tmp = int(tmp / 2)
if tmp > ans:
print(ans + 2)
elif tmp > 0:
print(ans + 1)
else:
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def sum(n):
if not n:
return 0
return n * (n + 1) // 2
def can(len, max):
height = len - max + 1
return height - max + 1 <= h
def sum(n):
return n * (n + 1) // 2
def sq(len, max):
height = len - max + 1
res = sum(height) + (max - 1) * height
if height > h:
res -= sum(height - h)
return res
def good(len):
if len == 1:
return n == 1
l = 1
r = len
while l < r:
m = (l + r) // 2
if can(len, m):
r = m
else:
l = m + 1
return sq(len, l) >= n
l = 1
r = n
while l < r:
m = (l + r) // 2
if good(m):
r = m
else:
l = m + 1
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
m = n
n += H * (H - 1) / 2
l = H
r = 2000000000000000000
while l + 1 < r:
mid = (l + r) // 2
heap = mid // 2
kek = 0
if mid % 2 == 1:
kek += heap * (heap + 1)
kek += heap + 1
else:
kek += heap * (heap + 1)
if kek >= n:
r = mid
else:
l = mid
a = 0
b = H
while a + 1 < b:
mid = (a + b) // 2
kek = mid * (mid + 1) // 2
if kek >= m:
b = mid
else:
a = mid
if H * (H + 1) // 2 >= m:
print(b)
exit()
print(r - H + 1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def sumab(a, b):
return (a + b) * (b - a + 1) // 2
def calc(spot, H):
if spot <= H:
return spot * (spot + 1) // 2
if (H - spot) % 2 == 0:
return sumab(1, H + (spot - H) // 2) + sumab(H, H + (spot - H) // 2 - 1)
return (
sumab(1, H + (spot - H) // 2)
+ sumab(H, H + (spot - H) // 2 - 1)
+ H
+ (spot - H) // 2
)
def solve():
n, H = map(int, input().split())
if n == 1:
print(1)
return 0
st = 1
en = 10**20
while st < en:
mid = (st + en) // 2
if calc(mid, H) >= n:
en = mid
else:
st = mid + 1
print(st)
solve()
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def check(osn):
global h
global n
if osn <= h:
res = osn * (osn + 1) // 2
else:
max_h = (h + osn) // 2
del_h = max(min(max_h - (osn - max_h), osn), 1)
res = max_h * max_h - del_h * (del_h - 1) // 2
if (h + osn) % 2 != 0:
res += osn - max_h
if res >= n:
return True
return False
n, h = map(int, input().split())
l = 1
r = 10**18 + 10
while r - l > 1:
m = (l + r) // 2
if check(m):
r = m
else:
l = m
if check(l):
print(l)
else:
print(r)
|
FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def compute(H, mid):
if mid <= H:
return (mid + 1) * mid // 2
else:
remain = mid - H + 1
ret = (H - 1) * H / 2 + H * remain
t = (mid - H) // 2
if remain % 2:
ret += t * t
else:
ret += (t + 1) * t
return ret
n, H = map(int, input().split())
left = 1
right = 10**12
while left < right:
mid = (left + right) // 2
maxuse = compute(H, mid)
if maxuse >= n:
right = mid
else:
left = mid + 1
print(right)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def binsearch():
l = 0
r = 10**10
while r - l > 1:
m = (l + r) // 2
if (m * m + m) // 2 <= n:
l = m
else:
r = m
return l
def solve(x):
val = x * x + (h - h * h) // 2
if val > n or x < 1:
return 2 * 10**18
else:
return 2 * x - h + (0 if n == val else (n - val - 1) // x + 1)
n, h = map(int, input().split())
if (h + h * h) / 2 > n:
x = binsearch()
val = (x * x + x) // 2
if val == n:
print(x)
elif n - val <= x:
print(x + 1)
elif n - val <= 2 * x:
print(x + 2)
else:
print(x + 3)
else:
x = int((n + (h * h - h) // 2) ** 0.5)
ans = 2 * 10**18
for c in range(x - 100, x + 1):
ans = min(ans, solve(c))
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP NUMBER BIN_OP NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
l = 1
r = 10**10
while r - l > 1:
mid = l + r >> 1
if mid <= h:
calc = (mid + 1) * mid
calc = calc >> 1
if calc > n:
r = mid
else:
l = mid
continue
calc = (mid + 1) * mid
calc = calc >> 1
calc += (mid + h - 1) * (mid - h) // 2
if calc > n:
r = mid
else:
l = mid
ans = l
if l > h:
ans += l - h
calc = (l + 1) * l
calc = calc >> 1
if l > h:
calc += (l + h - 1) * (l - h) // 2
n -= calc
ans += n // l
n %= l
if n > 0:
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def solve(mid):
sum_n = mid * (mid + 1) // 2
return sum_n <= n
ok = 0
ng = h + 1
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
if h != ok:
ans = ok
nokori = n - ans * (ans + 1) // 2
if nokori > 0:
ans += 1
print(ans)
else:
ans = ok
nokori = n - ans * (ans + 1) // 2
while True:
if 10**6 * 2 * h + (10**6) ** 2 <= nokori:
nokori -= 10**6 * 2 * h + (10**6) ** 2
ans += 2 * 10**6
h += 10**6
continue
if h + 1 + h <= nokori:
nokori -= h + 1 + h
ans += 2
h += 1
else:
ans += -(-nokori // h)
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR BIN_OP NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = [int(x) for x in input().split()]
l = -1
r = n + 1
m = (l + r) // 2
while r - l > 1:
if m <= h:
v = m * (m + 1) // 2
else:
v = m * (m + 1) // 2 - h * (h - 1) // 2 + m * (m - 1) // 2
if v <= n:
l = m
else:
r = m
m = (l + r) // 2
if l < h:
v = l * (l + 1) // 2
print(l + (n - v) // l + ((n - v) % l != 0))
exit()
else:
v = l * (l + 1) // 2 - h * (h - 1) // 2 + l * (l - 1) // 2
print(l - h + l + (n - v) // l + ((n - v) % l != 0))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
d = h * (h + 1) // 2
if n >= d:
ok = h
ng = 10**20
while ng - ok > 1:
mid = (ok + ng) // 2
if (h + mid) * (mid - h + 1) // 2 + mid * (mid - 1) // 2 <= n:
ok = mid
else:
ng = mid
s = (h + ok) * (ok - h + 1) // 2 + ok * (ok - 1) // 2
print(ok + ok - h + (n - s + ok - 1) // ok)
else:
ok = 10**20
ng = 0
while ok - ng > 1:
mid = (ok + ng) // 2
if mid * (mid + 1) // 2 >= n:
ok = mid
else:
ng = mid
print(ok)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def sumi(a, b):
return (a + b) * (b - a + 1) // 2
def main():
n, H = list(map(int, input().split()))
volh = (H + 1) * H // 2
if n < volh:
low = 0
high = n + 1
while high - low > 1:
mid = low + (high - low) // 2
vol = (mid + 1) * mid // 2
if vol >= n:
high = mid
else:
low = mid
print(high)
return
low = 0
high = n + 1
while high - low > 1:
mid = low + (high - low) // 2
twoi = mid - H + 2
if twoi % 2 != 0:
peakh = H + (twoi - 3) // 2
vol = sumi(H, peakh) + sumi(1, peakh)
else:
peakh = H + (twoi - 2) // 2
vol = sumi(H, peakh) + sumi(1, peakh - 1)
if vol >= n:
high = mid
else:
low = mid
print(high)
main()
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def bin(f, l, val, h):
mid = int((f + l) / 2)
p = mid * (mid + 1) - int(h * (h - 1) / 2) - mid
if p <= val and val < (mid + 1) * (mid + 2) - int(h * (h - 1) / 2) - mid - 1:
return mid
elif val >= p:
f = mid + 1
return bin(f, l, val, h)
elif val < p:
l = mid - 1
return bin(f, l, val, h)
else:
return mid
def binS(f, l, val):
mid = int((f + l) / 2)
p = mid * (mid + 1) / 2
if p <= val and val < (mid + 1) * (mid + 2) / 2:
return mid
elif val >= p:
f = mid + 1
return binS(f, l, val)
elif val < p:
l = mid - 1
return binS(f, l, val)
n, h = input().split()
n = int(n)
h = int(h)
an = binS(0, pow(10, 12), n)
if an <= h:
if an == 1414213560 - 1:
an += 1
if int(an * (an + 1) / 2) == n:
print(an)
else:
print(an + 1)
else:
an = bin(0, pow(10, 12), n, h)
if int(an * (an + 1)) - int(h * (h - 1) / 2) < n:
print(an * 2 - h + 2)
elif an * (an + 1) - int(h * (h - 1) / 2) - an == n:
print(2 * an - h)
else:
print(2 * an - h + 1)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER VAR IF VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def bst(n):
l = 0
r = 10**44
o = 0
if n <= 0:
return 0
while l <= r:
m = (l + r) // 2
if m * (m + 1) // 2 >= n:
o = m
r = m - 1
else:
l = m + 1
return o
def asc(x0, m):
return x0 * m + m * (m - 1) // 2
def fitt(m, h):
if m <= h:
return m * (m + 1) // 2
y = (m + h) // 2
x = (m - h) // 2 + 1
return asc(h, x) + asc(1, m - x)
def resi():
n, h = map(int, input().split())
t = bst(n)
if t <= h:
return t
l = 0
r = 10**44
sol = r
while l <= r:
m = (l + r) // 2
if fitt(m, h) >= n:
sol = m
r = m - 1
else:
l = m + 1
return sol
print(resi())
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n = 0
H = 0
def test(k):
if k <= H:
return (k + 1) * k // 2 >= n
else:
a = H * (2 * k + 1 - H) // 2
cnt = (k - H - 1 + 1) // 2
if (k - H - 1) % 2 == 0:
start = 2
else:
start = 1
b = (start + (k - H - 1)) * cnt // 2
return a + b >= n
n, H = (int(x) for x in input().split())
lo = 1
hi = n
while lo < hi:
mid = (lo + hi) // 2
if test(mid):
hi = mid
else:
lo = mid + 1
print(lo)
|
ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
base = h * (h + 1) // 2
if n <= base:
lo = 0
hi = h + 1
while lo < hi:
mi = (lo + hi) // 2
if mi * (mi + 1) // 2 >= n:
hi = mi
else:
lo = mi + 1
print(lo)
else:
lo = 1
hi = 10**20
def get(x):
res = 0
res += base
res += x * h
y = x - 1
top = (y + 1) // 2
if y % 2 == 1:
res += top * (top - 1)
res += top
else:
res += top * (top + 1)
return res
while lo < hi:
mi = (lo + hi) // 2
if get(mi) >= n:
hi = mi
else:
lo = mi + 1
print(lo + h)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = list(map(int, input().split()))
a = 1
b = n
while 1:
m = (a + b) // 2
t1 = ((h + m - 1) * max(m - h, 0) + m * (m + 1)) // 2
t2 = ((h + m) * max(m + 1 - h, 0) + (m + 1) * (m + 2)) // 2
if t1 > n:
b = m
elif n < t2:
l = max(m - h, 0) + m
l, t = l + (n - t1) // m, t1 + (n - t1) // m * m
if t < n:
l += 1
print(l)
break
else:
a = m
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def tri(n):
return n * (n + 1) // 2
def struct_size(n):
half_n = n // 2
if n % 2 == 0:
return 2 * tri(half_n)
else:
return tri(half_n) + tri(half_n + 1)
n, h = map(int, input().split())
a, b, c = 0, n, 0
while a < b:
c = (a + b) // 2
h_min = min([h - 1, c // 2])
if struct_size(c) - tri(h_min) < n:
a = c + 1
else:
b = c
h_min = min([h - 1, a // 2])
result = a - h_min
print(result)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
import sys
readline = sys.stdin.readline
def check(x):
if x <= H:
if x * (x + 1) // 2 >= N:
return True
else:
return False
res = x - H - 1
r1 = res // 2
r2 = res - r1
if (r1 * (r1 + 1) + r2 * (r2 + 1)) // 2 + (x - H) * H + H * (H + 1) // 2 >= N:
return True
else:
return False
N, H = map(int, readline().split())
ok = N
ng = 0
while abs(ok - ng) > 1:
med = (ok + ng) // 2
if check(med):
ok = med
else:
ng = med
print(ok)
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f(N, H, n):
total = 0
if n <= H:
total = n * (n + 1) // 2
else:
total = H * (H + 1) // 2
n -= H
n -= 1
total += H
q, r = n // 2, n % 2
total += (q * (q + 1) // 2 + q * H) * 2
total += r * (H + q + 1)
return total >= N
n, H = map(int, input().split())
l = 0
r = n
while l < r:
m = (l + r) // 2
if f(n, H, m):
r = m
else:
l = m + 1
print(r)
|
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
l = 1
r = n
while l < r:
mid = int((l + r) / 2)
ans = 0
if mid <= H:
ans = int(mid * (mid + 1) // 2)
pass
else:
R = int(mid - (H - 1))
Len = int(R - 2)
ans = ans + int(H * (H - 1) // 2)
tmp = int(R / 2)
ans = ans + (H + H + tmp - 1) * tmp + (R - tmp * 2) * (H + tmp)
pass
if ans >= n:
r = mid
else:
l = mid + 1
pass
print("%d" % l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, k = map(int, input().split())
def get(x):
if x <= k:
return x * (x + 1) // 2
res = k * x - k * (k - 1) // 2
sz = x - k - 1
if sz % 2 == 0:
cnt = sz // 2
res += (2 + sz) * cnt // 2
else:
cnt = sz // 2 + 1
res += (1 + sz) * cnt // 2
return res
l = 0
r = 10**18
while r - l > 1:
m = l + (r - l) // 2
if get(m) >= n:
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
INF = 1000000000000000000
def f(k, n, h):
h1 = min(k, h)
a = k * k
b = h1 * (h1 - 1) // 2
c = a - b
return c > n
n, h = map(int, input().split())
low = 1
high = INF
while low < high:
mid = low + (high - low) // 2
if f(mid, n, h) == True:
high = mid
else:
low = mid + 1
k = low - 1
h1 = min(k, h)
spot = 2 * k - h1
requires = k * k - h1 * (h1 - 1) // 2
leftOver = n - requires
spot += (leftOver + k - 1) // k
print(spot)
|
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def check(a):
if a <= h:
return (a + 1) * a // 2 >= n
else:
kol = a - h + 1
kol1 = (a - h + 1) // 2
kol2 = kol - kol1
return (h - 1) * h // 2 + (h + kol1 - 1) * (h + kol1) // 2 - (
h - 1
) * h // 2 + (h + kol2 - 1) * (h + kol2) // 2 - (h - 1) * h // 2 >= n
n, h = map(int, input().split())
l = 0
r = n
while l + 1 < r:
m = (l + r) // 2
if check(m):
r = m
else:
l = m
print(r)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def ii():
return int(input())
def mi():
return map(int, input().split())
def li():
return list(mi())
N, H = mi()
tar = N + H * (H - 1) // 2
lo, hi = H, tar
while lo < hi:
mid = (lo + hi + 1) // 2
if mid * mid <= tar:
lo = mid
else:
hi = mid - 1
if lo * lo == tar:
ans = 2 * lo - H
elif lo * lo > tar:
lo1, hi1 = 1, N
while lo1 < hi1:
mid1 = (lo1 + hi1 + 1) // 2
if mid1 * (mid1 + 1) // 2 <= N:
lo1 = mid1
else:
hi1 = mid1 - 1
ans = lo1
if lo1 * (lo1 + 1) // 2 < N:
ans += 1
else:
dif = tar - lo * lo
if dif <= lo:
ans = 2 * lo - H + 1
else:
ans = 2 * lo - H + 2
print(ans)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
import sys
def main():
import sys
input = sys.stdin.readline
N, H = map(int, input().split())
if H * (H + 1) // 2 >= N:
ok = H
ng = 0
mid = (ok + ng) // 2
while ok - ng > 1:
if (mid + 1) * mid // 2 >= N:
ok = mid
else:
ng = mid
mid = (ok + ng) // 2
print(ok)
exit()
ok = 10**12
ng = 0
mid = (ok + ng) // 2
while ok - ng > 1:
if mid + H & 1:
x = (H + mid - 1) // 2
M = x * (x + 1) // 2 + (x + H) * (x - H + 1) // 2
else:
x = (H + mid) // 2
M = x * (x + 1) // 2 + (x - 1 + H) * (x - H) // 2
if M >= N:
ok = mid
else:
ng = mid
mid = (ok + ng) // 2
print(ok)
main()
|
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def get(a):
if a <= h:
return a * (a + 1) // 2
tmp = a - h + 1
re = h * (h - 1) // 2
l = (tmp + 1) // 2
r = tmp // 2
re += l * (h - 1 + l + h) // 2
re += r * (h - 1 + r + h) // 2
return re
lo = 1
hi = 1000000000000000001
n, h = list(map(int, input().split()))
while lo < hi:
mi = (lo + hi) // 2
if get(mi) < n:
lo = mi + 1
else:
hi = mi
print(lo)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def ok(x, n, h):
if x == 1:
return n == 1
if x == 2:
return h >= 2 and n <= 3 or h == 1 and n <= 2
ubound = 0
if x <= h:
ubound = x * (x + 1) // 2
elif x == h + 1:
ubound = h + (x - 1) * (h + 1) // 2
else:
exc = x - h
if exc % 2 == 1 and x % 2 == 0:
exc += 1
coll = exc // 2 + 1
if coll % 2 == 0 or h % 2 == 1:
ubound = (coll - 1) * (h + h + coll - 2) // 2 + (x - (coll - 1)) * (
h + coll - 1 + 1
) // 2
else:
ubound = (
coll * (h + h + coll - 1) // 2 + (x - coll) * (h + coll - 1 + 1) // 2
)
return ubound >= n
n, h = [int(x) for x in input().split(" ")]
a = n
b = 1 << 61
while b >= 1:
if a - b >= 1 and ok(a - b, n, h):
a -= b
b = b // 2
print(a)
|
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def f(x):
return x * (x + 1) // 2
def g(x, th):
if x == 0:
return 0
if x == 1:
return th
if x == 2:
return th + th
ret = x * th
mx = (x - 1) / 2
if x & 1:
ret += f(mx) + f(mx - 1)
else:
ret += f(mx) + f(mx)
return ret
lb, ub = 0, 2000000000
while lb < ub:
mid = lb + ub >> 1
if f(mid) > n:
ub = mid
elif f(mid) == n:
ub = lb = mid
else:
lb = mid + 1
if lb <= h:
print(lb)
else:
pt1, ans1 = h * (h - 1) // 2, h - 1
pt2 = n - pt1
l, r = 0, pt2 // h + (pt2 % h != 0)
while l < r:
mid = l + r >> 1
if g(mid, h) > pt2:
r = mid
elif g(mid, h) == pt2:
r = l = mid
else:
l = mid + 1
print(ans1 + l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f(x):
global h
if x <= h:
return x * (x + 1) // 2
elif (x - h) % 2 == 0:
return (x - h) ** 2 // 4 + h * (x - h) + h * (h + 1) // 2
else:
return (x - h + 1) ** 2 // 4 - (x - h + 1) // 2 + h * (x - h) + h * (h + 1) // 2
n, h = map(int, input().split())
l = 0
r = n
while 1 < r - l:
m = (l + r) // 2
if n <= f(m):
r = m
else:
l = m
print(r)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def check(n, t):
su = t * (t + 1) // 2
if n >= su:
return 1
else:
return 0
def check2(n, ch, l):
sua = (ch + l) * (l - ch + 1) // 2
sub = l * (l - 1) // 2
if sua + sub > n:
return 0
else:
return 1
n, h = map(int, input().split())
l = 0
r = h
while r - l > 1:
mid = (l + r) // 2
if check(n, mid):
l = mid
else:
r = mid
ch = 0
if check(n, r):
ch = r
else:
ch = l
l = ch
r = 10000000000
while r - l > 1:
mid = (l + r) // 2
if check2(n, ch, mid):
l = mid
else:
r = mid
fans = 0
if check2(n, ch, r):
fans = r
else:
fans = l
ans = fans + fans - ch
sua = (ch + fans) * (fans - ch + 1) // 2
sub = fans * (fans - 1) // 2
ff = sua + sub
n = n - ff
ans += (n + fans - 1) // fans
print(ans)
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def func1(number, number2):
if number > 1414213569:
return 1000000000000000099
return (number + 1) * number // 2
def func2(number, number2):
if number > 1414213569:
return 1000000000000000099
return (number - number2) * (number + number2 - 1) // 2 + number * (number + 1) // 2
def solve(first, last, number, number2, func):
mid = (last + first) // 2
number3 = func(mid, number2)
while last >= first:
if number3 < number:
first = mid + 1
elif number3 > number:
last = mid - 1
else:
return mid
mid = (last + first) // 2
number3 = func(mid, number2)
number3 = func(first, number2)
if number3 == number:
return first
return last
n, h = map(int, input().split())
number = min(solve(2, 2000000001, n, 0, func1), h)
number2 = solve(2, 2000000001, n, number, func2)
number3 = (n - func2(number2, number)) / float(number2)
if number3 > int(number3):
print(number2 - number + number2 + int(number3) + 1)
else:
print(number2 - number + number2 + int(number3))
|
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
l = 1
r = h
while l < r:
m = (l + r + 1) // 2
if m * (m + 1) // 2 <= n:
l = m
else:
r = m - 1
ans = l
n -= l * (l + 1) // 2
if n > 0:
ans += 1
n -= l
if n > 0:
l += 1
lb = 0
rb = n // l
while lb < rb:
m = (lb + 1 + rb) // 2
if (2 * l + m - 1) * m < n:
lb = m
else:
rb = m - 1
ans += lb * 2
n -= (2 * l + lb - 1) * lb
if n > l + lb:
ans += 2
else:
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def solve(n, H):
def ligma(s):
return (s * s + s) // 2
if ligma(H) >= n:
lo, hi = 1, H
while lo < hi:
mid = (lo + hi) // 2
if ligma(mid) >= n:
hi = mid
else:
lo = mid + 1
return lo
def blocks(R):
k = 1 + (R - H) // 2
S = (R - H) * k - 2 * ligma(k - 1)
return ligma(R) - S
lo, hi = H, n
while lo < hi:
mid = (lo + hi) // 2
if blocks(mid) >= n:
hi = mid
else:
lo = mid + 1
return lo
n, H = map(int, input().split())
print(solve(n, H))
|
FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f(n):
return (n * n + n) // 2
def g(n):
if n % 2 == 0:
return 2 * f(n // 2)
else:
return f(n // 2 + 1) + f(n // 2)
n, h = (int(x) for x in input().split(" "))
if f(h) >= n:
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if f(mid) >= n:
hi = mid
else:
lo = mid + 1
print(hi)
else:
h = h - 1
n += f(h)
lo, hi = h, n
while lo < hi:
mid = (lo + hi) // 2
if g(mid) >= n:
hi = mid
else:
lo = mid + 1
print(hi - h)
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
mx = int((1 + 8 * n) ** 0.5)
while (mx + 1) * (mx + 1) <= 1 + 8 * n:
mx += 1
mx = (mx - 1) // 2
if mx <= H:
if (1 + mx) * mx // 2 < n:
mx += 1
print(mx)
exit()
else:
mx = H
n -= (1 + H) * H // 2
def f(moves, mx):
res = moves * mx
add = (moves - 1) // 2
res += (add + 1) * add
if moves % 2 == 0:
res += add + 1
return res
l = 1
r = 10**18
while l < r - 1:
m = (l + r) // 2
if f(m, mx) < n:
l = m
else:
r = m
if f(l, mx) >= n:
r = l
print(mx + r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def fun(t, h):
if t <= h:
return (t * t + t) // 2
if (t - h) % 2 == 1:
ret = h + (t - h) // 2
ans = (t - h) // 2 + 1
return ans * (h + ret) // 2 + ret * (ret + 1) // 2
ret = h + (t - h) // 2
ans = (t - h) // 2 + 1
return ans * (h + ret) // 2 + ret * (ret - 1) // 2
l = 1
r = 1000000000000000000
while l < r:
m = (l + r) // 2
if n > fun(m, h):
l = m + 1
else:
r = m
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def main():
n, H = map(int, input().split())
if (1 + H) * H // 2 >= n:
l = 1
r = H
while l < r:
h = (l + r) // 2 + 1
if (1 + h) * h // 2 > n:
r = h - 1
else:
l = h
h = r
rest = n - (1 + h) * h // 2
q, r = divmod(rest, h)
ans = h + q + (1 if r != 0 else 0)
else:
l = H
r = n
while l < r:
h = (l + r) // 2 + 1
if (H + h - 1) * (h - H) // 2 + (1 + h) * h // 2 > n:
r = h - 1
else:
l = h
h = r
rest = n - ((H + h - 1) * (h - H) // 2 + (1 + h) * h // 2)
q, r = divmod(rest, h)
ans = h - H + h + q + (1 if r != 0 else 0)
print(ans)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def check(n, h, t):
if t < h:
return n <= t * (t + 1) // 2
if n <= h * (h - 1) // 2:
return True
n -= h * (h - 1) // 2
t -= h - 1
if t % 2 == 0:
upper = t // 2 * (t // 2 + 1)
lower = (h - 1) * t
return n <= upper + lower
else:
upper = (t // 2 + 1) * (t // 2 + 1)
lower = (h - 1) * t
return n <= upper + lower
n, h = map(int, input().split())
lo, hi, ans = 1, n - 1, n
while lo <= hi:
mid = lo + hi >> 1
if check(n, h, mid):
ans = mid
hi = mid - 1
else:
lo = mid + 1
print(ans)
|
FUNC_DEF IF VAR VAR RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def f(x):
return (
x * (x + 1) // 2
if x < h
else h * (h + 1) // 2
+ (x - h) * h
+ (x - h) / 2 * ((x - h) / 2)
- 0.25 * ((x - h) % 2)
)
l = 1
r = n
while l < r:
mid = (l + r) // 2
if f(mid) >= n:
r = mid
else:
l = mid + 1
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = input().split(" ")
n = int(n)
h = int(h)
l = 1
r = 1000000000000000000
while l < r:
k = (l + r) // 2
u = 0
if k <= h:
u = k * (k + 1) // 2
else:
u = h * (h + 1) // 2
u += h * (k - h)
d = k - h
if d % 2 == 0:
u += d * d // 4
else:
d //= 2
u += d * (d + 1)
if u < n:
l = k + 1
else:
r = k
print(l)
|
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
from sys import stdin
def f(n):
return (n - 1) // 2 * (n - 1 - (n - 1) // 2)
def g(n):
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if f(mid) < n:
lo = mid + 1
else:
hi = mid
return lo
def ar(a, b, n):
return (a + b) * n // 2
def total(n, H):
if n < H:
return ar(n, 1, n)
return ar(n, n - H + 1, H) + f(n - H + 1)
def main():
for line in stdin.readlines():
n, H = [int(x) for x in line.split()]
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if total(mid, H) < n:
lo = mid + 1
else:
hi = mid
print(lo)
main()
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
import sys
n, H = map(int, sys.stdin.readline().split())
def maximum_quantity(k):
if k >= H:
c = (k - H) // 2
return H * (c + 1) + c * (c + 1) // 2 + (k - c) * (k - c - 1) // 2
else:
return k * (k + 1) // 2
k_inf = 0
k_sup = n
while k_sup - k_inf > 1:
k = (k_inf + k_sup) // 2
if maximum_quantity(k) >= n:
k_sup = k
else:
k_inf = k
print(k_sup)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
N, H = map(int, input().split())
ans = N
ok = 1
ng = H + 1
while ng - ok > 1:
md = (ok + ng) // 2
K = md * (md + 1) // 2
if K <= N:
L = N - K
X = L // md
if L % md != 0:
X += 1
if ans > md + X:
ans = md + X
ok = md
else:
ng = md
if H * (H + 1) // 2 > N:
print(ans)
exit(0)
ok = H
ng = N
while ng - ok > 1:
md = (ok + ng) // 2
K = md * (md + 1) // 2 + md * (md - 1) // 2 - H * (H - 1) // 2
if K <= N:
L = N - K
X = md + md - H + L // md
if L % md != 0:
X += 1
if ans > X:
ans = X
ok = md
else:
ng = md
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
left, right = 0, n
limit = n + (H - 1) * H // 2
def check(t):
if t <= H:
return t * (t + 1) // 2 >= n
if (t + H - 1) % 2:
return (t + H) ** 2 // 4 >= limit
else:
return ((t + H) ** 2 - 1) // 4 >= limit
while True:
if right - left <= 1:
break
tmp = (left + right) // 2
if check(tmp):
right = tmp
else:
left = tmp
print(right)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR WHILE NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
import sys
[n, H] = map(int, sys.stdin.read().split())
INF = 1000000000000000000000000000000
def tri(n):
return n * (n + 1) // 2
def build(n, h):
vol = tri(h)
if h > H:
r = h - H
l = h - 1 - r
sys.stderr.write("BUILD {} : {} {}\n".format(h, l, r))
sys.stderr.write("LSIDE: {}\n".format(tri(l + r) - tri(l)))
vol += tri(l + r) - tri(l)
w = h + r
else:
w = h
if vol > n:
sys.stderr.write("BUILD {} -> NO WAY\n".format(h))
return INF
n -= vol
w += n // h
if n % h > 0:
w += 1
sys.stderr.write("BUILD {} -> {}\n".format(h, w))
return w
l = 1
r = n
while l + 4 < r:
sys.stderr.write("RANGE {}..{}\n".format(l, r))
a = (l + l + r + 2) // 3
b = (l + r + r + 2) // 3
ay = build(n, a)
by = build(n, b)
if by == INF:
r = b
continue
if ay < by:
r = b
else:
l = a
ans = INF
for i in range(l, r + 1):
ans = min(ans, build(n, i))
print(ans)
|
IMPORT ASSIGN LIST VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def g(d):
if d < h:
return d * (d + 1) // 2
d = d - h + 1
dd = d // 2
p = 2 * ((h - 1) * dd + dd * (dd + 1) // 2)
if d % 2 == 1:
p += h - 1 + dd + 1
return (h - 1) * (h - 1 + 1) // 2 + p
a = 0
b = 10**20
while a != b:
c = (a + b) // 2
if n <= g(c):
b = c
else:
a = c + 1
print(a)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = list(map(int, input().split()))
left = 1
right = n
mid = 0
while right > left:
mid = (left + right) // 2
maxnum = 0
if mid <= h:
maxnum = (1 + mid) * mid // 2
else:
maxnum = (mid + mid - h + 2) * (h - 1) // 2
tmplen = mid - h + 1
if tmplen % 2 == 0:
high = tmplen // 2
maxnum += (tmplen + 2) * high // 2
else:
high = (tmplen + 1) // 2
maxnum += (tmplen + 1) * high // 2
if maxnum >= n:
right = mid
else:
left = mid + 1
print(right)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def check(x):
k = min(x, h)
return x * x - (k * (k - 1) >> 1) >= n
def get(x):
global n
k = min(x, h)
q = x * x - (k * (k - 1) >> 1)
rest = 2 * x - k
n -= q
return rest + (n + x - 1) // x
l, r = 0, int(1e18)
while r - l > 1:
m = l + r >> 1
if check(m):
r = m
else:
l = m
print(get(r))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
N, H = list(map(int, input().split()))
ok = N
ng = 0
while ok - ng > 1:
mid = (ok + ng) // 2
if mid <= H:
sum = mid * (mid + 1) // 2
if sum < N:
ng = mid
else:
ok = mid
else:
sum = H * (H + 1) // 2
rest = mid - H
sum += rest * H
if rest % 2 == 1:
height = (rest - 1) // 2
add = height * (height + 1) // 2
sum += add * 2
else:
bottom = rest // 2
add = bottom * (bottom + 1) // 2
sum += add * 2
sum -= bottom
if sum < N:
ng = mid
else:
ok = mid
print(ok)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def sum(a, b):
if a > b:
return sum(b, a)
n = b - a + 1
return (a + b) * n // 2
[n, h] = [int(x) for x in input().split()]
l = 0
r = n
while l + 1 < r:
m = (l + r) // 2
mx = 0
if m < h:
mx = sum(1, m)
elif m % 2 == h % 2:
hm = (h + m) // 2
mx = sum(h, hm) + sum(1, hm - 1)
else:
hm = (h + m) // 2
mx = sum(h, hm) + sum(1, hm)
if mx >= n:
r = m
else:
l = m
print(r)
|
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def parser():
while 1:
data = list(input().split(" "))
for number in data:
if len(number) > 0:
yield number
input_parser = parser()
def get_word():
global input_parser
return next(input_parser)
def get_number():
data = get_word()
try:
return int(data)
except ValueError:
return float(data)
N = get_number()
H = get_number()
if (1 + H) * H // 2 < N:
L = H - 1
R = 100000000000000000000000000
while L + 1 < R:
mid = (L + R) // 2
if (1 + mid) * mid // 2 + (H + mid - 1) * (mid - 1 - H + 1) // 2 >= N:
R = mid
else:
L = mid
ans = R + R - H
L = H - 1
R = 100000000000000000000000000
while L + 1 < R:
mid = (L + R) // 2
if (1 + mid) * mid // 2 + (H + mid) * (mid - H + 1) // 2 >= N:
R = mid
else:
L = mid
if R + R - H + 1 < ans:
ans = R + R - H + 1
else:
L = 0
R = H
while L + 1 < R:
mid = (L + R) // 2
if (1 + mid) * mid // 2 >= N:
R = mid
else:
L = mid
ans = R
print(ans)
|
FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
numbers = list(map(int, input().split()))
n = numbers[0]
H = numbers[1]
l = 0
r = n
while l + 1 < r:
mid = (l + r) // 2
if mid <= H:
if mid * (mid + 1) >= n * 2:
r = mid
else:
l = mid
continue
x = (mid + H) // 2
pos = mid - x + 1
val = (H + x - 1) * (pos - 1) // 2 + x * (x + 1) // 2
if x - H + 1 < pos:
val = (H + x) * (pos - 1) // 2 + x * (x + 1) // 2
if val >= n:
r = mid
else:
l = mid
print(r)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def triang(n):
return n * (n + 1) // 2
def triang2(n):
if n % 2 == 0:
return n // 2 * (n + 2) // 2
return ((n + 1) // 2) ** 2
def satisfy(n, b, H):
return b <= 2 * n and n <= triang(b) - triang(max(0, b - H)) + triang2(
max(0, b - H - 1)
)
n, H = list(map(int, input().split()))
b_max = 2 * n
b = b_max
LN = 60
for i in reversed(range(LN + 1)):
if b - (1 << i) <= 0:
continue
if satisfy(n, b - (1 << i), H):
b -= 1 << i
print(b)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def UpperDiv(a, b):
if a < 0:
return 0
if a % b == 0:
return a // b
return a // b + 1
def GetAnswer(n, h, k):
h1 = min(h, k)
x = 2 * k - 1 - (h1 - 1)
y = k * k - h1 * (h1 - 1) // 2
return x + UpperDiv(n - y, k)
def Check(n, h, k):
h1 = min(h, k)
a = k * k
b = h1 * (h1 - 1) // 2
value = a - b
return value <= n
def BinarySearch(n, h):
left = 1
right = n
while right - left > 1:
middle = (left + right) // 2
if Check(n, h, middle):
left = middle
else:
right = middle
return left
n, h = map(int, input().split())
k = BinarySearch(n, h)
print(GetAnswer(n, h, k))
|
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
l, r = 0, n
while l + 1 < r:
p = (l + r) // 2
if p * (p + 1) // 2 >= n:
needToRemove, canRemove = p - h, p * (p + 1) // 2 - n
needToRemove = max(needToRemove, 0)
d = needToRemove // 2
canRemove -= d * (d + 1)
if needToRemove % 2:
canRemove -= needToRemove // 2 + 1
if canRemove >= 0:
r = p
else:
l = p
else:
l = p
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
T = input().split(" ")
n = int(T[0])
H = int(T[1])
a = 1
b = n
def f(m):
tot = 42
if m <= H:
tot = m * (m + 1) // 2
else:
tot = H * (H + 1) // 2
if (m - H) % 2 == 1:
mx = H + (m - H) // 2
tot += 2 * (mx * (mx + 1) // 2 - H * (H - 1) // 2)
tot -= H
else:
mx = H + (m - H) // 2
tot += 2 * (mx * (mx + 1) // 2 - H * (H - 1) // 2)
tot -= H
tot -= mx
return tot >= n
if n == 1:
print(1)
else:
m = (a + b) // 2
while b - a > 1:
if f(m):
b = m
else:
a = m
m = (a + b) // 2
print(b)
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
sum = (h + h * h) // 2
if sum >= n - h:
l = 0
r = h + 1
while l < r - 1:
m = (l + r) // 2
sum = (m + m * m) // 2
if sum <= n:
l = m
else:
r = m
sum = (l + l * l) // 2
ans = l
if sum < n:
ans += 1
print(ans)
else:
l = h
r = 100000000000000000000000000
while l < r - 1:
m = (l + r) // 2
sum1 = (m + m * m) // 2
sum2 = (m - h + 1) * (h + m) // 2
if sum1 + sum2 <= n:
l = m
else:
r = m
sum = (l + l * l) // 2 + (l - h + 1) * (h + l) // 2
ans = l + l - h + 1
n = n - sum
if n >= l + 1:
ans = ans + 1
n = n - (l + 1)
if n > 0:
ans = ans + 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = list(map(int, input().split()))
def f(m):
return ((h + m - 1) * max(m - h, 0) + m * (m + 1)) // 2
a, b = 1, n
while 1:
m = (a + b) // 2
t = f(m)
if t > n:
b = m
elif f(m + 1) <= n:
a = m
else:
break
l = max(m - h, 0) + m
l, t = l + (n - t) // m, t + (n - t) // m * m
if t < n:
l += 1
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, h = map(int, input().split())
def f(k, n, h):
r = 0
if k <= h:
r = k * (k + 1) // 2
else:
bb = k - h + 1
zz = bb // 2
r = h * (h + 1) // 2 - h + bb * h + zz * bb - zz * (zz + 1)
return r
l = 0
r = n
while l + 1 < r:
k = l + (r - l) // 2
cnt = f(k, n, h)
if cnt < n:
l = k
else:
r = k
if f(l, n, h) < n:
l += 1
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
def can(x):
if x <= H:
return n <= (1 + x) * x // 2
L = x + H - 1
h = L // 2
s = (1 + h) * h
if L % 2 == 1:
s += h + 1
s -= H * (H - 1) // 2
return n <= s
m, M = 0, int(1e18) + 5
while M - m > 1:
mid = m + (M - m) // 2
if can(mid):
M = mid
else:
m = mid
print(M)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def S(l):
return l * (l + 1) // 2
def f(l, h):
if l <= h:
return S(l)
s = S(h)
l -= h
s += l * h
s += S(l // 2) + S(l // 2 - (1 - l % 2))
return s
n, H = map(int, input().split())
l = 0
r = n + 1
while r - l > 1:
md = (l + r) // 2
if f(md, H) >= n:
r = md
else:
l = md
print(r)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = input().split()
n = int(n)
H = int(H)
def can(m):
global n, H
ans = 0
tt = min(m, H)
ans += (tt + 1) * tt // 2
ans += (m - tt) * H
ost = m - tt
if ost % 2 == 0:
up = ost // 2 - 1
ans += (up + 1) * up
ans += up + 1
else:
up = ost // 2
ans += (up + 1) * up
return ans
l = 0
r = n + H
while r - l > 1:
m = (l + r) // 2
if can(m) < n:
l = m
else:
r = m
print(r)
|
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
n, H = map(int, input().split())
def chek(x):
if x <= H:
if (x + 1) * x // 2 >= n:
return 1
else:
return 0
h = 0
h = h + (H + 1) * H // 2
p = x - H
h = h + p * H
p += 1
p -= 2
if p > 0:
if p % 2 == 1:
xx = p // 2 + 1
h = h + xx * xx
else:
xx = p // 2
h = h + xx * (xx + 1)
if h >= n:
return 1
return 0
l = 1
r = n
while l + 1 < r:
m = (l + r) // 2
if chek(m) == 1:
r = m
else:
l = m
if chek(l) == 1:
r = l
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle: h_1 β€ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| β€ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
-----Input-----
The only line contains two integer numbers n and H (1 β€ n, H β€ 10^18) β the number of sand packs you have and the height of the fence, respectively.
-----Output-----
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
-----Examples-----
Input
5 2
Output
3
Input
6 8
Output
3
-----Note-----
Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]
|
def f():
if x <= m:
return x * (x + 1) // 2
t = x + m
return t // 2 * (t - t // 2) - m * (m - 1) // 2
n, m = map(int, input().split())
lo, hi = 0, n
while lo + 1 < hi:
x = (lo + hi) // 2
if f() < n:
lo = x
else:
hi = x
print(hi)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.