classBigUint{public:BigUint(){}BigUint(u32num){while(num){autoconst[quot,rem]=div(num,10);digits_.push_back(rem);num=quot;}}BigUintoperator-(BigUintconst&rhs)const{assert(digits_.size()>=rhs.digits_.size());autoret=digits_;for(u32i=0,sz=rhs.digits_.size();i!=sz;++i){ret[i]-=rhs.digits_[i];if(ret[i]<0){ret[i]+=10;--ret[i+1];}}for(u32i=rhs.digits_.size();ret[i]<0;){ret[i]+=10;--ret[++i];}trunc_zero(ret);return{move(ret)};}BigUintoperator+(BigUintconst&rhs)const{if(digits_.size()<rhs.digits_.size()){returnrhs+*this;}autoret=digits_;ret.push_back(0);for(u32i=0,sz=rhs.digits_.size();i!=sz;++i){ret[i]+=rhs.digits_[i];if(ret[i]>=10){ret[i]-=10;++ret[i+1];}}for(u32i=rhs.digits_.size();ret[i]>=10;++i){++ret[i+1];ret[i]-=10;}trunc_zero(ret);return{move(ret)};}friendostream&operator<<(ostream&os,BigUintconst&num){if(num.digits_.empty()){returnos<<'0';}autoi=num.digits_.rbegin();autoed=num.digits_.rend();while(i!=ed){os<<+*i++;}returnos;}private:usingDigitVec=vector<i8>;DigitVecdigits_;BigUint(DigitVec&&digits):digits_(digits){}voidstatictrunc_zero(DigitVec&vec){while(!vec.empty()&&vec.back()==0){vec.pop_back();}}};intmain(){BigUintarr[102]{0,0,1,1};// Start from one.u32k,n;cin>>k>>n;for(u32i=4;i<=k;++i)arr[i]=arr[i-1]+arr[i-1];for(u32i=max((u32)4,k+1);i<=n;++i)arr[i]=arr[i-1]+arr[i-1]-arr[i-1-k];cout<<arr[n]<<'\n';}
我看到这题的数据量后,十分肯定地认为它不过四位数……是我智熄了……
附一个无符号数加减模板。高精度代码写起来在数学上有也没什么技术含量,就是多想想……
ull都不够。。。。
include
include
include
include
include
using namespace std;
vector aims;
string addStringNum(string small,string big)
{
if(small.length()>big.length())
swap(small,big);
reverse(small.begin(),small.end());
reverse(big.begin(),big.end());
int addition=0,yu=0;
for(int i=0; i<big.length(); i++)
{
char mid=’\0’;
if(i<small.size())
mid=small[i];
else
mid=‘0’;
yu=((mid-‘0’)+(big[i]-‘0’)+addition)%10;
addition=((mid-‘0’)+(big[i]-‘0’)+addition)/10;
big[i]=(char)(yu+‘0’);
}
if(addition==1)
big=big+”1”;
reverse(big.begin(),big.end());
return big;
}
//闭区间的和
string getSum(int beginIndex,int endIndex)
{
string sum=”“;
for(int i=beginIndex; i<=endIndex; i++)
sum=addStringNum(sum,aims[i]);
return sum;
}
int main()
{
//初始化
aims.push_back(“0”);
aims.push_back(“1”);
int k,n;
cin>>k>>n;
while(k>aims.size())
aims.push_back(getSum(0,aims.size()-1));
if(aims.size()>=n)
{
cout<<aims[n-1]<<endl;
return 0;
}
for(int i=aims.size(); i<n; i++)
aims.push_back(getSum(i-k,i-1));
cout<<aims[aims.size()-1]<<endl;
return 0;
}