zoukankan      html  css  js  c++  java
  • codeforces 762A. k-th divisor

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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.

    题意:求n的第k个约数。

    首先可以求得小于等于根号n的约数,如果k比较小的话直接输出即可

    而其他约数均为n/小于根号n的约数,所以数量为2倍。对于根号n为整数的再做个特判即可。

    时间复杂度O(sqrt(n));

    #include<cstdio>
    #include<vector>
    using namespace std;
    typedef long long ll;
    vector<int>x;
    int main()
    {
        ll n;
        int k;
        scanf("%lld%d",&n,&k);
        for(ll i=1;i*i<=n;++i)
            if(n%i==0) x.push_back(i);
        if(x.size()>=k) printf("%d",x[k-1]);
        else
        {
            int l=x.size(),sf=(ll)x[l-1]*x[l-1]==n;
            if(k>l*2-sf) printf("-1");
            else printf("%lld",n/(ll)x[2*l-k-sf]);
        }
        return 0;
    }
  • 相关阅读:
    洛谷P2045 K方格取数(算竞进阶习题)
    洛谷P2764 最小路径覆盖问题
    BZOJ 1051 受欢迎的牛
    BZOJ 4196 软件包管理器
    跨域知识(一)——CORS
    CSS 实现隐藏滚动条同时又可以滚动
    数组map用法总结
    js和Jquery获取选中select值和文本
    closest和parents方法区别
    CSS面试题总结2(转)
  • 原文地址:https://www.cnblogs.com/bzmd/p/6352254.html
Copyright © 2011-2022 走看看