-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1561.cpp
59 lines (55 loc) · 1.12 KB
/
1561.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Created by kdongha on 2020/01/29.
//
#include <bits/stdc++.h>
int N, M;
int t[10000];
void solve() {
std::cin >> N >> M;
if (N <= M) {
std::cout << N;
return;
}
for (int i = 0; i < M; i++) {
std::cin >> t[i];
}
long long int left, right, result;
right = 1L * 30 * N;
left = 0;
while (left <= right) {
long long int mid = (left + right) / 2;
long long int sum = M;
for (int i = 0; i < M; i++) {
sum += mid / t[i];
if (sum >= N) {
break;
}
}
if (sum >= N) {
result=mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
int sum = M;
for (int i = 0; i < M; i++) {
sum += (result - 1) / t[i];
}
for (int i = 0; i < M; i++) {
if (left % t[i] == 0) {
sum++;
}
if (sum == N) {
std::cout << i + 1;
return;
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
solve();
return 0;
}