|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + |
| 20 | + static long[][] dp; |
| 21 | + static int N, K; |
| 22 | + static int[] len, A; |
| 23 | + static char[][] temp; |
| 24 | + static long[] pre; |
| 25 | + |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + |
| 28 | + ready(); |
| 29 | + solve(); |
| 30 | + |
| 31 | + bwEnd(); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + static void ready() throws Exception{ |
| 36 | + |
| 37 | + N = Integer.parseInt(br.readLine()); |
| 38 | + temp = new char[N][]; |
| 39 | + for(int i=0;i<N;i++) temp[i] = br.readLine().toCharArray(); |
| 40 | + K = Integer.parseInt(br.readLine()); |
| 41 | + dp = new long[K][1<<N]; |
| 42 | + len = new int[N]; |
| 43 | + A = new int[N]; |
| 44 | + pre = new long[16]; |
| 45 | + pre[1] = 10; |
| 46 | + for(int i=2;i<16;i++) pre[i] = pre[i-1] * 10; |
| 47 | + |
| 48 | + for(int i=0;i<N;i++) { |
| 49 | + len[i] = temp[i].length; |
| 50 | + int res = 0; |
| 51 | + for(char c : temp[i]) { |
| 52 | + res = (res * 10 + (c-'0')) % K; |
| 53 | + } |
| 54 | + A[i] = res; |
| 55 | + dp[res][1<<i] = 1; |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + static void solve() throws Exception{ |
| 61 | + |
| 62 | + for(int i=1;i<(1<<N);i++) { |
| 63 | + for(int k=0;k<K;k++) { |
| 64 | + for(int x=0;x<N;x++) if((i & (1<<x)) == 0) { |
| 65 | + long tmp = k; |
| 66 | + int cnt = len[x]; |
| 67 | + while(cnt > 0) { |
| 68 | + int diff = Math.min(15, cnt); |
| 69 | + tmp = (tmp * pre[diff]) % K; |
| 70 | + cnt-=diff; |
| 71 | + } |
| 72 | + int res = ((int)tmp + A[x]) % K; |
| 73 | + dp[res][i|(1<<x)] += dp[k][i]; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + long ans = dp[0][(1<<N)-1], total = 1; |
| 79 | + for(long i=2;i<=N;i++) total *= i; |
| 80 | + |
| 81 | + if(ans == 0) total = 1; |
| 82 | + else { |
| 83 | + long[] P = {2,3,5,7,11,13}; |
| 84 | + for(long i:P) { |
| 85 | + while(ans%i==0 && total%i==0) { |
| 86 | + ans/=i; |
| 87 | + total/=i; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + bw.write(ans+"/"+total); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +``` |
0 commit comments