codeforces 762A. k-th divisor

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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比较小的话直接输出即可

而其他约数均为n/小于根号n的约数,所以数量为2倍。对于根号n为整数的再做个特判即可。

时间复杂度O(sqrt(n));

#include<cstdio>
#include<vector>
using namespace std;
typedef long long ll;
vector<int>x;
int main()
{
    ll n;
    int k;
    scanf("%lld%d",&n,&k);
    for(ll i=1;i*i<=n;++i)
        if(n%i==0) x.push_back(i);
    if(x.size()>=k) printf("%d",x[k-1]);
    else
    {
        int l=x.size(),sf=(ll)x[l-1]*x[l-1]==n;
        if(k>l*2-sf) printf("-1");
        else printf("%lld",n/(ll)x[2*l-k-sf]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/bzmd/p/6352254.html