⚡백준 1300 K번째 수
풀이
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
ull n, k;
bool decision(int x) {
ull cnt = 0;
for (ull i = 1; i <= n; ++i)
cnt += min(x / i, n);
return cnt >= k;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
ull hi = 100000 * 100000;
ull lo = 1;
for (int it = 0; it < 100; ++it) {
double mid = (hi + lo) / 2;
if (decision(mid)) hi = mid;
else lo = mid;
}
cout << hi;
return 0;
}
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll N;
ll k;
int check(ll x) {
ll cnt = 0;
for (ll i = 1; i <= N; ++i)
cnt += min(x / i, N);
return cnt >= k;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N;
cin >> k;
ll high = min((ll)10000000000, N*N);
ll low = 1;
while (low < high) {
ll mid = (high + low) / 2;
if (check(mid)) high = mid;
else low = mid + 1;
}
cout << high;
return 0;
}
틀린 풀이
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n, k;
ll countPair[100001] = { 0 };
void getCountPair() {
countPair[0] = 0;
for (int i = 1; i <= n; ++i)
countPair[i] = countPair[i - 1] + 1 + 2 * (n - i);
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
getCountPair();
int i = 1;
while (true) {
if (k > countPair[i]) ++i;
else break;
}
ll kk = countPair[i - 1];
if (++kk == k) {
cout << i * i;
return 0;
}
int j = i;
while (kk < k) {
j++;
kk += 2;
}
cout << i * j;
return 0;
}
100000
1000000000
output : 352629984
answer : 204535821
📌참고자료