zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    小端大端
    位域
    c++ 2.1 编译器何时创建默认构造函数
    python 内置&&递归
    python返回值与局部全局变量
    python file
    python set
    python 字典的函数
    python FileError
    python pickle
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626686.html
Copyright © 2011-2022 走看看