为什么MLE

P1001 A+B Problem

jjy2023 @ 2023-08-01 22:09:19

P1001 为什么会MLE?求大佬解答

MLE代码:

#include<bits/stdc++.h>
using namespace std;
int n,ans=0;
void dfs(int x,int y)
{
    if(x==0&&y==0) return;
    if(x>y) ans++,dfs(x-1,y);
    else ans++,dfs(x,y-1);
}
int main() 
{
    int a,b;
    cin>>a>>b;
    dfs(a,b);
    cout<<ans;
    return 0;
}

by sc84bbs @ 2023-08-01 22:11:05

深度太高炸了。


by Terrible @ 2023-08-01 22:12:12

考虑以下输入:

1000000000 1000000000
-1 -1

by jjy2023 @ 2023-08-01 22:14:45

@Terrible 收到,谢谢指导


by politeelm @ 2023-08-02 12:27:29

@jjy2023 正确答案:

#include<bits/stdc++.h>
using namespace std;
struct RAINBOW{
    void AC(){
        int a,b;
        cin>>a>>b;
        cout<<a+b;
    }
    void WA(){
        puts("QWQ");
    }
    void RE(){
        cout << (*(int *)NULL) << endl;
    }
    void TLE(){
        while(1);
    }
    long long *magic[10086];
    void MLE(){
        for(int i = 0;;){
            magic[i] = new long long;
        }
    }
    void OLE(){
        for(long long i = 1;true; i++)
            putchar(i % 26 + 'A');
    }
    void UKE(){
        puts("zhang_kevin AK IOI");
    }
}Rainbow;
int main(){
    int n;
    n=rand()%6;
    if(n == 1) Rainbow.AC();
    if(n == 2) Rainbow.WA();
    if(n == 3) Rainbow.RE();
    if(n == 4)Rainbow.TLE();
    if(n == 5)Rainbow.MLE();
    if(n == 6)Rainbow.OLE();
    if(n == 7)Rainbow.UKE();
    return 0;
}

https://www.luogu.com.cn/record/118578568 亲测有效!


by WWW_bilibili_com @ 2023-08-08 08:36:20

答案

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

by zouzouzi @ 2023-08-08 14:54:40

@WWW_bilibili_com 开long long更安全吧,用printfscanf更快吧(要用lld)


by Gcc_Gdb_7_8_1 @ 2023-08-08 15:05:40

@Diandian07go 这个有必要用scanf()printf()吗?


by zouzouzi @ 2023-08-08 15:14:25

@wenlida 这样更快


by Gcc_Gdb_7_8_1 @ 2023-08-08 15:38:44

@Diandian07go

这样是更快,但还会MLE,代码不是TLE或WA,还有,我想问一下,作者为什么要用dfs?


by zouzouzi @ 2023-08-08 15:47:32

@wenlida 我说的是这个代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

| 下一页