【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
- 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 768C - Jon Snow and his Favourite Number 机智题
- CodeForces 292D Connected Components(并查集 前后缀)
- Codeforces-592C - The Big Race - 思维
- codeforces C#264. Gargari and Bishops
- CodeForces 602C__The Two Routes
- CODEFORCES 445B DZY Loves Chemistry
- codeforces 746 A.Compote【水题】
- codeforces 709D Recover the String (构造+模拟)
- codeforces 339D 简单的线段树操作
- 2018.2.4【 CodeForces - 831A 】解题报告(模拟)
- Codeforces 463C Gargari and Bishops 题解
- Codeforces 601B Lipshitz Sequence(高效)
- Codeforces 696A. Lorenzo Von Matterhorn(map离散化)