The problem
Gustavo knows how to count, but he's 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 are different than 1, so he thinks this 4 is another a-to write 1. Besides that, he's have fun with a little game he created himself:he make numbers (with those four digits) and sum the IR values. For instance:
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 the This, 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 so he can make 5 numbers:11, A, A, 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 your to help him.The Input
Input would consist on an arbitrary number of sets. Each set would consist on a 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
Test instructions: Gustavo always confuse 1 and 4 when he counts, he thinks 4 is just the second of 1. Give an integer n,gustavo want to know how many numbers the sum of the number is exactly n. For example, when n=2, there are 5 numbers: 11, 14, 41, 44, 2. Analysis: If F (n) represents the use of 1. The total number of sequences constructed by 2,3,4 and N, in which the sequence number starting with 1 is F (n-1). Start with 2 as F (n-2). The total number of sequences starting with 3 is F (n-3), the total number of sequences starting with 4 is F (n-4), because Gustavo treats 4 as 1, there is f (n-4) = f (n-1),
So 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 conditions: 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;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
UVA 10198 Counting