【codeforces 762A】k-th divisor
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;
}
【codeforces 762A】k-th divisor的更多相关文章
- 【Codeforces 762A】 k-th divisor
[题目链接] 点击打开链接 [算法] 我们知道,一个数的因子是成对出现的,一半小于等于sqrt(N),一半大于sqrt(N),因此,我们可以从 2..sqrt(N)枚举因子 [代码] #include ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 757B】 Bash's Big Day
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
- 【codeforces 797E】Array Queries
[题目链接]:http://codeforces.com/problemset/problem/797/E [题意] 给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问 ...
- 【codeforces 801A】Vicious Keyboard
[题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...
随机推荐
- [1,2,3].forEach(alert);这样的写法有什么利和弊吗?
以下这个问题遇到了之后.问了太阳神,以下是太阳神的解答: [1,2,3].forEach(alert);这样的写法有什么利和弊吗? 首先forEach使用方法非常easy降低代码量, 可是也有非常多地 ...
- js数组遍历和对象遍历小结
数组的遍历 for循环 for(var j = 0,len = arr.length; j < len; j++){ console.log(arr[j]); } forEach,性能比for还 ...
- document.write的注意点
如果给button点击事件添加document.write会消除页面所有元素,包括button按钮 <!DOCTYPE html> <html> <head> &l ...
- Nginx分发服务
nginx配置分发tomcat服务 http://blog.csdn.net/yan_chou/article/details/53265775 http://www.cnblogs.com/deng ...
- gvim不能直接打开360压缩打开的文件
1. 压缩文件a.rar 2. 默认使用360压缩打开 3.用gvim打开对应的a.c文件,提示permission denied 4.用gvim跟踪目录,发现360管理的缓冲目录无法打开 原因未分析 ...
- Android自己定义View画图实现拖影动画
前几天在"Android画图之渐隐动画"一文中通过画线实现了渐隐动画,但里面有个问题,画笔较粗(大于1)时线段之间会有裂隙.我又改进了一下.这次效果好多了. 先看效果吧: 然后我们 ...
- 简介及环境搭建跑通Hello
简介及环境搭建跑通Hello Spring Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.是为了解决企业应用程序开 ...
- 介绍linux设备驱动编程
目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer): 主要利用C库函数和Linux API进行应用 ...
- 宝塔 ftp 不能连接 列出时出错
摘抄自 https://www.bt.cn/bbs/forum.php?mod=viewthread&tid=1903&page=1&extra=#pid6515 1.注意内网 ...
- (转)SQL Server 2012笔记分享-25:配置备份维护计划
本文转自http://543925535.blog.51cto.com/639838/1427529 在日常的SQL维护中,有很多需要重复周期性去做的工作我们不太可能去手动操作完成,比如备份作业.重建 ...