UVa 10198 Counting: Combinatorial mathematics & High Precision

Source: Internet
Author: User
Tags bool time limit

10198-counting

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1139

The Problem

Gustavo knows how to count, but it now learning how write numbers. As he was a very good student, he already learned 1, 2, 3 and 4. But He didn ' t realize yet this 4 is different than 1, so he thinks ' 4 are another way to write 1. Besides that, who has fun with a little game him created himself:he make numbers (with those four digits) and sum the IR values. For instance:

132 = 1 + 3 + 2 = 6
112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)

After making a lot of the numbers in this way, Gustavo then wants to know how much more numbers he can create such that their The sum is A number n. For instance, for n = 2 The He noticed so he can make 5 numbers:11, and 2 (him knows how to count them up, but he Doesn ' t know to write five). However, he can ' t figure it out for n greater than 2. So, he asked your to help him.

The Input

Input would consist on a arbitrary number of sets. Each set would consist on a integer n such that 1 <= n <= 1000. You are must read until you to the end of file.

The Output

For each number read, your must output another number (on a line alone) stating so much numbers Gustavo can do such that The sum of their digits is equal to the given number.

Sample Input

2
3

Sample Output

5
13

Train of thought: with F[i] n=i when the answer, then consider the lowest number, if you choose 1, then a total of f[i-1] number, if it is 2, altogether have f[i-2] number, 3 have f[i-3] number, 4 have f[i-1] number.

So: f[i]=2*f[i-1]+f[i-2]+f[i-3] (f[1]=2,f[2]=5,f[3]=13)

Complete code:

/*0.032s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  
    
const int MAXN = 410;  
    
Char NUMSTR[MAXN];  
    
    struct Bign {int len, S[MAXN];  
        Bign () {memset (s, 0, sizeof (s));  
    len = 1;  
    } bign (int num) {*this = num;  
    } bign (const char* num) {*this = num;  
        } bign operator = (const int num) {char S[MAXN];  
        sprintf (S, "%d", num);  
        *this = s;  
    return *this;  
        } bign operator = (const char* num) {len = strlen (num);  
        for (int i = 0; i < len; i++) S[i] = num[len-i-1] & 15;  
    return *this;  ///Output Const char* str () const {if (len) {for (int i = 0; i < Len  
            i++) numstr[i] = ' 0 ' + s[len-i-1]; Numstr[len] = ' I ';  
        else strcpy (Numstr, "0");  
    return numstr;  
    ///to the leading 0 void clean () {while (len > 1 &&!s[len-1]) len--; ///add///This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45360.htm bign operator + (const BIGN&A mp  
        b) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; G | | | i < MAX (Len, B.len); i++) {int x = g;  
            if (i < len) x + = S[i];  
            if (I < B.len) x + = B.s[i];  
            c.s[c.len++] = x% 10;  
        g = X/10;  
    return C;  
        ///minus bign operator-(const bign& B) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; i < len; i++) {int x = s[i]-G;  
            if (I < b.len) x-= B.s[i];  
            if (x >= 0) g = 0;  
        else {        g = 1;  
            x + 10;  
        } c.s[c.len++] = x;  
        } C.clean ();  
    return C;  
        ///Multiply bign operator * (const bign& b) const {bign C;  
        C.len = len + B.len; for (int i = 0; i < len; i++) for (int j = 0; J < B.len; J +) C.s[i + j] = = S[i] * b  
        . S[j];  
            for (int i = 0; i < c.len-1 i++) {c.s[i + 1] + = c.s[i]/10;  
        C.s[i]%= 10;  
        } C.clean ();  
    return C;  
        }///except bign operator/(const bign &b) const {bign ret, cur = 0;  
        Ret.len = Len;  
            for (int i = len-1 i >= 0; i--) {cur = cur * 10;  
            Cur.s[0] = s[i];  
                while (cur >= b) {cur = b;  
            ret.s[i]++; }} Ret.clean();  
    return ret;  
        }///modulo, remainder bign operator% (const bign &b) Const {bign c = *this/b;  
    return *this-c * b;  
        BOOL operator < (const bign& b) Const {if (len!= b.len) return Len < B.len;  
        for (int i = len-1 i >= 0; i--) if (S[i]!= b.s[i]) return s[i] < b.s[i];  
    return false;  
    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 &a) const {return *this > A | | *this < A; } bign OPerator + = (const bign &a) {*this = *this + A;  
    return *this;  
        } bign Operator-= (const bign &a) {*this = *this-a;  
    return *this;  
        } bign operator *= (const bign &a) {*this = *this * A;  
    return *this;  
        } bign operator/= (const bign &a) {*this = *this/a;  
    return *this;  
        } bign operator%= (const bign &a) {*this = *this% A;  
    return *this;  
    
}} f[1010];  
    int main (void) {f[1] = 2, f[2] = 5, f[3] = 13;  
    for (int i = 4; I <= 1000 ++i) f[i] = F[i-1] + f[i-1] + f[i-2] + f[i-3];  
    int n;  
    while (~SCANF ("%d", &n)) puts (F[n].str ());  
return 0; }

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.