二分a+b,40分

P1001 A+B Problem

touxi @ 2021-11-05 19:29:41

#include<bits/stdc++.h>
using namespace std;
long long a,b;
long long sb(long long x)
{
    if (x==1) return 1;
    else return sb(x/2)+sb(x-x/2);
}
int main()
{
    cin>>a>>b;
    cout<<sb(a)+sb(b);
    return 0;
}

怎么优化


by matianchen @ 2021-11-07 13:22:23

怎么优化\ 直接

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

就行了


by IGUNarding33 @ 2021-11-07 22:26:44

咱不能简单点吗...


by rz54cyq54088 @ 2021-11-17 13:41:06

这么简单的题被你搞的花里胡哨的


by felixesintot @ 2021-11-27 11:58:21

雾) 本题的恶搞题解到此为止。

为防止新人受到误导,不再接受新的此类题解。

以前的保留不会删除,但请不要再提交。

啊这。。。。。


by Terrysong @ 2021-12-07 22:44:38

这不是递归吗?


by codeLJH114514 @ 2021-12-15 19:49:49

只有一个数二分个辣子啊!

在我印象中不应该是两个数么?

真要二分:

#include <iostream>
using namespace std;
int l, r;
int a, b;
int main() {
    cin >> a >> b;
    l = -2000000000, r = 2000000000;
    while (l <= r) {
        int m = (l + r) / 2;
        if (a + b < m) {
            r = m - 1;
        } else if (a + b == m) {
            cout << m;
            exit(0);
        } else {
            l = m + 1;
        }
    }
    return 0;
}

或者(递归版)

#include <iostream>
using namespace std;
int l, r;
int a, b;
int search(int l, int r) {
    int m = (l + r) / 2;
    if (a + b < m) {
        return search(l, m - 1);
    } else if (a + b > m) {
        return search(m + 1, r);
    } else {
        return m;
    }
}
int main() {
    cin >> a >> b;
    l = -2000000000, r = 2000000000;
    cout << search(l, r);
    return 0;
}

by Dream1234 @ 2021-12-22 20:56:11

你想啥呢?为什么你不直接输出a+b???

代码 上!

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;//输入两个数
    cout<<a+b;//输出他们的和
    return 0;//愉快的结束
}

by Roy_Yu @ 2021-12-24 18:51:29

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
using namespace std;
struct node 
{
    int data,rev,sum;
    node *son[2],*pre;
    bool judge();
    bool isroot();
    void pushdown();
    void update();
    void setson(node *child,int lr);
}lct[233];
int top,a,b;
node *getnew(int x)
{
    node *now=lct+ ++top;
    now->data=x;
    now->pre=now->son[1]=now->son[0]=lct;
    now->sum=0;
    now->rev=0;
    return now;
}
bool node::judge(){return pre->son[1]==this;}
bool node::isroot()
{
    if(pre==lct)return true;
    return !(pre->son[1]==this||pre->son[0]==this);
}
void node::pushdown()
{
    if(this==lct||!rev)return;
    swap(son[0],son[1]);
    son[0]->rev^=1;
    son[1]->rev^=1;
    rev=0;
}
void node::update(){sum=son[1]->sum+son[0]->sum+data;}
void node::setson(node *child,int lr)
{
    this->pushdown();
    child->pre=this;
    son[lr]=child;
    this->update();
}
void rotate(node *now)
{
    node *father=now->pre,*grandfa=father->pre;
    if(!father->isroot()) grandfa->pushdown();
    father->pushdown();now->pushdown();
    int lr=now->judge();
    father->setson(now->son[lr^1],lr);
    if(father->isroot()) now->pre=grandfa;
    else grandfa->setson(now,father->judge());
    now->setson(father,lr^1);
    father->update();now->update();
    if(grandfa!=lct) grandfa->update();
}
void splay(node *now)
{
    if(now->isroot())return;
    for(;!now->isroot();rotate(now))
    if(!now->pre->isroot())
    now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}
node *access(node *now)
{
    node *last=lct;
    for(;now!=lct;last=now,now=now->pre)
    {
        splay(now);
        now->setson(last,1);
    }
    return last;
}
void changeroot(node *now)
{
    access(now)->rev^=1;
    splay(now);
}
void connect(node *x,node *y)
{
    changeroot(x);
    x->pre=y;
    access(x);
}
void cut(node *x,node *y)
{
    changeroot(x);
    access(y);
    splay(x);
    x->pushdown();
    x->son[1]=y->pre=lct;
    x->update();
}
int query(node *x,node *y)
{
    changeroot(x);
    node *now=access(y);
    return now->sum;
}
int main()
{
    scanf("%d%d",&a,&b);
    node *A=getnew(a);
    node *B=getnew(b);
    //连边 Link
        connect(A,B);
    //断边 Cut
        cut(A,B);
    //再连边orz Link again
        connect(A,B);
    printf("%d\n",query(A,B)); 
    return 0;
}

by xuzhihao1 @ 2022-01-25 13:48:45

二分查找???


by strcmp @ 2022-02-16 15:49:43

来自2022的问候

跟我以前蒟蒻的时候写快速幂一样(现在是大蒟蒻了

T(n)=2T(n/2)+1=4T(n/4)+2=...... =nT(1)+log_2n=n+log_2n=O(n)

上一页 | 下一页