fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. int n, k;
  8. cin >> n >> k;
  9. int count = 0;
  10.  
  11. // Iterate from 1 to sqrt(n)
  12. for (int i = 1; i * i <= n; i++) {
  13. if (n % i == 0) {
  14. count++; // Found a divisor
  15. if (count == k) {
  16. cout << i << "\n"; // Print the k-th smallest divisor
  17. return 0;
  18. }
  19.  
  20. // Check the paired divisor n/i, only if it's different
  21. if (i != n / i) {
  22. count++;
  23. if (count == k) {
  24. cout << n / i << "\n"; // Print the paired divisor
  25. return 0;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. // If the k-th divisor doesn't exist
  32. cout << -1 << "\n";
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
-1