您的位置:首页 > 其它

CodeForces - 762A k-th divisor

2017-02-02 22:40 246 查看
You are given two integers n and k. Find k-th smallest divisor of n, or report that it doesn't exist.

Divisor of n is any such natural number, that n can be divided by it without remainder.

Input

The first line contains two integers n and k (1 ≤ n ≤ 1015, 1 ≤ k ≤ 109).

Output

If n has less than k divisors, output -1.

Otherwise, output the k-th smallest divisor of n.

Example

Input
4 2


Output
2


Input
5 3


Output
-1


Input
12 5


Output
6


Note

In the first example, number 4 has three divisors: 1, 2 and 4. The second one is 2.

In the second example, number 5 has only two divisors: 1 and 5. The third divisor doesn't exist, so the answer is -1.

题目大意:给你两个数字n和k,然后问你n的因子里面第k个数是哪个

题目分析:正常来说,这道题目直接一个枚举就行了,但是这个题目给的数据尤其的大,这就要求需要一些其他的操作了,需要一个不定长的数组,所以要用vector,那个push_back,是往里面存储数据的,然后之后,因为O(n)的对于这道题目来说会直接超时,所以,我们把它缩减为O(sqrt(n)),也就是用两个数组去分别存储n的因子i和这个i对应的另外一个因子j,这样就缩减了很多的时间。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define pb push_back
#define LL long long
using namespace std;
vector<LL> a, b;
int main(){
LL k,t,n;
while((scanf("%I64d%I64d",&n,&k))!=EOF){
for(LL i=1;i*i<=n;i++){
if(n%i == 0){
if(i*i == n)
a.pb(i);
else {
a.pb(i);
b.pb(n/i);
}
}
}
if(b.size() + a.size() < k)
printf("-1\n");
else {
if(a.size() >=k)
printf("%I64d\n",a[k-1]);
else
printf("%I64d\n",b[b.size()-(k-a.size()-1)-1]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: