Sicily 1029. Rabbit

Source: Internet
Author: User

The rabbits have powerful reproduction ability. One pair of adult rabbits can give birth to one pair of kid rabbits every month. And after M months, the kid rabbits can become adult rabbits.

As we all know, when m = 2, the sequence of the number of pairs of rabbits in each month is called Fibonacci sequence. but when m <> 2, the problem seems not so simple. you job is to calculate after D months, how many pairs of the rabbits are there if there is exactly one pair of adult rabbits initially. you may assume that none of the rabbits dies in this period. inputthe input may have multiple test cases. in each test case, there is one line having two integers m (1 <= m <= 10), D (1 <= d <= 100 ), M is the number of months after which kid rabbits can become adult rabbits, and D is the number of months after which you shoshould calculate the number of pairs of rabbits. the input will be terminated by M = D = 0. outputyou must print the number of pairs of Rabbits after D months, one integer per line. sample input copy sample input to clipboard
2 33 51 1000 0
Sample output


Analysis: first, the recursive formula F (n) = f (n-1) + f (n-m) can be obtained through analysis. Then, the above recursive formula is used to obtain F (d). In general, recursion is not required, because recursion consumes a lot of time when the value is large, so here we use the memory method, in fact, to save the results with an array. Because the topic describes d <= 100, the number of results can be saved in an array of 100. The other one is the big integer calculation. Here I use a string to represent it. Although it looks a bit annoying, it can still be understood.


# Include <iostream> # include <string> # include <algorithm> using namespace STD; string add (string num1, string num2) {// high-precision addition reverse (num1.begin (), num1.end (); reverse (num2.begin (), num2.end (); string: iterator iter1 = num1.begin (); string: iterator iter2 = num2.begin (); string STR = ""; int add = 0; For (; iter1! = Num1.end (), iter2! = Num2.end (); ++ iter1, ++ iter2) {int value = (* iter1-'0') + (* iter2-'0') + Add; STR = (char) (Value % 10) + '0') + STR; add = value/10;} string: iterator iter = (iter1 = num1.end ()? Iter2: iter1); string: iterator iter_end = (iter1 = num1.end ()? Num2.end (): num1.end (); For (; iter! = Iter_end; ++ ITER) {int value = (* ITER-'0') + Add; STR = (char) (Value % 10) + '0 ') + STR; add = value/10;} If (add! = 0) STR = (char) (add + '0') + STR; return STR;} string getresult (int m, int D) {// The total number of recursive rabbits string result [103]; for (INT I = 0; I <= D; I ++) {if (I <= m) {int value = I + 1; char T [256]; sprintf (T, "% d", value); Result [I] = string (t );} else result [I] = add (result [I-1], result [I-m]); // recursive formula: F (n) = f (n-1) + f (n-m)} return result [d];} int main (INT argc, char const * argv []) {int M, D; whi Le (CIN> m> D & M! = 0 & D! = 0) {cout <getresult (M, d) <Endl;} return 0 ;}



Sicily 1029. Rabbit

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: 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.