|
| 1 | +package Permutation.P5568; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class Main { |
| 12 | + |
| 13 | + static int N, K; |
| 14 | + static int[] nums; |
| 15 | + static int[] cases = new int[4]; |
| 16 | + static boolean[] visited = new boolean[10]; |
| 17 | + static List<Integer> ans = new ArrayList<>(); |
| 18 | + |
| 19 | + public static void main(String[] args) throws Exception { |
| 20 | + System.setIn(new FileInputStream("src/Permutation/P5568/input.txt")); |
| 21 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 22 | + |
| 23 | + N = Integer.parseInt(br.readLine()); |
| 24 | + K = Integer.parseInt(br.readLine()); |
| 25 | + |
| 26 | + nums = new int[N]; |
| 27 | + for (int i = 0; i < N; i++) { |
| 28 | + nums[i] = Integer.parseInt(br.readLine()); |
| 29 | + } |
| 30 | + |
| 31 | + Arrays.sort(nums); |
| 32 | + dfs(0); |
| 33 | + |
| 34 | + Collections.sort(ans); |
| 35 | + int count = 1; |
| 36 | + for (int i = 1; i < ans.size(); i++) { |
| 37 | + if (!ans.get(i).equals(ans.get(i - 1))){ |
| 38 | + count ++; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + System.out.println(count); |
| 43 | + |
| 44 | + br.close(); |
| 45 | + } |
| 46 | + |
| 47 | + static void dfs(int count){ |
| 48 | + if (count == K) { |
| 49 | + ans.add(arrToNum(cases, K)); |
| 50 | + return; |
| 51 | + } |
| 52 | + int selected = 0; |
| 53 | + for (int i = 0; i < N; i++) { |
| 54 | + if (!visited[i] && nums[i] != selected) { |
| 55 | + visited[i] = true; |
| 56 | + cases[count] = nums[i]; |
| 57 | + dfs(count + 1); |
| 58 | + visited[i] = false; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static int arrToNum(int[] arr, int size){ |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + for (int i = 0; i < size; i++) { |
| 66 | + sb.append(arr[i]); |
| 67 | + } |
| 68 | + return Integer.parseInt(String.valueOf(sb)); |
| 69 | + } |
| 70 | +} |
0 commit comments