|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N, M, R; |
| 57 | + static long[][] dp; |
| 58 | + static boolean[][] vis; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + |
| 62 | + io = new IOController(); |
| 63 | + |
| 64 | + init(); |
| 65 | + solve(); |
| 66 | + |
| 67 | + io.close(); |
| 68 | + } |
| 69 | + |
| 70 | + public static void init() throws Exception { |
| 71 | + |
| 72 | + N = io.nextInt(); |
| 73 | + M = io.nextInt(); |
| 74 | + dp = new long[N][1<<N]; |
| 75 | + vis = new boolean[N][1<<N]; |
| 76 | + R = io.nextInt()-1; |
| 77 | + int state = 0, last = R; |
| 78 | + for(int i=1;i<M;i++) { |
| 79 | + last = io.nextInt()-1; |
| 80 | + state |= (1<<last); |
| 81 | + } |
| 82 | + dp[last][state] = 1; |
| 83 | + vis[last][state] = true; |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + static void solve() throws Exception { |
| 88 | + |
| 89 | + io.write(get(R,(1<<N)-1) + "\n"); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + static long get(int n, int k) { |
| 94 | + if(dp[n][k] != 0 || vis[n][k]) return dp[n][k]; |
| 95 | + vis[n][k] = true; |
| 96 | + int state = k | (1<<R); |
| 97 | + |
| 98 | + boolean can = false; |
| 99 | + boolean[] leftCan = new boolean[N]; |
| 100 | + for(int i=(n+N-1)%N,x=0;x<N-1;x++) { |
| 101 | + int min = Math.min(i,n), max = Math.max(i,n); |
| 102 | + if(min+N-1 == max || max-min == 1) leftCan[i] = false; |
| 103 | + else leftCan[i] = can; |
| 104 | + if(!can) { |
| 105 | + if((state & (1<<i)) != 0) can = true; |
| 106 | + } |
| 107 | + i = (i+N-1)%N; |
| 108 | + } |
| 109 | + |
| 110 | + can = false; |
| 111 | + boolean[] rightCan = new boolean[N]; |
| 112 | + for(int i=(n+1)%N,x=0;x<N-1;x++) { |
| 113 | + int min = Math.min(i,n), max = Math.max(i,n); |
| 114 | + if(min+N-1 == max || max-min == 1) rightCan[i] = false; |
| 115 | + else rightCan[i] = can; |
| 116 | + if(!can) { |
| 117 | + if((state & (1<<i)) != 0) can = true; |
| 118 | + } |
| 119 | + i = (i+1)%N; |
| 120 | + } |
| 121 | + |
| 122 | + for(int i=0;i<N;i++) if((k & (1<<i)) != 0 && leftCan[i] && rightCan[i]) { |
| 123 | + dp[n][k] += get(i, k^(1<<n)); |
| 124 | + } |
| 125 | + |
| 126 | + return dp[n][k]; |
| 127 | + } |
| 128 | + |
| 129 | +} |
| 130 | +``` |
0 commit comments