The Problem
Gustavo knows how to count, but he is now learning how write numbers. as he is a very good student, he already learned 1, 2, 3 and 4. but he didn't realize yet that 4 is different than 1, so he thinks that 4 is another way to write 1. besides that, he is having fun with a little game he created himself: He make numbers (with those four digits) and sum their values. for instance:
132 = 1 + 3 + 2 = 6112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)
After making a lot of numbers in this way, Gustavo now wants to know how much numbers he can create such that their sum is a number n. for instance, for n = 2 he noticed that he can make 5 numbers: 11, 14, 41, 44 and 2 (he knows how to count them up, but he doesn' t know how to write five ). however, he can't figure it out for N greater than 2. so, he asked you to help him. the input
Input will consist on an arbitrary number of sets. Each set will consist on an integer N such that 1 <=n <= 1000. You must read until you reach the end of file.
The output
For each number read, you must output another number (on a line alone) stating how much numbers Gustavo can make such that the sum of their digits is equal to the given number.
Sample Input
23
Sample output
513
The number of Gustavo is always mixed with 1 and 4. He thinks 4 is just another method of writing 1. Given an integer N, Gustavo wants to know the sum of the numbers EXACTLY n. For example, when n = 2, there are 5 numbers: 11, 14, 41, 44, and 2. Analysis: Assume that F (n) represents the total number of sequences constructed using 1, 2, 4, and N, then in these sequences, the number of Sequence Types starting with 1 is F (n-1 ), start with 2 as F (n-2) and start with 3 as F (n-3) the total number of sequences starting with 4 is F (n-4). Because Gustavo treats 4 as 1, F (n-4) = f (n-1 ),
Therefore, F (n) = f (n-1) + f (n-2) + f (n-3) + f (n-4) = 2 * F (n-1) + f (n-2) + f (n-3 ).
Boundary Condition: F (1) = 2, F (2) = 5, F (3) = 13.
#include<string>#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<string> v;string add(string a, string b){ string s; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int i = 0; int m, k = 0; while(a[i] && b[i]) { m = a[i] - '0' + b[i] - '0' + k; k = m / 10; s += (m % 10 + '0'); i++; } if(i == a.size()) { while(i != b.size()) { m = k + b[i] - '0'; k = m / 10; s += m % 10 + '0'; i++; } if(k) s += k + '0'; } else if(i == b.size()) { while(i != a.size()) { m = k + a[i] - '0'; k = m / 10; s += m % 10 + '0'; i++; } if(k) s += k + '0'; } reverse(s.begin(), s.end()); return s;}void solve(){ v.push_back("0"); v.push_back("2"); v.push_back("5"); v.push_back("13"); string s; for(int i = 4; ; i++) { s = add(v[i-1], v[i-1]); s = add(v[i-2], s); s = add(v[i-3], s); v.push_back(s); if(v[i].size() > 1001) break; }}int main(){ solve(); int n; int Size = v.size(); while(cin >> n) { cout << v[n] << endl; } return 0;}