二分 50分求助

P1001 A+B Problem

somek_ @ 2023-10-12 19:06:55

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    long long l = 1, r = pow(2, 31) - 2;
    while (l < r) {
        long long mid = (l + r) / 2;
        if (a + b <= mid)
            r = mid;
        else
            l = mid + 1;
    }
    cout << r;
}

by GreenMelon @ 2023-10-12 19:08:31

P1001 A+B Problem?你确定?


by somek_ @ 2023-10-12 19:09:36

@I_mAmonkey


by GreenMelon @ 2023-10-12 19:10:37

e...那我看看你这个多此亿举的题


by GreenMelon @ 2023-10-12 19:14:28

其实你不需要二分,你看看样例哈,没有,就默认为 a+b\le10^{18},这就要用到高精度了


by somek_ @ 2023-10-12 19:15:17

@I_mAmonkey

高精度怎么弄(doge)


by GreenMelon @ 2023-10-12 19:15:45

然后这是高精度的代码:

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main(){
    string a, b;
    cin>>a>>b;
    int x[205]={0}, y[205]={0}, s[205]={0};
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for(int i=0;i<a.size();i++){
        x[i]=a[i]-'0';
    }for(int i=0;i<b.size();i++){
        y[i]=b[i]-'0';
    }bool is=false;
    for(int i=0;i<204;i++){
        int tmp=x[i]+y[i];
        if(is==true){
            tmp++;
        }if(tmp/10>=1){
            is=true;
        }else{
            is=false;
        }
        s[i]=tmp%10;
    }bool I_LoveHuTao=false;
    for(int i=204;i>=0;i--){
        if(s[i]!=0&&!I_LoveHuTao){
            I_LoveHuTao=true;
            cout<<s[i];
            continue;
        }
        if(i==0&&!I_LoveHuTao){
            cout<<0;
        }if(I_LoveHuTao){
            cout<<s[i];
        }
    }
}

by GreenMelon @ 2023-10-12 19:16:34

你看看,TLE代码!


by somek_ @ 2023-10-12 19:17:14

@I_mAmonkey

看似高精度,其实夹带私货


by GreenMelon @ 2023-10-12 19:17:58

?什么私货?


by somek_ @ 2023-10-12 19:18:29

@I_mAmonkey

I_LoveHuTao


| 下一页