Recursive + high precision UVA 10497 Sweet Child makes trouble (cute kids get in trouble)

Source: Internet
Author: User

Topic links

Test instructions

The number of scenarios in which n items are all in order (not in the original position).

Ideas:

Dp[i] represents the number of scenarios in which I items are ordered in order, so the state transfer equation. Consider the i-1 items in order to put the article I items must be and I-1 one of the exchange position, that is; Consider i-2 items disorderly order, the first i-1 and I first in the original position, the two methods make the disorderly order, A and I exchange (can not and the former I-2 exchange, so into dp[i-1]), There is also the first and second i-1 exchanges, and then one exchange with the former i-2, that is, to think carefully, this and dp[i-1] is a different method of exchange.

Other than that:

There are two-dimensional DP notation, although high-precision can not be opened, but it is instructive. Set DP[I][J] means the number of items J of the I item, the state transfer equation, the last item (i-j+1) means that the former i-1 in the original position of the goods to exchange, namely.

#include <bits/stdc++.h>const int maxn = + 5;struct bign {int len, num[maxn];bign () {len = 0;memset (num, 0, si Zeof (num));} Bign (int number) {*this = number;} Bign (const char* number) {*this = number;} void Delzero (), void Put (), void operator = (int number), void operator = (char* number), BOOL operator < (const BIGN&AM P b) Const;bool operator > (const bign& B) Const {return b < *this;} BOOL operator <= (const bign& B) Const {return! b < *this); }bool operator >= (const bign& B) Const {return! *this < b); }bool operator! = (const bign& b) Const {return b < *this | | *this < b;} BOOL operator = = (CONST bign& b) Const {return! ( b! = *this); }void operator + + (); void operator--(); Bign operator + (const int& b); Bign operator + (const bign& b); Bign Opera Tor-(const int& b); Bign operator-(const bign& b); bign operator * (const int& b); bign operator * (const BIG n& b); bign operator/(const int& B);Bign operator/(const bign& b); int operator% (const int& b);};    Bign Dp[805];int Main () {dp[0] = 0; dp[1] = 0; dp[2] = 1;    for (int i=3; i<=800; ++i) {Dp[i] = (Dp[i-1] + dp[i-2]) * (I-1);    } int n; while (scanf ("%d", &n) = = 1 && n! =-1) {Dp[n].        Put ();    Puts (""); } return 0;} void Bign::D Elzero () {while (len && num[len-1] = = 0) len--;if (len = = 0) {num[len++] = 0;}} void bign::P ut () {for (int i = len-1; I >= 0; i--) printf ("%d", Num[i]);} void Bign::operator = (char* number) {len = strlen (number), for (int i = 0; i < len; i++) num[i] = number[len-i-1]-' 0 ' ;D Elzero ();} void Bign::operator = (int number) {len = 0;while (number) {num[len++] = Number%10;number/= 10;} Delzero ();} BOOL Bign::operator < (const bign& b) Const {if (len! = B.len) return len < b.len;for (int i = len-1; I >= 0; i--) if (num[i]! = B.num[i]) return Num[i] < B.num[i];return false;} void Bign::operator + + () {int s = 1;for (int i = 0; i < Len; i++) {s = s + num[i];num[i] = s% 10;s/= 10;if (!s) break;} while (s) {num[len++] = S%10;s/= 10;}} void Bign::operator--() {if (num[0] = = 0 && len = = 1) return;int s = -1;for (int i = 0; i < len; i++) {s = s + Num[i];num[i] = (s + Ten)% 10;if (s >= 0) break;} Delzero ();} Bign Bign::operator + (const int& b) {bign a = B;return *this + A;} Bign Bign::operator + (const bign& b) {int bignsum = 0;bign ans;for (int i = 0; i < len | | i < B.len; i++) {if ( I < len) Bignsum + = Num[i];if (i < B.len) Bignsum + = b.num[i];ans.num[ans.len++] = bignsum% 10;bignsum/= 10;} while (bignsum) {ans.num[ans.len++] = bignsum% 10;bignsum/= 10;} return ans;} Bign Bign::operator-(const int& b) {bign a = B;return *this-a;} Bign Bign::operator-(const bign& b) {int bignsub = 0;bign ans;for (int i = 0; i < len | | i < B.len; i++) {bign Sub + = num[i];bignsub-= b.num[i];ans.num[ans.len++] = (bignsub +)% 10;if (Bignsub < 0) bignsub =-1;}Ans. Delzero (); return ans;}  Bign Bign::operator * (const int& b) {int bignsum = 0;bign Ans;ans.len = len;for (int i = 0; i < len; i++) {Bignsum + = num[i] * b;ans.num[i] = bignsum% 10;bignsum/= 10;} while (bignsum) {ans.num[ans.len++] = bignsum% 10;bignsum/= 10;} return ans;}  Bign Bign::operator * (const bign& b) {bign Ans;ans.len = 0; for (int i = 0; i < len; i++) {int bignsum = 0;  for (int j = 0; J < B.len; J + +) {bignsum + = num[i] * B.num[j] + ans.num[i+j];  ANS.NUM[I+J] = bignsum% 10;  Bignsum/= 10;}  Ans.len = i + B.len;  while (bignsum) {ans.num[ans.len++] = bignsum% 10;  Bignsum/= 10;} } return ans; Bign bign::operator/(const int& b) {bign ans;int s = 0;for (int i = len-1; I >= 0; i--) {s = s * + Num[i];ans. Num[i] = S/b;s%= b;} Ans.len = Len;ans. Delzero (); return ans;} int Bign::operator% (const int& b) {bign ans;int s = 0;for (int i = len-1; I >= 0; i--) {s = s * + NUM[I];ANS.N Um[i] = S/b;s%= b;} return s;}

  

Recursive + high precision UVA 10497 Sweet Child makes trouble (cute kids get in trouble)

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.