_8008008 @ 2023-03-25 08:43:29
#include<iostream>
using namespace std;
int n;
int f(int m,int s){
m+=s;
if(m==n)return 1;
if(m>n)return 0;
return f(m,1)+f(m,2);
}
int main(){
cin>>n;
cout<<f(0,1)+f(0,2);
return 0;
}
by zhanglihao2011 @ 2023-03-25 09:05:59
#include<iostream>
using namespace std;
long long ans=0;
void dfs(int x){
if(x==1) {
ans++;
return;
}
else if(x==2){
ans=ans+2;
return;
}
for(int i=1;i<=2;i++){
if(i==1){
dfs(x-1);
}
else{
dfs(x-2);
}
}
}
int main(){
int n;
cin>>n;
dfs(n);
cout<<ans;
return 0;
}
by Loser_Syx @ 2023-03-25 09:11:24
高精度
by OldDriverTree @ 2023-03-25 09:15:06
@_8008008 @zhanglihao2011 正解是递推+高精度
by _8008008 @ 2023-03-25 09:33:26
行了 我以为an,n=5000时int和类型递归能抗的住
#include<iostream>
#include<string>
using namespace std;
string add(string str1,string str2){
string str;
int len1=str1.length();
int len2=str2.length();
if(len1<len2){
for(int i=1;i<=len2-len1;i++)
str1="0"+str1;
}
else{
for(int i=1;i<=len1-len2;i++)
str2="0"+str2;
}
len1=str1.length();
int cf=0;
int temp;
for(int i=len1-1;i>=0;i--){
temp=str1[i]-'0'+str2[i]-'0'+cf;
cf=temp/10;
temp%=10;
str=char(temp+'0')+str;
}
if(cf!=0)str=char(cf+'0')+str;
return str;
}
int main(){
string a="1",b="1",c;
int n;
cin>>n;
if(n==1){
cout<<1;
return 0;
}
if(n==2){
cout<<2;
return 0;
}
for(int i=1;i<n;i++){
c=add(a,b);
a=b,b=c;
}
cout<<c;
return 0;
}
by zhanglihao2011 @ 2023-07-17 13:59:47
谢谢