[Sicily online] 1029. Rabbit

Source: Internet
Author: User
/* 1, large integer addition, use string to simulate 2, use an array to simulate the rabbit growth process constraintstime limit: 1 secs, memory limit: 32 mbdescriptionthe 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 month S 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 input2 33 51 1000 0 sample output591267109600228229401496703205376 */# include <iostream> # include <fstream> # include <vector> # Include <string> # include <algorithm> # include <cmath> # include <list> # include <map> # include <string. h> using namespace STD; string strplus (string X1, string x2) {for (string: size_type I = 0; I <x2.size (); I ++) {if (I> = x1.size () x1.push _ back ('0'); if (I> = x2.size () break; int num1 = x1 [I]-'0'; int num2 = x2 [I]-'0'; int sum = num1 + num2; x1 [I] = sum % 10 + '0'; If (sum <10) continue; Int J = I + 1; while (1) {If (j> = x1.size () {x1.push _ back ('1'); break ;} If (x1 [J] = '9') {x1 [J] = '0'; j ++;} else {x1 [J] ++; break ;}}return X1 ;}int main () {int M, D; while (CIN> m> D & M! = 0) {vector <string> mouths (m); mouths [M-1] = "1"; while (d --) {string tmplast = mouths M-1]; if (M> 1) mouths [M-1] = strplus (Mouths [M-1], mouths [m-2]); else mouths [s-1] = strplus (Mouths [M-1], mouths [M-1]); For (INT I = m-3; I> = 0; I --) {mouths [I + 1] = mouths [I];} // end forif (M> 1) mouths [0] = tmplast;} string sum = "0"; for (INT I = 0; I <m; I ++) sum = strplus (sum, mouths [I]); reverse (sum. begin (), Sum. end (); cout <sum <Endl ;}// end while}

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.