```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 ``` ```/* 　三角数, 四角数, 五角数, 六角数, 七角数, 八角数が全て表れる6つの巡回する4桁の数からなる 　唯一の順序集合の和を求めよ. */ #include #define BEGINTRI 45 #define ENDTRI 140 #define BEGINSQU 32 #define ENDSQU 99 #define BEGINPEN 26 #define ENDPEN 81 #define BEGINHEX 23 #define ENDHEX 70 #define BEGINHEP 21 #define ENDHEP 63 #define BEGINOCT 19 #define ENDOCT 58 int list[6][100][100]; int root; int triangular(int n) { return n * (n + 1) / 2; } int square(int n) { return n * n; } int pentagonal(int n) { return n * (3 * n - 1) / 2; } int hexagonal(int n) { return n * (2 * n - 1); } int heptagonal(int n) { return n * (5 * n - 3) / 2; } int octagonal(int n) { return n * (3 * n - 2); } void makeList(void) { int i; int twoUpperIdx[100] = { 0 }; int num; int twoUpper; for( i = BEGINSQU; i <= ENDSQU; i++ ) { num = square(i); if( num % 100 < 10 ) continue; twoUpper = num / 100; list[1][twoUpper][twoUpperIdx[twoUpper]] = num; twoUpperIdx[twoUpper]++; } for( i = 0; i < 100; i++ ) twoUpperIdx[i] = 0; for( i = BEGINPEN; i <= ENDPEN; i++ ) { num = pentagonal(i); if( num % 100 < 10 ) continue; twoUpper = num / 100; list[2][twoUpper][twoUpperIdx[twoUpper]] = num; twoUpperIdx[twoUpper]++; } for( i = 0; i < 100; i++ ) twoUpperIdx[i] = 0; for( i = BEGINHEX; i <= ENDHEX; i++ ) { num = hexagonal(i); if( num % 100 < 10 ) continue; twoUpper = num / 100; list[3][twoUpper][twoUpperIdx[twoUpper]] = num; twoUpperIdx[twoUpper]++; } for( i = 0; i < 100; i++ ) twoUpperIdx[i] = 0; for( i = BEGINHEP; i <= ENDHEP; i++ ) { num = heptagonal(i); if( num % 100 < 10 ) continue; twoUpper = num / 100; list[4][twoUpper][twoUpperIdx[twoUpper]] = num; twoUpperIdx[twoUpper]++; } for( i = 0; i < 100; i++ ) twoUpperIdx[i] = 0; for( i = BEGINOCT; i <= ENDOCT; i++ ) { num = octagonal(i); if( num % 100 < 10 ) continue; twoUpper = num / 100; list[5][twoUpper][twoUpperIdx[twoUpper]] = num; twoUpperIdx[twoUpper]++; } return; } int rec(int num, int check) { int i, j; int rep; if( check == 0x3f ) { if( num % 100 == root / 100 ) { return num; } else { return -1; } } for( i = 0; i <= 5; i++ ) { if( 1 << i & check ) continue; for( j = 0; list[i][num % 100][j] != 0; j++ ) { rep = rec(list[i][num % 100][j], check | 1 << i); if( rep != -1 ) return rep + num; } } return -1; } void problem61(void) { int i; int num; int rep; makeList(); for( i = BEGINTRI; i <= ENDTRI; i++ ) { num = triangular(i); root = num; rep = rec(num, 1); if( rep != -1 ) { printf("%d\n", rep); } } return; } int main(void) { problem61(); return 0; } ```
 ```1 ``` ```28684 ```