【codeforces 762A】k-th divisor
来源:互联网 发布:淘宝零食店铺推荐知乎 编辑:程序博客网 时间:2024/05/21 10:58
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_backusing 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
- codeforces 762A k-th divisor
- CodeForces - 762A k-th divisor (数学
- Codeforces 762A k-th divisor
- Codeforces 762A-k-th divisor
- CodeForces - 762A k-th divisor
- CodeForces 762A k-th divisor
- 762A k-th divisor
- codeforces-762A k-th divisor(数学题)
- k-th divisor CodeForces
- k-th divisor CodeForces
- Educational Codeforces Round 17-A k-th divisor
- codefoces A. k-th divisor
- Codeforces 762 A.k-th divisor (简单有关因子的问题)
- A. k-th divisor(水题,数论,sqrt运用)
- A. k-th divisor-就问你敢开多大的数组
- codeforces A. k-Factorization
- nginx静态资源缓存配置
- 332. Reconstruct Itinerary**
- 三星Note7燃损原因水落石出创新与检查双管齐下保障未来产品安全
- 卡特兰数定义及其几个常用的应用(附例子"括号匹配"代码)+应用总结
- 实验吧 Noisy By Assassin
- 【codeforces 762A】k-th divisor
- UESTC 8 God Only Knows! AC自动机
- python题目-----type特殊用法
- 《玲音》
- CodeForces - 4C
- Python零基础入门二十九之Tkinter隐藏技能
- 我能想到的最浪漫的人生。
- 慕课网自学-AngularJS实战
- 清华大学 约数的个数