[High accuracy] [Codevs 1145] Hanoi Twin Towers problem

Source: Internet
Author: User

#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < Cmath> #define MAXN 2000#define base 10000using namespace Std;int n;struct bign{int c[maxn],len,sign;//Initialize Bign () { Memset (C,0,sizeof (c)), Len = 1,sign = 0;} High-clear 0 void Zero () {while (len > 1 && c[len] = = 0) len--;if (len = = 1 && C[len] = = 0) sign = 0;}//press bit read in void  Write (char *s) {int k = 1,l = strlen (s); for (int i = L-1;i >= 0;i--) {C[len] + = (s[i]-' 0 ') * k;k *= 10;if (k = = base) {k = 1;len++;}}} void Read () {char S[maxn] = {0};scanf ("%s", s); Write (s);} output void Print () {if (sign) printf ("-");p rintf ("%d", C[len]), and for (int i = len-1;i >= 1;i--) printf ("%04d", C[i]);p rintf ( "\ n");} overloading = operator, low-fine assignment to high-precision bign operator = (int a) {char s[100];sprintf (s, "%d", a); Write (s); return *this;//this can only be used for member functions, representing the address of the current object}//overloading < operator BOOL Operator < (const bign &a) const{if (len! = A.len) Return Len < a.len;for (int i = Len;i >= 1;i--) {if (C[i]! = A.c[i]) return c[i] < a.c[i];} Return0;} BOOL operator > (const bign &a) Const{return a < *this;} BOOL operator <= (const bign &a) Const{return! ( a < *this);} BOOL operator >= (const bign &a) Const{return! ( *this < a);} BOOL operator! = (const bign &a) Const{return a < *this | | *this < A;} BOOL operator = = (Const bign &a) Const{return! ( A < *this) &&! (*this < a);} BOOL operator = = (const int &a) Const{bign b;b = A;return *this = = b;}  Overloaded + operator Bign operator + (const bign &a) {bign R;r.len = max (Len,a.len) + 1;for (int i = 1;i <= r.len;i++) {R.c[i] + = C[i] + a.c[i];r.c[i + 1] + = R.c[i]/base;r.c[i]%= base; }r.zero (); return r;} Bign operator + (const int &a) {bign b;b = A;return *this + b;} Overloaded-operator bign operator-(const bign &a) {bign b,c;//B-CB = *this;c = A;if (c > B) {swap (b,c); b.sign = 1;} for (int i = 1;i <= b.len;i++) {b.c[i]-= C.c[i];if (B.c[i] < 0) {B.c[i] + = Base;b.c[i + 1]--;}} B.zero (); return b;} Bign operator-(const int &a) {Bign b;b = A;rEturn *this-b;} Overloaded * operator bign operator * (const bign &a) {bign R;r.len = len + A.len + 2;for (int i = 1;i <= len;i++) {for (int j = 1 ; J <= a.len;j++) {r.c[i + j-1] + = c[i] * a.c[j];}} for (int i = 1;i <= r.len;i++) {r.c[i + 1] + = R.c[i]/base;r.c[i]%= base;} R.zero (); return r;} Bign operator * (const int &a) {bign b;b = a;return *this * b;} Overloaded/Operator bign operator/(const bign &b) {bign r,t,a;a = b;//if (A = = 0) Return R;r.len = len;for (int i = len;i >= 1 ; i--) {t = T * base + c[i];int div,ll = 0,RR = Base;while (ll <= RR) {int mid = (ll + RR)/2; Bign k = A * mid;if (K > t) rr = Mid-1;else{ll = mid + 1;div = Mid;}} R.c[i] = div;t = T-a * DIV;} R.zero (); return R; }bign operator/(const int &a) {bign b;b = A;return *this/b;} Overloaded% operator Bign operator% (const bign &a) {return *this-*this/a * A;} Bign operator% (const int &a) {bign b;b = A;return *this% B;}}; int main () {scanf ("%d", &n); Bign ans;ans =1;for (int i=1;i<=n+1;i++) ans=ans+ans;ans=ans-2; ans. Print (); return 0;}

[High accuracy] [Codevs 1145] Hanoi Twin Towers problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.