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
Output
Input
Output
Input
Output
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;
}
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;
}
相关文章推荐
- Struts2——struts.xml常用配置
- SRM148_DIV2
- struts2上传文件(Common-FileUpload)
- 谈谈对象大小——从字节对齐到对象模型
- HTML+CSS的学习
- 我的年假2016-2017
- 洛谷 P1020 导弹拦截
- UVa1584 - Circular Sequence
- python3.x安装ipython notebook
- 快速沃尔什变化(FWT)介绍
- bzoj3123
- 现代企业的人力资源困局
- 9.1 套接字可选性和IO缓冲大小
- 线程通讯
- 蓝桥杯方格填数
- 强人工智能资料
- 笔试题8
- CABAC基于上下文的自适应二进制熵编码
- CAVLC基于上下文自适应的可变长编码
- Freebase Data Dump结构初探