Codeforces 762A k-th divisor(数论)
题目链接:k-th divisor
求出N的第K大因子,满足N <= 10^15,K <= 10^9
直接暴力……
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 #define LL long long 7 8 LL n, k, h, ans; 9 int num; 10 11 int main(){ 12 13 scanf("%lld%lld", &n, &k); 14 h = (LL)sqrt(n + 0.5); 15 if (h * h == n){ 16 rep(i, 1, h - 1) if (n % i == 0) num += 2; 17 ++num; 18 } 19 else{ rep(i, 1, h) if (n % i == 0) num += 2; } 20 21 if (num < k){ puts("-1"); return 0; } 22 23 if (h * h == n){ 24 int cnt = 0; 25 if (k <= num / 2){ 26 rep(i, 1, h) if (n % i == 0){ 27 ++cnt; 28 if (cnt == k){ 29 ans = i; 30 break; 31 } 32 } 33 } 34 35 else if (k == num / 2 + 1){ 36 printf("%lld\n", h); 37 return 0; 38 } 39 40 else{ 41 int m = k - num / 2 - 1; 42 int ret = num / 2 - m + 1; 43 rep(i, 1, h) if (n % i == 0){ 44 ++cnt; 45 if (cnt == ret){ 46 ans = n / i; 47 break; 48 } 49 } 50 } 51 } 52 53 54 55 else 56 { 57 int cnt = 0; 58 if (k <= num / 2){ 59 rep(i, 1, h) if (n % i == 0){ 60 ++cnt; 61 if (cnt == k){ 62 ans = i; 63 break; 64 } 65 } 66 } 67 68 else{ 69 int m = k - num / 2, cnt = 0; 70 int ret = num / 2 - m + 1; 71 rep(i, 1, h) if (n % i == 0){ 72 ++cnt; 73 if (cnt == ret){ 74 ans = n / i; 75 break; 76 } 77 } 78 } 79 } 80 81 printf("%lld\n", ans); 82 return 0; 83 84 }