您的位置:首页 > 其它

【codeforces 762A】k-th divisor

2017-10-04 18:45 239 查看

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

【题目链接】:http://codeforces.com/contest/762/problem/A

【题解】

可以只枚举sqrt(n);
因子是成对出现的;
所以i是n的因子
n/i也是n的因子;
注意i*i=n的情况就好;
对于小于sqrt(n)的放在v里面,大于sqrt(n)的放在vv里面;
v是升序的,vv是降序的;因为n/i=x (i< sqrt(n),则x>sqrt(n))
然后根据k和两个v的size的关系.控制输出就好;
(一个数的因子不会那么多的,就算1e15,也没超过1000个因子)

【完整代码】

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;

LL n,k,cnt = 0,temp;
vector <LL> v,vv;

int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> k;
LL t = sqrt(n);
for (LL i = 1;i <= t-1;i++)
if (n%i==0)
{
v.pb(i);
vv.pb(n/i);
}
if (t*t==n)
v.pb(t);
else
if (n%t==0)
{
v.pb(t);
vv.pb(n/t);
}
int len1 = v.size(),len2 = vv.size();
int cnt = len1+len2;
if (cnt<k)
puts("-1");
else
{
if (k<=len1)
cout << v[k-1] << endl;
else
cout << vv[len2-1-(k-len1)+1]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: