【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;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 门牙牙齿蛀掉了怎么办 吃了辣的牙齿痛怎么办 牙颈部楔状缺损怎么办 牙齿磨了很疼怎么办 小孩牙有窟窿疼怎么办 西瓜吃多了尿不停怎么办 吃了个西瓜不停拉肚子怎么办 例假完了又来了怎么办 肾上面有个肿瘤怎么办 膀胱壁毛糙增厚怎么办 痔疮手术后尿不出来怎么办 小牛肚一天不尿怎么办 小孩拉肚一直不好怎么办 尿涨但是尿很少怎么办 十四岁尿血医生说是肾炎怎么办 吃肉反胃想吐怎么办 母牛排尿带血尿发烧怎么办 4岁发烧40度怎么办 狗狗拉肚子咳漱哮喘怎么办 拉肚子拉脱水人无力怎么办 孕中期拉稀拉水怎么办 吃坏肚子拉稀水怎么办 手上起小疙瘩疼怎么办 手上长东西很痛怎么办 七八十斤猪拉稀怎么办 宝宝扁桃体化脓反复发烧怎么办 骑单车后膝盖痛怎么办 孩子一运动就喘怎么办 小孩晚上咳嗽很厉害怎么办 1岁宝宝夜里咳嗽怎么办 咳嗽咳的胸疼怎么办 儿童又咳又喘怎么办 咳嗽咳到胸口痛怎么办 咳嗽咳得肋骨疼怎么办 孕妇咳嗽咳得胸口疼怎么办 怀孕偏左侧宫腔怎么办 晨起活动后咳嗽怎么办 运动后乳房坠痛怎么办 嗓子痒咳嗽怎么办夜间最为难受 小孩鼻炎引起的咳嗽怎么办 跑完800米喉咙痒怎么办