#include <stdio.h> #include <map> using namespace std; int edge1[128]; int edge2[128]; map<int, int> ways; void print(bool deb = false) { int n = 0; for (int i = 0; i <= 100; i++) if (edge1[i] != -1 || edge2[i] != -1) n = i; n = 100; //{ //n++; //if (n > 102) break; //} //n++; printf("%d\n", n); for (int i = 0; i < 100; i++) { int e1 = edge1[i] == -1 ? -1 : edge1[i] + (deb?0:1); int e2 = edge2[i] == -1 ? -1 : edge2[i] + (deb?0:1); if (deb) printf("%d: %d %d\n", i +(deb?0:1), e1, e2); else printf("%d %d\n", e1, e2); } } int count(int s, int e) { if (s == 0) ways.clear(); if (s == -1) return 0; if (s == e) return 1; if (ways[s] != 0) return ways[s]; int res = count(edge1[s],e) + count(edge2[s],e); ways[s] = res; return res; } void cls() { for (int i = 0; i <= 100; i++) { edge1[i] = edge2[i] = -1; } edge1[0] = 1; } int gen2(int s,int m) { //cls(); edge1[s] = 1+s; edge2[s] = 2+s; int e = 2 * m + 1 + s; for (int i = 0; i< m; i++) { if (i<m-1) { edge1[2 * i + 1 + s] = 2 * i + 1 + s + 2; edge2[2 * i + 1 + s] = 2 * i + 2 + s + 2; edge1[2 * i + 2 + s] = 2 * i + 1 + s + 2; edge2[2 * i + 2 + s] = 2 * i + 2 + s + 2; } else { edge1[2 * i + 1 + s] = e; edge2[2 * i + 2 + s] = e; } } //edge2[e - 1] = -1; //edge2[s] = 2; return e; } void testpow() { for (int y = 2; y < 31; y++) { int e = gen2(0, y); printf("numer of roads2 %d = %d \n", y, count(0, e)); } } int gen_binary(int n) { cls(); int leaves = 1; int level = 0; while (leaves < n) { leaves <<= 1; level++; } int n_total = leaves + n; for (int i = 1; i <= n_total; i++) { if (i < leaves) { const int ia = i * 2 < n_total ? i * 2 -1: -1; const int ib = i * 2 + 1 < n_total ? i * 2 + 1 -1: -1; edge1[i - 1] = ia; edge2[i - 1] = ib; } else { if (i!= n_total) { edge1[i - 1] = -1; edge2[i - 1] = -1; } } } return n_total-1; } void solve(int n) { cls(); gen_binary(16); int e = gen2(32, 30); edge1[90] = 99; edge2[91] = 99; int nn = n; int pos = 0; int level = 0; while (nn > 0) { if ((nn % 2) == 1) { if (pos % 2 == 0) edge1[16 + pos / 2] = 90 - 2 * level; else edge2[16 + pos / 2] = 90 - 2 * level; } level++; nn >>= 1; pos++; } } void test() { for (int ii = 1; ii < 1000000; ii++) { int i = rand() % 1000000000; solve(i); int res = count(0, 99); if (i % 10000 == 0) printf("%d\n", i); if (res != i) { printf("test :%3d =%d ", i, res); printf("FAILED!!!!!\n"); return; } //else printf("passed\n"); } } int main() { //test(); int n; scanf("%d\n", &n); solve(n); print(false); return 0; }
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #include <stdio.h> #include <map> using namespace std; int edge1[128]; int edge2[128]; map<int, int> ways; void print(bool deb = false) { int n = 0; for (int i = 0; i <= 100; i++) if (edge1[i] != -1 || edge2[i] != -1) n = i; n = 100; //{ //n++; //if (n > 102) break; //} //n++; printf("%d\n", n); for (int i = 0; i < 100; i++) { int e1 = edge1[i] == -1 ? -1 : edge1[i] + (deb?0:1); int e2 = edge2[i] == -1 ? -1 : edge2[i] + (deb?0:1); if (deb) printf("%d: %d %d\n", i +(deb?0:1), e1, e2); else printf("%d %d\n", e1, e2); } } int count(int s, int e) { if (s == 0) ways.clear(); if (s == -1) return 0; if (s == e) return 1; if (ways[s] != 0) return ways[s]; int res = count(edge1[s],e) + count(edge2[s],e); ways[s] = res; return res; } void cls() { for (int i = 0; i <= 100; i++) { edge1[i] = edge2[i] = -1; } edge1[0] = 1; } int gen2(int s,int m) { //cls(); edge1[s] = 1+s; edge2[s] = 2+s; int e = 2 * m + 1 + s; for (int i = 0; i< m; i++) { if (i<m-1) { edge1[2 * i + 1 + s] = 2 * i + 1 + s + 2; edge2[2 * i + 1 + s] = 2 * i + 2 + s + 2; edge1[2 * i + 2 + s] = 2 * i + 1 + s + 2; edge2[2 * i + 2 + s] = 2 * i + 2 + s + 2; } else { edge1[2 * i + 1 + s] = e; edge2[2 * i + 2 + s] = e; } } //edge2[e - 1] = -1; //edge2[s] = 2; return e; } void testpow() { for (int y = 2; y < 31; y++) { int e = gen2(0, y); printf("numer of roads2 %d = %d \n", y, count(0, e)); } } int gen_binary(int n) { cls(); int leaves = 1; int level = 0; while (leaves < n) { leaves <<= 1; level++; } int n_total = leaves + n; for (int i = 1; i <= n_total; i++) { if (i < leaves) { const int ia = i * 2 < n_total ? i * 2 -1: -1; const int ib = i * 2 + 1 < n_total ? i * 2 + 1 -1: -1; edge1[i - 1] = ia; edge2[i - 1] = ib; } else { if (i!= n_total) { edge1[i - 1] = -1; edge2[i - 1] = -1; } } } return n_total-1; } void solve(int n) { cls(); gen_binary(16); int e = gen2(32, 30); edge1[90] = 99; edge2[91] = 99; int nn = n; int pos = 0; int level = 0; while (nn > 0) { if ((nn % 2) == 1) { if (pos % 2 == 0) edge1[16 + pos / 2] = 90 - 2 * level; else edge2[16 + pos / 2] = 90 - 2 * level; } level++; nn >>= 1; pos++; } } void test() { for (int ii = 1; ii < 1000000; ii++) { int i = rand() % 1000000000; solve(i); int res = count(0, 99); if (i % 10000 == 0) printf("%d\n", i); if (res != i) { printf("test :%3d =%d ", i, res); printf("FAILED!!!!!\n"); return; } //else printf("passed\n"); } } int main() { //test(); int n; scanf("%d\n", &n); solve(n); print(false); return 0; } |