Ultraviolet A 10844-Bloques (second type of Stringer)

Source: Internet
Author: User

Ultraviolet A 10844-Bloques

Question Link

Question: given n numbers, the n numbers can be divided into subsets and divided into several methods.
Idea: At the beginning, I first thought about a state. dp [I] [j] indicates the solution for placing I numbers and dividing them into j sets. Then, from dp [I-1] [j-1] to one more set, and from dp [I-1] [j, the transfer equation is dp [I] [j] = dp [I-1] [j-1] + dp [I-1] [j] * j; it is reasonable to say that this state transfer is OK, but because the answer to this question is high precision, when n is 900, the answer is as high as more than 1700 bits. In addition, the high-precision calculation result times out, the bell number can be introduced through the Yang Hui triangle. Specifically, a number is equal to the sum of the Left and top left sides of the Yang Hui triangle, so that the State transfer becomes a pure high-precision addition, then the high-precision addition operation was performed to compress the bit with high precision, barely passing
Code:

#include 
  
   #include 
   
    #include 
    
     #include using namespace std;using namespace std;const int MAXN = 1800;struct bign {    int len, num[MAXN];    bign () {    len = 0;    memset(num, 0, sizeof(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& 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 operator - (const int& b);    bign operator - (const bign& b);    bign operator * (const int& b);    bign operator * (const bign& b);    bign operator / (const int& b);    //bign operator / (const bign& b);    int operator % (const int& b);};/*Code*/const int N = 905;int n;bign dp[2][N], ans[N];void init() {    int pre = 1, now = 0;    dp[now][1] = 1; ans[1] = 1;    for (int i = 2; i <= 900; i++) {    swap(now, pre);    dp[now][1] = dp[pre][i - 1];    for (int j = 2; j <= i; j++)        dp[now][j] = dp[now][j - 1] + dp[pre][j - 1];    ans[i] = dp[now][i];    }}int main() {    init();    while (~scanf("%d", &n) && n) {    printf("%d, ", n);    ans[n].Put(); printf("\n");    }    return 0;}void bign::DelZero () {    while (len && num[len-1] == 0)    len--;    if (len == 0) {    num[len++] = 0;    }}void bign::Put () {    printf("%d", num[len - 1]);    for (int i = len-2; i >= 0; i--)     printf("%08d", num[i]);}void bign::operator = (char* number) {    len = strlen (number);    for (int i = 0; i < len; i++)    num[i] = number[len-i-1] - '0';    DelZero ();}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 + 10) % 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 % 100000000;    bignSum /= 100000000;    }    while (bignSum) {    ans.num[ans.len++] = bignSum % 100000000;    bignSum /= 100000000;    }    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++) {    bignSub += num[i];    bignSub -= b.num[i];    ans.num[ans.len++] = (bignSub + 10) % 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 * 10 + 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 * 10 + num[i];    ans.num[i] = s/b;    s %= b;    }    return s;}
    
   
  

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.