MLE求助!!!!!!

P1001 A+B Problem

xueruo @ 2022-11-06 10:47:03

//二分
//#include<iostream>
//using namespace std;
//const long long INF=2*(1e9+10);
//long long a,b;
//int main(){
//  cin>>a>>b;
//  long long mid,left=(0-INF),right=INF;
//  while(left<=right&&mid!=(a+b)){
//      mid=(left+right)>>1;
//      if(mid<a+b)left=mid+1;
//      if(mid>a+b)right=mid-1;
//  }
//  cout<<mid;
//  return 0;
//}
//深搜
#include<iostream>
using namespace std;
long long a,b;
long long dfs(long long x){
    if(x==1)return 1;
    else return dfs(x-1)+1;
}
int main(){
    cin>>a>>b;
    cout<<dfs(a)+dfs(b);
    return 0;
}

函数递归调用太多了,占空间溢出,求优化!!doge


by xueruo @ 2023-03-07 16:29:08

#include<iostream>
#define int long long
using namespace std;
int a,b;
int dfs(int a,int b){
    if(a!=0){
        if(a<0){
            if(a&1==0){
                return dfs(a+2,b-2);
            }else{
                return dfs(a+1,b-1);
            }
        }else{
            if(a&1==0){
                return dfs(a-2,b+2);
            }else{
                return dfs(a-1,b+1);
            }
        }
    }else{
        return b;
    }
}
signed main(){
    cin>>a>>b;
    cout<<dfs(a,b);
    return 0;
}

还是MLE
@youdu666


by youdu666 @ 2023-03-07 16:31:30

@dengjiaqiang 这应该是爆栈了

可以试试记搜?


by youdu666 @ 2023-03-07 16:33:24

@youdu666 我是傻逼,我以为有人在问问题

停止整活


上一页 |