vector、set 练习 k-th divisor

k-th divisor

 

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 ≤ 10151 ≤ 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
代码实现:
 1 #include<cstdio>
 2 #include <set>
 3 #include<vector>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 int main()
 9 {
10     set<long long int>a;
11     long long int n,k;
12     long long int p1,pp;
13     scanf("%lld%lld",&n,&k);
14     pp=sqrt(n);
15     for(int i=1; i<=pp; i++)
16     {
17         p1=n%i;
18         if(p1==0)
19         {
20             a.insert(n/i);
21             a.insert(i);
22         }
23     }
24     vector<long long int>v;
25     insert_iterator<vector<long long int> > in_it(v, v.begin());
26     copy(a.begin(), a.end(), in_it);
27     //printf("%d\n",v.size());
28     if(k>v.size())
29         printf("-1\n");
30 
31     else
32     {
33         printf("%lld\n", v[k-1]);
34     }
35 
36     return 0;
37 }

 

 

转载于:https://www.cnblogs.com/2016024291-/p/7043896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值