Sicily 1029 Rabbits

来源:互联网 发布:淘宝 黑搜与白搜 编辑:程序博客网 时间:2024/05/22 17:11

f(n)=f(n-1)+f(n-m)

高精度加法

#include <iostream>#include <string>#include <vector>using namespace std;string add(string a, string b) {// Assumption: a>bint carry=0;string::reverse_iterator aIter=a.rbegin();string::reverse_iterator bIter=b.rbegin();while(bIter!=b.rend()) {int tmp=(*aIter)-'0'+(*bIter)-'0'+carry;(*aIter)=tmp%10+'0';carry=tmp/10;aIter++;bIter++;}if (carry==1) {while (aIter!=a.rend())if ((*aIter!='9'))break;else(*aIter++)='0';if (aIter!=a.rend())(*aIter)+=1;elsea.insert(aIter.base(), '1');}return a;}int main() {int m, d;while(cin>>m>>d, m!=0) {vector<string> rabbits(m, "0");int month;rabbits[0]="2";for (month=1; (month<m) && (month<d); month++)rabbits[month]=add(rabbits[month-1], "1");for (;month<d; month++)rabbits[month%m]=add(rabbits[(month-1)%m], rabbits[(month-m)%m]);cout<<rabbits[(month-1)%m]<<"\n";}}// by wbchou// Feb 19th, 2013


原创粉丝点击