import java.util.BitSet; /* * プログラミングのお題スレ Part11 * //mevius.5ch.net/test/read.cgi/tech/1524570314/597 * 597 名前:デフォルトの名無しさん[sage] 投稿日:2018/07/14(土) 05:33:19.05 ID:jpR0mrXc * 国際数学オリンピック * 問3反パスカル的三角形 * //twitter.com/worapolketanond/status/1017612366840717312 */ class Q11_597 { { for(int i = 1; i <= 8; i++) solve(i); } static void solve(int n) { int max = n * n + n >> 1; int[][] table = new int[n][]; for (int i = 0; i < n; i++) table[i] = new int[i + 1]; LOOP: for (int pos = 0; pos >= 0;) { table[pos][0] = bitset.previousClearBit(table[pos][0] == 0 ? max : table[pos][0] - 1); if (table[pos][0] == 0) { pos--; for (int i = 0; i <= pos; i++) bitset.clear(table[pos][i]); continue; } bitset.set(table[pos][0]); for (int i = 0; i < pos; i++) { if (bitset.get(table[pos][i + 1])) { for (int j = 0; j <= i; j++) bitset.clear(table[pos][j]); continue LOOP; } bitset.set(table[pos][i + 1]); } pos++; if (pos == n) { StringBuilder sb = new StringBuilder(); for (int i = n - 1; i >= 0; i--) { sb.append('[').append(table[i][i]); for (int j = i + 1; j < n; j++) { sb.append(", ").append(table[j][i]); } sb.append("]\n"); } pos--; for (int i = 0; i <= pos; i++) bitset.clear(table[pos][i]); } } } }
Standard input is empty
N=1 [1] N=2 [1] [3, 2] [2] [3, 1] [1] [2, 3] [2] [1, 3] N=3 [1] [4, 3] [6, 2, 5] [2] [5, 3] [6, 1, 4] [3] [1, 4] [5, 6, 2] [1] [3, 4] [5, 2, 6] [3] [2, 5] [4, 6, 1] [2] [3, 5] [4, 1, 6] [3] [4, 1] [2, 6, 5] [3] [5, 2] [1, 6, 4] N=4 [4] [6, 2] [1, 7, 5] [9, 10, 3, 8] [4] [1, 5] [6, 7, 2] [9, 3, 10, 8] [4] [5, 1] [2, 7, 6] [8, 10, 3, 9] [3] [7, 4] [2, 9, 5] [8, 10, 1, 6] [4] [2, 6] [5, 7, 1] [8, 3, 10, 9] [3] [2, 5] [7, 9, 4] [8, 1, 10, 6] [3] [5, 2] [4, 9, 7] [6, 10, 1, 8] [3] [4, 7] [5, 9, 2] [6, 1, 10, 8] N=5 [5] [9, 4] [2, 11, 7] [10, 12, 1, 8] [13, 3, 15, 14, 6] [5] [4, 9] [7, 11, 2] [8, 1, 12, 10] [6, 14, 15, 3, 13] N=6 N=7 N=8