|
| 1 | +package Combination.P1256; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static int[][] combis = new int[202][202]; |
| 11 | + static int MAX = 1000000000; |
| 12 | + |
| 13 | + static int N, M, K; |
| 14 | + static int aCase, zCase; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | +// System.setIn(new FileInputStream("src/Combination/P1256/input.txt")); |
| 18 | + |
| 19 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 21 | + |
| 22 | + N = Integer.parseInt(st.nextToken()); |
| 23 | + M = Integer.parseInt(st.nextToken()); |
| 24 | + K = Integer.parseInt(st.nextToken()); |
| 25 | + |
| 26 | + if (K > comb(N + M, N)) { |
| 27 | + System.out.println(-1); |
| 28 | + } else { |
| 29 | + StringBuilder sb = new StringBuilder(); |
| 30 | + while (N + M > 0) { |
| 31 | + if (N == 0 || M == 0) { |
| 32 | + break; |
| 33 | + } |
| 34 | + aCase = comb(N + M - 1, N - 1); |
| 35 | + zCase = comb(N + M - 1, M - 1); |
| 36 | + if (K <= aCase) { |
| 37 | + sb.append("a"); |
| 38 | + N--; |
| 39 | + } else { |
| 40 | + sb.append("z"); |
| 41 | + M--; |
| 42 | + K -= aCase; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for (int i = 0; i < N; i++) { |
| 47 | + sb.append('a'); |
| 48 | + } |
| 49 | + for (int i = 0; i < M; i++) { |
| 50 | + sb.append('z'); |
| 51 | + } |
| 52 | + System.out.println(sb); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static int comb(int n, int k) { |
| 57 | + if (combis[n][k] != 0) |
| 58 | + return combis[n][k]; |
| 59 | + |
| 60 | + for (int i = 1; i <= n; i++) { |
| 61 | + for (int j = 0; j <= k; j++) { |
| 62 | + if (i == j || j == 0){ |
| 63 | + combis[i][j] = 1; |
| 64 | + } else { |
| 65 | + long result = combis[i-1][j-1] + combis[i-1][j]; |
| 66 | + if (result > MAX) |
| 67 | + combis[i][j] = MAX; |
| 68 | + else |
| 69 | + combis[i][j] = (int) result; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return combis[n][k]; |
| 75 | + } |
| 76 | +} |
0 commit comments