-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6_489C.cpp
43 lines (41 loc) · 1.05 KB
/
day6_489C.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
/* we can create the number greedly, choose the min number we can add such that we can create number with m digits,
now for getting the max number we select the max digit we can such that it does not exceeds the sum.
*/
#include <bits/stdc++.h>
using namespace std;
string ans = "";
int n, m;
// checks wether we can create the number with length m, whose digits sum is k.
int check(int m, int k) {
return k >= 0 && 9 * m >= k;
}
int main() {
cin >> m >> n;
string res = "";
if(n == 0 && m > 1) cout << "-1 -1";
else if (9*m < n) cout << "-1 -1";
else {
string mn, mx;
int sum = n;
for(int i = 0; i < m; i ++)
for(int j = 0; j <= 9; j ++) {
if(check(m - i - 1, sum - j) && (i > 0 || j > 0 || (m == 1 && j == 0))) {
mn += to_string(j);
sum -= j;
break;
}
}
cout << mn << " ";
sum = n;
for(int i = 0; i < m; i ++)
for(int j = 9; j >= 0; j --) {
if(check(m - i - 1, sum - j) && (i > 0 || j > 0 || (m == 1 && j == 0))) {
mx += to_string(j);
sum -= j;
break;
}
}
cout << mx << endl;
}
return 0;
}