失踪人口回归 来一发水题表示我还活着
A. 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,让你求出 n 的第 k 个因子,如果不存在输出 -1。
考虑到n比较大(1e5),从头枚举肯定被T(也只有初学者会这样枚举吧),然后我们可以参考质数的判断,从1枚举到sqrt(n),就能得出所有的因子了
然后要注意特判一下i * i == n的情形
当然最优美的做法是直接一个set刚过去,又去重又排序
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main(int argc, char const *argv[])
{
LL n, m;
int li;
set<LL> S;
while(scanf("%lld%lld", &n, &m) == 2){
S.clear();
li = sqrt(n) + 0.5;
for(int i = 1; i <= li; i ++){
if(n % i == 0){
S.insert(i);
S.insert(n / i);
}
}
if(m > S.size()){
printf("-1\n");
}
else{
int cnt = 1;
for(set<LL>::iterator it = S.begin(); it != S.end(); it ++){
if(cnt == m){
printf("%lld\n", *it);
break;
}
else{
cnt ++;
}
}
}
}
return 0;
}
当然你也可以 数组 or vector + sort搞掉 不过要注意一点就是乱开内存会被MLE。。。。
#include <bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;
LL ma[31622880];
int main(int argc, char const *argv[])
{
LL n;
int m;
int li;
int cnt;
while(scanf("%lld%d", &n, &m) == 2){
cnt = 0;
li = sqrt(n) + 0.5;
for(int i = 1; i <= li; i ++){
if(n % i == 0){
if(1ll * i * i == n){
ma[cnt ++] = i;
}
else{
ma[cnt ++] = i;
ma[cnt ++] = n / i;
}
}
}
sort(ma, ma + cnt);
if(m > cnt){
printf("-1\n");
}
else{
printf("%lld\n", ma[m - 1]);
}
}
return 0;
}