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
| #include <bits/stdc++.h> #define eps 1e-8 using namespace std; #define ms(a, b) memset((a), (b), sizeof(a))
typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; const int N = 1e5 + 5; const int M = 1e6 + 5; const int INF = 0x3f3f3f3f; const ll ll_max = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; inline ll read() { ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~ res + 1) : res; } inline int _(char a, char b){ if (a == 'a' && b == 'b') return 1; if (a == 'a' && b == 'c') return 2; if (a == 'b' && b == 'a') return 3; if (a == 'b' && b == 'c') return 4; if (a == 'c' && b == 'a') return 5; if (a == 'c' && b == 'b') return 6; } struct node{ ll sum[7]; node(){ms(sum, 0);}; node operator + (const node & x)const{ node tmp; for (int i = 1; i <= 6; ++i) tmp.sum[i] = sum[i] + x.sum[i]; return tmp; } ll getsum(){ ll tmp = 0; for (int i = 1; i <= 6; ++i) tmp += sum[i]; return tmp; } }ans; using tu = tuple<int, char, char, char>; map<tu, int>vis; map<tu, node>ma; node Hanoi(int n, char a, char b, char c){ tu p = make_tuple(n, a, b, c); if (vis[p]) return ma[p]; node tmp; if (n == 1) { tmp.sum[_(a, c)]++; return tmp; } tmp = tmp + Hanoi(n - 1, a, c, b); tmp.sum[_(a, c)]++; tmp = tmp + Hanoi(n - 1, b, a, c); vis[p] = 1; return ma[p] = tmp; }
int main(){ int n = read(); ans = Hanoi(n, 'a', 'b', 'c'); printf("A->B:%lld\n",ans.sum[1]); printf("A->C:%lld\n",ans.sum[2]); printf("B->A:%lld\n",ans.sum[3]); printf("B->C:%lld\n",ans.sum[4]); printf("C->A:%lld\n",ans.sum[5]); printf("C->B:%lld\n",ans.sum[6]); printf("SUM:%lld\n", ans.getsum()); return 0; }
|