兔子
来源:互联网 发布:匡恩网络 经常加班 编辑:程序博客网 时间:2024/03/29 05:57
Problem Description
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.
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.
Input
The 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 should calculate the number of pairs of rabbits. The input will be terminated by m=d=0.
Output
You must print the number of pairs of rabbits after d months, one integer per line.
Sample Input
2 33 51 1000 0
Sample Output
591267650600228229401496703205376
/*注:数值较大要用大数模板*/#include<cstdio>#include<string.h>#include<iostream>using namespace std;struct mad{ char a[1100];};int main(){ int m, d, k, x1; int n1, n2, n3, r; int i, j, ii; mad b[111]; char c[1100], x; while(cin >> m >> d) { if(!m && !d) break; r = 0, n3 = 0; for(i = 1; i <= m; i++) { x = i + 1;//b[i].a=i+1; if(x > 1 && x <= 9) b[i].a[0] = x + '0'; else { b[i].a[0] = x / 10 + '0'; b[i].a[1] = x % 10 + '0'; } } for(i = m + 1; i <= d; i++) { r = 0, n3 = 0, x1 = 0; n1 = strlen(b[i - 1].a); n2 = strlen(b[i - m].a); //b[i].a=b[i-1].a+b[i-m].a; for(j = n2 - 1, ii = n1 - 1; j >= 0 && ii >= 0; j--, ii--) { x1 = (b[i - 1].a[ii] - '0') + (b[i - m].a[j] - '0') + r; if(x1 > 9) { c[n3++] = x1 % 10 + '0'; r = x1 / 10; } else if(x1 >= 0 && x1 <= 9) { c[n3++] = x1 + '0'; r = 0; } } if(ii < 0 && j < 0 && r) c[n3++] = r + '0'; while(ii >= 0) { x1 = (b[i - 1].a[ii] - '0') + r; if(x1 > 9) { c[n3++] = x1 % 10 + '0'; r = x1 / 10; } else if(x1 >= 0 && x1 <= 9) { c[n3++] = x1 + '0'; r = 0; } ii--; } while(j >= 0) { x1 = (b[i - m].a[j] - '0') + r; if(x1 > 9) { c[n3++] = x1 % 10 + '0'; r = x1 / 10; } else if(x1 >= 0 && x1 <= 9) { c[n3++] = x1 + '0'; r = 0; } j--; } c[n3] = '\0'; k = 0; for(j = n3 - 1; j >= 0; j--) b[i].a[k++] = c[j]; c[0]='\0'; } printf("%s\n", b[d].a); } return 0;}
0 0
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子
- 兔子生兔子问题
- 兔子生兔子问题
- 兔子生兔子问题
- 兔子生兔子问题
- 兔子生兔子
- 兔子生兔子问题
- python兔子生兔子
- 源文件与命令行执行HelloWorld
- hdu 2859
- python获取本机IP、mac地址、计算机名
- String,StringBuffer与StringBuilder的区别??
- mysql where in 排序/不排序
- 兔子
- Android 访问Android Wear数据层Api——同步Data Items
- windows下的端口监听、程序端口查找命令
- hdu 2861
- Android Application对象必须掌握的八点
- VMWare 随系统启动指定虚拟机
- Promise in AngularJS
- 关于hibernate的关系映射的理解
- $random函数的应用