给定整数k和单调递增序列:
f(n)= an + bn [log2(n)] + cn ^ 3其中( a = 1,2,3,…),( b = 1,2,3,…),( c = 0,1, 2、3,…)
在这里,[log 2 (n)]表示将日志取至基数2并将该值四舍五入。
如果n = 1,则值为0。
如果n = 2-3,则值为1。
如果n = 4-7,则值为2。
如果n = 8-15,则值为3。
任务是找到n使得f(n)= k ,如果k不属于序列,则打印0 。
注意:值以这样一种方式可以以64位表示,并且三个整数a,b和c不超过100。
例子:
Input: a = 2, b = 1, c = 1, k = 12168587437017
Output: 23001
f(23001) = 12168587437017
Input: a = 7, b = 3, c = 0, k = 119753085330
Output: 1234567890
天真的方法:给定a,b,c的值,为每个n值找到f(n)的值并将其进行比较。
高效方法:使用二元搜索,选择n =(min + max)/ 2 ,其中min和max是n可能的最小值和最大值,然后,
- 如果f(n)
则增加n 。 - 如果f(n)> k,则递减n 。
- 如果f(n)= k,则n是必需的答案。
- 重复上述步骤,直到找到所需的值或无法按顺序进行。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
#define SMALL_N 1000000
#define LARGE_N 1000000000000000
using namespace std;
// Function to return the value of f(n) for given values of a, b, c, n
long long func(long long a, long long b, long long c, long long n)
{
long long res = a * n;
long long logVlaue = floor(log2(n));
res += b * n * logVlaue;
res += c * (n * n * n);
return res;
}
long long getPositionInSeries(long long a, long long b,
long long c, long long k)
{
long long start = 1, end = SMALL_N;
// if c is 0, then value of n can be in order of 10^15.
// if c!=0, then n^3 value has to be in order of 10^18
// so maximum value of n can be 10^6.
if (c == 0) {
end = LARGE_N;
}
long long ans = 0;
// for efficient searching, use binary search.
while (start <= end) {
long long mid = (start + end) / 2;
long long val = func(a, b, c, mid);
if (val == k) {
ans = mid;
break;
}
else if (val > k) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
return ans;
}
// Driver code
int main()
{
long long a = 2, b = 1, c = 1;
long long k = 12168587437017;
cout << getPositionInSeries(a, b, c, k);
return 0;
}
Python3
# Python 3 implementation of the approach
from math import log2, floor
SMALL_N = 1000000
LARGE_N = 1000000000000000
# Function to return the value of f(n)
# for given values of a, b, c, n
def func(a, b, c, n) :
res = a * n
logVlaue = floor(log2(n))
res += b * n * logVlaue
res += c * (n * n * n)
return res
def getPositionInSeries(a, b, c, k) :
start = 1
end = SMALL_N
# if c is 0, then value of n
# can be in order of 10^15.
# if c!=0, then n^3 value has
# to be in order of 10^18
# so maximum value of n can be 10^6.
if (c == 0) :
end = LARGE_N
ans = 0
# for efficient searching,
# use binary search.
while (start <= end) :
mid = (start + end) // 2
val = func(a, b, c, mid)
if (val == k) :
ans = mid
break
elif (val > k) :
end = mid - 1
else :
start = mid + 1
return ans;
# Driver code
if __name__ == "__main__" :
a = 2
b = 1
c = 1
k = 12168587437017
print(getPositionInSeries(a, b, c, k))
# This code is contributed by Ryuga
PHP
$k)
$end = $mid - 1;
else
$start = $mid + 1;
}
return $ans;
}
// Driver code
$a = 2;
$b = 1;
$c = 1;
$k = 12168587437017;
print(getPositionInSeries($a, $b, $c, $k));
// This code is contributed by mits
?>
输出:
23001