A. k-th divisor

https://codeforces.com/problemset/problem/762/A

This is a easy problem based on number theory.
We just simply iterate i from 1 to the value of sqrt(n), and check if n is divisble by the value of i and find all of its divisors, then sort them and get the answer.

void solve(){
    long long n;
    cin >> n;

    vector<long long> factors;
    for (long long i = 1; i * i <= n; ++i){
        if (n % i == 0){
            factors.emplace_back(i);
            if (i != n / i){
                factors.emplace_back(n / i);
            }
        }
    }

    sort(factors.begin(), factors.end());
    int k;
    cin >> k;

    cout << (k > factors.size() ? -1ll : factors[k - 1]) << '\n';
}

//what a easy problem..

posted @ 2024-03-08 21:55  _Yxc  阅读(9)  评论(0编辑  收藏  举报