设函数g(x)= x的除数.给定两个整数a和b,我们需要找到 - >
G(A)+ G(A + 1)... +克(b)中.
我想这一步 - >
for every x from a to b
sum+=number of divisor of x(in sqrt(x) complexity)
Run Code Online (Sandbox Code Playgroud)
但是给定1 <= a <= b <= 2 ^ 31-1
因此,在a和b之间进行迭代会花费我很多时间....例如 - >如果a = 1且b = 2 ^ 31-1.
有更好的方法吗?
给定一个字符串A和另一个字符串B.查找B的任何排列是否作为A的子字符串存在.
例如,
如果A ="百科全书"
如果B ="dep"则返回true,因为ped是dep的排列,ped是A的子串.
My solution->
if length(A)=n and length(B)=m
I did this in 0((n-m+1)*m) by sorting B and then checking A
with window size of m each time.
Run Code Online (Sandbox Code Playgroud)
我需要找到一个更好,更快的解决方案.
我知道我们需要包括一些比较功能才能实现此目的。
但无法为此写。
例如:
向量的元素={(2,4),(4,2),(5,1),(5,3)}
找到= 5
lower_bound()应该返回2
代码->
#define pp pair<int,int>
bool cmp(const pp &l,const pp &r) {
return l.first < r.first;
}
int main() {
vector<pp> v;
sort(v.begin(), v.end(), cmp);
int id=(int)(lower_bound(v.begin(), v.end(), ??) - v.begin());
}
Run Code Online (Sandbox Code Playgroud) algorithm ×2
c++ ×1
lower-bound ×1
numbers ×1
permutation ×1
std-pair ×1
string ×1
substring ×1
vector ×1