DEV Community

Cover image for The Ultimate Chronicle of Over-Engineering the A+B Problem (POJ 1000)
StardustSeeker404
StardustSeeker404

Posted on

The Ultimate Chronicle of Over-Engineering the A+B Problem (POJ 1000)

A+B: When the Stars of Humanity Shined

Dedicated to all the pioneers of "over-engineering" on the A+B Problem.


Prologue: One Problem, Ten Thousand Ways to Flex

In the corners of countless OJs (Online Judges), there lies a problem named A+B Problem. It has only a single line of requirement: Input two numbers, output their sum.
Yet, warriors picked up their algorithmic weapons and launched an assault on a seemingly impenetrable "simple" fortress.
This is a chronicle of when the stars of humanity shined.


Chapter 1: Data Structures

Bro LCT — Emotional Turbulence

Some say programming is cold and lifeless. Bro LCT (Link-Cut Tree) disagrees.
He built two nodes and let them meetconnect(A, B). Then they broke upcut(A, B). Finally, they reconciledconnect(A, B). After enduring a full cycle of emotional twists and turns, he queried the "fruit of their love" — the path sum.
Two hundred lines of code, all for a romantic addition.

"Link-Cut Tree: I used 200 lines of code just to prove I can calculate 1+1."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_lct.cpp.

#include<iostream>
#include<cstring>
#include<cstdio>
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);
        connect(A,B);
        cut(A,B);
        connect(A,B);
    printf("%d\n",query(A,B));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro Segment Tree — A Sesame Seed in a Shipping Container

Bro Segment Tree said, "My code must have industrial-grade redundancy."
So, he initialized a $4 \times 10^5$ Segment Tree, built over an array containing exactly 1 element. He used Tag Permanization to do interval addition, even though he only added once. He passed the tags parameter to accumulate path markers, even though the path was only one layer deep.
A 50x constant overhead, all for the dignity of a 100-point perfect score.

"Storing a single sesame seed in a shipping container, then proudly saying: Look, my sesame seed has its own dedicated warehouse!"

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_segment_tree.cpp.

#include <cstdio>
#define mid L + (R-L >> 1)
const int maxn = 1e5+5;
int n, a[maxn], m;
int sl, sr, add;
struct segtree{
    int sum[maxn<<2], tag[maxn<<2];
    inline int lc(int o){return o<<1;}
    inline int rc(int o){return o<<1|1;}
    void build(int o, int L, int R){
        if(L == R){sum[o] = a[L];return;}
        int M = mid;
        build(lc(o), L, M);
        build(rc(o), M+1, R);
        sum[o] = sum[lc(o)] + sum[rc(o)];
    }
    void maintain(int o, int L, int R){
        if(R>L){
            sum[o] = sum[lc(o)] + sum[rc(o)];
            sum[o] += tag[o] * (R-L+1);
        } else {
            sum[o] += tag[o];
            tag[o] = 0;
        }
    }
    void updata(int o, int L, int R){
        if(sl <= L && R <= sr)tag[o] += add;
        else{
            int M = mid;
            if(sl <= M)updata(lc(o), L, M);
            if(sr > M)updata(rc(o), M+1, R);
        }
        maintain(o, L, R);
    }
    int query(int o, int L, int R, int tags){
        if(sl <= L && R <= sr)return sum[o] + tags * (R-L+1);
        else {
            int M = mid, res = 0;
            if(sl <= M)res += query(lc(o), L, M, tags+tag[o]);
            if(sr > M)res += query(rc(o), M+1, R, tags+tag[o]);
            return res;
        }
    }
} sol;
signed main(){
    n = 1;
    int a, b;
    scanf("%d%d", &a, &b);
    sol.build(1, 1, n);
    add=a; sl=1; sr=1;
    sol.updata(1, 1, 1);
    add=b;
    sol.updata(1, 1, 1);
    printf("%d\n", sol.query(1, 1, n, 0));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro BIT — A 500,000-Slot Storage Locker

Bro BIT (Binary Indexed Tree) was even more ruthless.
He allocated 500,005 slots of space, using lowbit to maintain the prefix sum of exactly 1 number. Only 1 number, but the array had to be opened up to half a million.
Leaving 499,999 slots empty is an attitude.

"The neighbor asked: Why are there 499,999 empty slots in your storage locker?"

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_bit.cpp.

#include <iostream>
using namespace std;
const int n = 1;
int a, b;
int c[500005];
inline int lowbit(int x){
    return x & (-x);
}
inline int sum(int x){
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i))
    ans+=c[i];
    return ans;
}
void add(int x,int y){
    for(int i=x;i<=n;i+=lowbit(i))
    c[i]+=y;
}
int main(){
    cin>>a>>b;
    add(1, a); add(1, b);
    printf("%d\n", sum(1));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro Splay — The Philosophy of Flipping

Bro Splay said, "Addition satisfies the commutative law."
Then he used 100 lines of code to implement a Splay tree with 4 nodes. He flipped the sequence [a, b] into [b, a]. Finally, they queried the interval sum — whether it was a+b or b+a.
He proved that flipping doesn't affect the result, just as love doesn't affect addition.

"Flipping a sequence just to prove the commutative law of addition—the math teacher is crying in the corner."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_splay.cpp.

#include <bits/stdc++.h>
#define ll long long
#define N 100000
using namespace std;
int sz[N], rev[N], tag[N], sum[N], ch[N][2], fa[N], val[N];
int n, m, rt, x;
void push_up(int x){
    sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
    sum[x] = sum[ch[x][1]] + sum[ch[x][0]] + val[x];
}
void push_down(int x){
    if(rev[x]){
        swap(ch[x][0], ch[x][1]);
        if(ch[x][1]) rev[ch[x][1]] ^= 1;
        if(ch[x][0]) rev[ch[x][0]] ^= 1;
        rev[x] = 0;
    }
    if(tag[x]){
        if(ch[x][1])
            tag[ch[x][1]] += tag[x], sum[ch[x][1]] += tag[x];
        if(ch[x][0])
            tag[ch[x][0]] += tag[x], sum[ch[x][0]] += tag[x];
        tag[x] = 0;
    }
}
void rotate(int x, int &k){
    int y = fa[x], z = fa[fa[x]];
    int kind = ch[y][1] == x;
    if(y == k) k = x;
    else ch[z][ch[z][1]==y] = x;
    fa[x] = z; fa[y] = x; fa[ch[x][!kind]] = y;
    ch[y][kind] = ch[x][!kind]; ch[x][!kind] = y;
    push_up(y); push_up(x);
}
void splay(int x, int &k){
    while(x != k){
        int y = fa[x], z = fa[fa[x]];
        if(y != k) if(ch[y][1] == x ^ ch[z][1] == y) rotate(x, k);
        else rotate(y, k);
        rotate(x, k);
    }
}
int kth(int x, int k){
    push_down(x);
    int r = sz[ch[x][0]]+1;
    if(k == r) return x;
    if(k < r) return kth(ch[x][0], k);
    else return kth(ch[x][1], k-r);
}
void split(int l, int r){
    int x = kth(rt, l), y = kth(rt, r+2);
    splay(x, rt); splay(y, ch[rt][1]);
}
void rever(int l, int r){
    split(l, r);
    rev[ch[ch[rt][1]][0]] ^= 1;
}
void add(int l, int r, int v){
    split(l, r);
    tag[ch[ch[rt][1]][0]] += v;
    val[ch[ch[rt][1]][0]] += v;
    push_up(ch[ch[rt][1]][0]);
}
int build(int l, int r, int f){
    if(l > r) return 0;
    if(l == r){
        fa[l] = f;
        sz[l] = 1;
        return l;
    }
    int mid = l + r >> 1;
    ch[mid][0] = build(l, mid-1, mid);
    ch[mid][1] = build(mid+1, r, mid);
    fa[mid] = f;
    push_up(mid);
    return mid;
}
int asksum(int l, int r){
    split(l, r);
    return sum[ch[ch[rt][1]][0]];
}
int main(){
    n = 2;
    rt = build(1, n+2, 0);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        add(i, i, x);
    }
    rever(1, n);
    printf("%d\n", asksum(1, n));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro Treap — Breaking a into a 1s

Bro Treap said, "Insert a copies of 1, insert b copies of 1, and then look up the rank of 1."
So the loop ran $a+b$ times, inserting 2 million 1s. The tree grew to a height of 20, rotating 40 million times, making the CPU fan spin like crazy.
Finally, it outputs 2000000.

"Using 40 million rotations just to do an addition—the neighbor doing 3D rendering thought I was running a simulation."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_treap.cpp.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read(){int a = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9')
    {if(ch == '-') f = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9')
    {a = a * 10 + ch - '0'; ch = getchar(); }
    return a * f;}
inline void write(int x){if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10); putchar(x % 10 + '0'); }
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
int n;
struct Treap{
    int ls, rs;
    int val, rnd;
    int siz, cnt;
}tr[N];
int tot = 0;
inline void Updata(int x){ tr[x].siz = tr[tr[x].ls].siz
    + tr[tr[x].rs].siz + tr[x].cnt; }
inline int New(int x){
    tr[++tot].val = x;
    tr[tot].rnd = rand();
    tr[tot].siz = tr[tot].cnt = 1;
    return tot;
}
int root = 0;
inline void Build(){
    New(-INF), New(INF);
    root = 1;
    tr[root].rs = 2;
    Updata(root);
}
inline void zig(int &y){
    int x = tr[y].ls;
    tr[y].ls = tr[x].rs;
    tr[x].rs = y;
    y = x;
    Updata(tr[y].rs);
    Updata(y);
}
inline void zag(int &y){
    int x = tr[y].rs;
    tr[y].rs = tr[x].ls;
    tr[x].ls = y;
    y = x;
    Updata(tr[y].ls);
    Updata(y);
}
inline void Insert(int &x, int val){
    if(!x){
        x = New(val);
        return ;
    }
    if(tr[x].val == val){
        ++tr[x].cnt;
        Updata(x);
        return ;
    }
    if(val < tr[x].val){
        Insert(tr[x].ls, val);
        if(tr[x].rnd < tr[tr[x].ls].rnd) zig(x);
    }
    else{
        Insert(tr[x].rs, val);
        if(tr[x].rnd < tr[tr[x].rs].rnd) zag(x);
    }
    Updata(x);
}
inline void Delete(int &x, int val){
    if(!x) return ;
    if(tr[x].val == val){
        if(tr[x].cnt > 1){
            --tr[x].cnt;
            Updata(x);
            return ;
        }
        if(!tr[x].ls && !tr[x].rs){
            x = 0;
            return ;
        }
        else if(!tr[x].rs ||
            (tr[x].ls && tr[tr[x].ls].rnd > tr[tr[x].rs].rnd)){
            zig(x);
            Delete(tr[x].rs, val);
        }
        else{
            zag(x);
            Delete(tr[x].ls, val);
        }
    }
    else if(val < tr[x].val) Delete(tr[x].ls, val);
    else Delete(tr[x].rs, val);
    Updata(x);
}
inline int get_rank(int x, int val){
    if(!x) return 0;
    if(tr[x].val == val) return tr[tr[x].ls].siz + tr[x].cnt;
    if(val < tr[x].val) return get_rank(tr[x].ls, val);
    return get_rank(tr[x].rs, val) + tr[tr[x].ls].siz + tr[x].cnt;
}
inline int get_val(int x, int rank){
    if(!x) return INF;
    if(tr[tr[x].ls].siz >= rank) return get_val(tr[x].ls, rank);
    if(tr[tr[x].ls].siz + tr[x].cnt >= rank) return tr[x].val;
    return get_val(tr[x].rs, rank - tr[tr[x].ls].siz - tr[x].cnt);
}
inline int get_pre(int x, int val){
    int ans = 1;
    while(x){
        if(val == tr[x].val){
            if(tr[x].ls > 0){
                x = tr[x].ls;
                while(tr[x].rs > 0) x = tr[x].rs;
                ans = x;
            }
            break;
        }
        if(val > tr[x].val && tr[x].val > tr[ans].val) ans = x;
        x = val < tr[x].val ? tr[x].ls : tr[x].rs;
    }
    return tr[ans].val;
}
inline int get_last(int x, int val){
    int ans = 2;
    while(x){
        if(val == tr[x].val){
            if(tr[x].rs > 0){
                x = tr[x].rs;
                while(tr[x].ls > 0) x = tr[x].ls;
                ans = x;
            }
            break;
        }
        if(val < tr[x].val && tr[x].val < tr[ans].val) ans = x;
        x = val < tr[x].val ? tr[x].ls : tr[x].rs;
    }
    return tr[ans].val;
}
int main(){
    srand(time(0));
    int a = read(), b = read();
    for(int i = 1; i <= a; i++) {
        Insert(root, 1);
    }
    for(int i = 1; i <= b; i++) {
        Insert(root, 1);
    }
    write(get_rank(root, 1));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Chapter 2: Graph Theory

Bro Dijkstra — The Navigation App's Nightmare

Bro Dijkstra built a graph: 0 -> 1 (edge weight a), and 1 -> 2 (edge weight b).
Then he ran a shortest-path algorithm: relax, enqueue, dequeue, and relax again. Finally, he solemnly announced: "The shortest path from 0 to 2 is a+b."
The navigation app listened to its own glorious achievement and silently uninstalled itself.

"Navigation App: Guess I'll just hand in my resignation?"

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_dijkstra.cpp.

#include<bits/stdc++.h>
#define int unsigned long long
#define code using
#define by namespace
#define XWC std;
#define f first
code by XWC
const int N=1e5+10;
struct edge{
    int v,w;
};
struct node{
    int dis,u;
    bool operator>(const node& a) const {return dis>a.dis;}
};
vector<edge> e[N];
int dis[N],vis[N];
int u,v,w;
int qq[10];
priority_queue<node,vector<node>,greater<node> > q;
void Dijkstra(int n,int s){
    for(int i=0;i<=n;i++) dis[i]=8e18+10;
    for(int i=0;i<=n;i++) vis[i]=0;
    dis[s]=0;
    while(!q.empty()) q.pop();
    q.push({0,s});
    while(!q.empty()){
        int u=q.top().u;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(auto ed:e[u]){
            int v=ed.v,w=ed.w;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push({dis[v],v});
            }
        }
    }
}
int n=2,m=1,a,b;
signed main(){
    cin>>a>>b;
    e[0].push_back({1, a});
    e[1].push_back({2, b});
    Dijkstra(n, 0);
    cout<<dis[2];
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro Kruskal — The Dignity of INF

Bro Kruskal was even more absurd.
He built three edges: a, b, and INF. Then he ran the Minimum Spanning Tree algorithm—it picked a and b, leaving INF completely neglected.
That INF edge was literally born to be ignored.

"The Edge: Excuse me, am I not worth some basic respect?"

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_kruskal.cpp.

#include <cstdio>
#include <algorithm>
#define INF 2140000000
using namespace std;
struct tree{int x,y,t;}a[10];
bool cmp(const tree&a,const tree&b){return a.t<b.t;}
int f[11],i,j,k,n,m,x,y,t,ans;
int root(int x){if (f[x]==x) return x;f[x]=root(f[x]);return f[x];}
int main(){
    for (i=1;i<=10;i++) f[i]=i;
    for (i=1;i<=2;i++){
        scanf("%d",&a[i].t);
        a[i].x=i+1;a[i].y=1;k++;
    }
    a[++k].x=1;a[k].y=3,a[k].t=INF;
    sort(a+1,a+1+k,cmp);
    for (i=1;i<=k;i++){
        x=root(a[i].x);y=root(a[i].y);
        if (x!=y) f[x]=y,ans+=a[i].t;
    }
    printf("%d\n",ans);
}
Enter fullscreen mode Exit fullscreen mode

Chapter 3: Mathematics

Bro Perfect Square — The Romance of Arbitrary Precision

Bro Perfect Square said, "We know that $(a+b)^2 = a^2 + b^2 + 2ab$."
So he wrote 150 lines of high-precision code: addition, subtraction, multiplication, division, square root, and comparison. Then he calculated $1^2 + 2^2 + 2 \times 1 \times 2 = 9$. Taking the square root, he got 3.
The math teacher shook his head in disapproval: "That is not how you use formulas."

"Young man, formulas are meant for you to expand expressions, not for you to take a detour around the world."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_perfect_square.cpp.

#include <bits/stdc++.h>
using namespace std;
string qf (string a)
{
    if (a[0] == '-') return a.substr (1 , a.size () - 1);
    return "-" + a;
}
string operator - (string a) {return qf (a);}
string gjc (string a , string b)
{
    if (a[0] == '-' && b[0] == '-') return gjc (- a , - b);
    else if (a[0] == '-') return - gjc (- a , b);
    else if (b[0] == '-') return - gjc (a , - b);
    int ans[a.size () + b.size ()] = {};
    reverse (a.begin () , a.end ());
    reverse (b.begin () , b.end ());
    for (int i = 0;i < a.size ();i ++)
        for (int j = 0;j < b.size ();j ++)
            ans[i + j] += (a[i] - '0') * (b[j] - '0');
    for (int i = 0;i < a.size () + b.size ();i ++)
        if (ans[i] > 9)
        {
            int x = ans[i] % 10 , y = ans[i] / 10;
            ans[i] = x;
            ans[i + 1] += y;
        }
    string ans2 = "";
    for (int i = 1;i <= a.size () + b.size ();i ++)
        ans2 += (char) (ans[i - 1] + '0');
    if (ans2[ans2.size () - 1] == '0')
        ans2 = ans2.substr (0 , ans2.size () - 1);
    reverse (ans2.begin () , ans2.end ());
    return ans2;
}
string max (string a , string b)
{
    if (a[0] == '-' && b[0] == '-')
    {
        if (- max (- a , - b) == a) return b;
        return a;
    }
    else if (a[0] == '-') return b;
    else if (b[0] == '-') return a;
    else if (a.size () > b.size ()) return a;
    else if (a.size () < b.size ()) return b;
    else
    {
        for (int i = 0;i < a.size ();i ++)
            if (a[i] > b[i]) return a;
            else if (a[i] < b[i]) return b;
        return a;
    }
}
string min (string a , string b)
{
    if (max (a , b) == a) return b;
    return a;
}
string gjj (string a , string b);
string cut (string a , string b)
{
    if (a[0] == '-' && b[0] == '-') return cut (- b , - a);
    else if (a[0] == '-') return - gjj (- a , b);
    else if (b[0] == '-') return gjj (b , - a);
    else if (max (a , b) != a) return - cut (b , a);
    reverse (a.begin () , a.end ());
    reverse (b.begin () , b.end ());
    string ans = "";
    for (int i = 1;i <= a.size () + 1;i ++) ans += "0";
    for (int i = 0;i < b.size ();i ++) ans[i] += a[i] - b[i];
    for (int i = b.size ();i < a.size ();i ++) ans[i] = a[i];
    for (int i = 0;i < ans.size ();i ++)
        if (ans[i] < '0')
        {
            ans[i] += 10;
            ans[i + 1] -= 1;
        }
    while (ans.size () > 1 && ans[ans.size () - 1] == '0')
        ans = ans.substr (0 , ans.size () - 1);
    reverse (ans.begin () , ans.end ());
    return ans;
}
string gjj (string a , string b)
{
    if (a[0] == '-' && b[0] == '-') return - gjc (- a , - b);
    else if (a[0] == '-') return cut (b , - a);
    else if (b[0] == '-') return cut (a , - b);
    if (a.size () < b.size ()) swap (a , b);
    reverse (a.begin () , a.end ());
    reverse (b.begin () , b.end ());
    string ans = "";
    for (int i = 1;i <= a.size () + 1;i ++)
        ans += "0";
    for (int i = 0;i < b.size ();i ++)
        ans[i] += a[i] - '0' + b[i] - '0';
    for (int i = b.size ();i < a.size ();i ++)
        ans[i] = a[i];
    for (int i = 0;i < ans.size ();i ++)
        if (ans[i] > '9')
        {
            ans[i] -= 10;
            ans[i + 1] += 1;
        }
    if (ans[ans.size () - 1] == '0')
        ans = ans.substr (0 , ans.size () - 1);
    reverse (ans.begin () , ans.end ());
    return ans;
}
string dev (string b , int a)
{
    if (a < 0 && b[0] == '-') return dev (- b , - a);
    else if (a < 0) return - dev (b , - a);
    else if (b[0] == '-') return - dev (- b , a);
    while (a % 10 == 0 && b[b.size () - 1] == '0')
    {
        a /= 10;
        b = b.substr (0 , b.size () - 1);
    }
    string ans = "";
    int yu = 0;
    for (int i = 0;i < b.size ();i ++)
    {
        yu = yu * 10 + b[i] - '0';
        ans += yu / a + '0';
        yu %= a;
    }
    while (ans.size () > 1 && ans[0] == '0') ans = ans.substr (1);
    return ans;
}
string operator / (string a , int b)
{return dev (a , b);}
string operator * (string a , string b)
{return gjc (a , b);}
string operator + (string a , string b)
{return gjj (a , b);}
string operator - (string a , string b)
{return cut (a , b);}
string sqrt (string s)
{
    if (s == "0") return "0";
    if (s == "1") return "1";
    string l = "1" , r = "1" , ans , dan = "1";
    for (int i = 1;i < s.size () / 2;i ++) l += "0";
    for (int i = 1;i <= s.size () / 2 + 1;i ++) r += "0";
    while (max (l , r) == r)
    {
        string mid = (l + r) / 2;
        if (max (mid * mid , s) == s)
        {
            ans = mid;
            l = gjj (mid , dan);
        }
        else r = mid - dan;
    }
    return ans;
}
signed main ()
{
    string a , b , c = "2";
    cin >> a >> b;
    if (a[0] == '-' && b[0] == '-')
        cout << - sqrt (a * a + b * b + c * a * b);
    else if (a[0] == '-')
    {
        if (max (- a , b) == b)
            cout << sqrt (a * a + b * b + c * a * b);
        else
            cout << - sqrt (a * a + b * b + c * a * b);
    }
    else if (b[0] == '-')
    {
        if (max (- b , a) == a)
            cout << sqrt (a * a + b * b + c * a * b);
        else
            cout << - sqrt (a * a + b * b + c * a * b);
    }
    else cout << sqrt (a * a + b * b + c * a * b);
}
Enter fullscreen mode Exit fullscreen mode

Bro FFT — A Dimensional Strike from Signals and Systems

Bro FFT said, "Addition in the time domain is convolution; in the frequency domain, it is multiplication!"
So he hand-wrote a complete arbitrary-precision math library from scratch—complex number classes, butterfly operations, bit-packed storage, negative number handling, and borrow-optimization—spanning a grand total of three hundred lines.
To calculate 1+2, he split the two numbers into digits using BASE=100, executed a Fast Fourier Transform to blast them into the frequency domain, performed a point-wise vector multiplication, ran an Inverse FFT, and finally handled the carries.
And that is how high-precision addition was accelerated to $\mathcal{O}(n \log n)$.

"FFT: I optimized the time complexity of big-integer addition. But before the butterfly could even think about flapping its wings, the math was already over."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_fft.cpp.

#include<cmath>
#include<iostream>
#include<cstring>
const double PI=4*atan(1);
template<typename T>
void Swap(T &a,T &b){
    T c=a;
    a=b;
    b=c;
    return;
}
template<typename T>
T Max(const T &a,const T &b){
    return a<b?b:a;
}
typedef long long ll;
struct comp{
    double real,imag;
    comp operator+(const comp &x)const{
        return {real+x.real,imag+x.imag};
    }
    comp operator-(const comp &x)const{
        return {real-x.real,imag-x.imag};
    }
    comp operator*(const comp &x)const{
        return {real*x.real-imag*x.imag,real*x.imag+x.real*imag};
    }
    comp operator/(const unsigned &x)const{
        return {real/(double)x,imag/(double)x};
    }
};
void FFT(comp *f,unsigned n,int rev){
    for(unsigned i=1,j=n>>1,k;i<n-1;i++){
        if(i<j)
            Swap(f[i],f[j]);
        k=n>>1;
        while(j>=k){
            j-=k;
            k>>=1;
        }
        j+=k;
    }
    for(unsigned l=2;l<=n;l<<=1){
        double arg=2*PI*rev/l;
        comp wn={cos(arg),sin(arg)};
        for(unsigned i=0;i<n;i+=l){
            comp w={1,0};
            for(unsigned j=0;j<(l>>1);j++){
                comp f1=f[i+j];
                comp f2=f[i+j+(l>>1)];
                f[i+j]=f1+w*f2;
                f[i+j+(l>>1)]=f1-w*f2;
                w=w*wn;
            }
        }
    }
    if(!~rev)
        for(unsigned i=0;i<n;i++)
            f[i]=f[i]/n;
}
#define BASE 100
template<const unsigned Size>
class bigint{
private:
    unsigned len;
    int num[Size];
    void init(){
        memset(num,0,sizeof(num));
        len=1;
    }
    bool abs_greater_equal(const bigint &a)const{
        if(len!=a.len)
            return len>a.len;
        for(int i=len;i;i--)
            if(num[i]!=a.num[i])
                return num[i]>a.num[i];
        return 1;
    }
public:
    bigint(){
        init();
    }
    void get_num(std::string s){
        init();
        int f=0;
        unsigned slen=s.length();
        if(s[0]=='-')
            num[0]=f=1;
        len=0;
        unsigned temp=0,w=1;
        for(int i=slen-1;i>=f;i--){
            temp+=(s[i]^48)*w;
            w=(w<<1)+(w<<3);
            if(w==BASE||i==f){
                num[++len]=(int)temp;
                temp=0;
                w=1;
            }
        }
        if(temp||len==0)
            num[++len]=temp;
    }
    bool operator<(const bigint &a)const{
        if(num[0]&&!a.num[0])
            return 1;
        if(!num[0]&&a.num[0])
            return 0;
        if(num[0]){
            if(len!=a.len)
                return len>a.len;
            for(int i=len;i;i--)
                if(num[i]!=a.num[i])
                    return num[i]>a.num[i];
        }
        else{
            if(len!=a.len)
                return len<a.len;
            for(int i=len;i;i--)
                if(num[i]!=a.num[i])
                    return num[i]<a.num[i];
        }
        return 0;
    }
    bigint operator+(const bigint &a)const{
        bigint res;
        if(len==1&&num[1]==0){
            res=a;
            return res;
        }
        if(a.len==1&&a.num[1]==0){
            res=*this;
            return res;
        }
        if(num[0]==a.num[0]){
            res.num[0]=num[0];
            unsigned len_sum=1;
            while(len_sum<len+a.len)
                len_sum<<=1;
            comp *fa=new comp[len_sum]();
            comp *fb=new comp[len_sum]();
            for(unsigned i=0;i<len;i++)
                fa[i]={(double)num[i+1],0};
            for(unsigned i=0;i<a.len;i++)
                fb[i]={(double)a.num[i+1],0};
            FFT(fa,len_sum,1);
            FFT(fb,len_sum,1);
            for(unsigned i=0;i<len_sum;i++)
                fa[i]=fa[i]+fb[i];
            FFT(fa,len_sum,-1);
            res.len=Max(len,a.len);
            ll temp=0;
            for(unsigned i=0;i<res.len;i++){
                ll val=(ll)round(fa[i].real)+temp;
                res.num[i+1]=(int)(val%BASE);
                temp=val/BASE;
            }
            if(temp)
                res.num[++res.len]=temp;
            while(res.len>1&&res.num[res.len]==0)
                res.len--;
            delete[] fa;
            delete[] fb;
        }
        else{
            if(abs_greater_equal(a)){
                res.num[0]=num[0];
                unsigned len_sum=1;
                while(len_sum<len+a.len)
                    len_sum<<=1;
                comp *fa=new comp[len_sum]();
                comp *fb=new comp[len_sum]();
                for(unsigned i=0;i<len;i++)
                    fa[i]={(double)num[i+1],0};
                for(unsigned i=0;i<a.len;i++)
                    fb[i]={(double)a.num[i+1],0};
                FFT(fa,len_sum,1);
                FFT(fb,len_sum,1);
                for(unsigned i=0;i<len_sum;i++)
                    fa[i]=fa[i]-fb[i];
                FFT(fa,len_sum,-1);
                res.len=Max(len,a.len);
                ll temp=0;
                for(unsigned i=0;i<res.len;i++){
                    ll val=(ll)round(fa[i].real)+temp;
                    if(val<0){
                        val+=BASE;
                        temp=-1;
                    }
                    else
                        temp=0;
                    res.num[i+1]=(int)(val%BASE);
                }
                if(temp)
                    res.num[++res.len]=temp;
                while(res.len>1&&res.num[res.len]==0)
                    res.len--;
                delete[] fa;
                delete[] fb;
            }
            else{
                res.num[0]=a.num[0];
                unsigned len_sum=1;
                while(len_sum<len+a.len)
                    len_sum<<=1;
                comp *fa=new comp[len_sum]();
                comp *fb=new comp[len_sum]();
                for(unsigned i=0;i<len;i++)
                    fa[i]={(double)num[i+1],0};
                for(unsigned i=0;i<a.len;i++)
                    fb[i]={(double)a.num[i+1],0};
                FFT(fa,len_sum,1);
                FFT(fb,len_sum,1);
                for(unsigned i=0;i<len_sum;i++)
                    fa[i]=fb[i]-fa[i];
                FFT(fa,len_sum,-1);
                res.len=Max(len,a.len);
                ll temp=0;
                for(unsigned i=0;i<res.len;i++){
                    ll val=(ll)round(fa[i].real)+temp;
                    if(val<0){
                        val+=BASE;
                        temp=-1;
                    }
                    else
                        temp=0;
                    res.num[i+1]=(int)(val%BASE);
                }
                if(temp)
                    res.num[++res.len]=temp;
                while(res.len>1&&res.num[res.len]==0)
                    res.len--;
                delete[] fa;
                delete[] fb;
            }
            if(res.len==1&&res.num[1]==0)
                res.num[0]=0;
        }
        return res;
    }
    bigint operator*(const bigint &a)const{
        bigint res;
        if((len==1&&num[1]==0)||(a.len==1&&a.num[1]==0))
            return res;
        res.num[0]=num[0]^a.num[0];
        unsigned len_sum=1;
        while(len_sum<len+a.len)
            len_sum<<=1;
        comp *fa=new comp[len_sum]();
        comp *fb=new comp[len_sum]();
        for(unsigned i=0;i<len;i++)
            fa[i]={(double)num[i+1],0};
        for(unsigned i=0;i<a.len;i++)
            fb[i]={(double)a.num[i+1],0};
        FFT(fa,len_sum,1);
        FFT(fb,len_sum,1);
        for(unsigned i=0;i<len_sum;i++)
            fa[i]=fa[i]*fb[i];
        FFT(fa,len_sum,-1);
        res.len=len+a.len;
        ll temp=0;
        for(unsigned i=0;i<res.len;i++){
            ll val=(ll)(fa[i].real+0.5)+temp;
            res.num[i+1]=(int)(val%BASE);
            temp=val/BASE;
        }
        if(temp)
            res.num[++res.len]=temp;
        while(res.len>1&&res.num[res.len]==0)
            res.len--;
        delete[] fa;
        delete[] fb;
        return res;
    }
    void read(){
        init();
        std::string s;
        char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')
                s.push_back('-');
            ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            s.push_back(ch);
            ch=getchar();
        }
        get_num(s);
    }
    void print(){
        if(num[0])
            putchar('-');
        bool leading_zero=1;
        for(int i=len;i;i--){
            if(leading_zero)
                printf("%d",num[i]);
            else
                printf("%02d",num[i]);
            leading_zero=0;
        }
        putchar('\n');
        return;
    }
};
const int N=1<<10,M=100;
int n,m;
bigint<114514> a,b,c;
int main(){
    a.read();
    b.read();
    c=a+b;
    c.print();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Chapter 4: Search Space Overkill

Bro Binary Search — Acting Skills for Sneaking a Peek at the Answer

Bro Binary Search wrote a binary search algorithm.
He tried to guess a number within the range $[-10^9, 10^9]$, repeating the process 31 times. Inside every single loop iteration, he ran a comparison: if(mid == a+b).
He clearly already calculated a+b, yet he put on an act pretending he didn't know it.

"It's like sneaking a peek at the answer key during an exam, and then acting like you solved it yourself."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_binary_search.cpp.

#include<bits/stdc++.h>
#define N 100010
using namespace std;
int a,b;
int s(int a,int b) {
    int l=-1e9,r=1e9,mid;
    while(l<r) {
        mid=(l+r)/2;
        if(mid==a+b) return mid;
        else if(mid>a+b) r=mid;
        else l=mid;
    }
}
int main() {
    cin>>a>>b;
    cout<<s(a,b);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bro Simulated Annealing — Confusing the Physics Department

Bro Simulated Annealing cranked the virtual system temperature to 1,000,000, set the cooling coefficient to 0.999, and ran the simulation for 100 grueling rounds. With 100,000 iterations per round, that's a grand total of 10 million random rolls of the dice.
As the system cooled down from a million degrees to a freezing $10^{-10}$, the thermodynamic system finally stabilized and converged to... 3.
A physics major from next door walked over to take a look: "Why are you simulating molecular thermodynamics or quantum chaos?"
"No, I'm calculating 1+2."
The physics student walked away and never spoke to him again.

"Physics Student: We simulate actual molecular kinetics to discover new states of matter. What do you guys simulate? First-grade math."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_simulated_annealing.cpp.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const double d=0.999;
const double lim=1e-10;
ll a,b;
ll ans;
ll num;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+(ch^48);
        ch=getchar();
    }
    return x*f;
}
int calc(int x)
{
    return abs(a+b-x)-abs(a+b-ans);
}
void ghost_fire()
{
    double T=1000000;
    while(T>lim)
    {
        int x=num+((rand()<<1)-RAND_MAX)*T;
        int del=calc(x);
        if(del<0)
        {
            ans=x;
            num=x;
        }
        else if(exp(-del/T)>(double)rand()/RAND_MAX) num=x;
        T*=d;
    }
}
void work()
{
    for(int i=1;i<=100;i++) ghost_fire();
}
int main()
{
    a=read();
    b=read();
    work();
    cout<<ans<<endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Chapter 5: Low-Level Madness

Bro VM — Raising a Cow for a Glass of Milk

Bro VM architected a custom 5-instruction Instruction Set Architecture (ISA): LOAD, ADD, STORE, PRINT, HALT.
Then he wrote a 100-line virtual environment interpreter. Next, he wrote a 6-line custom assembly program. Finally, he deployed and executed it on his custom software-defined silicon.
And after all that hardware emulation, he got 3.

"To calculate 1+2, I literally built a virtual CPU architecture, defined an instruction set, wrote an assembly script, and emulated a computer... People ask me why I didn't just use the built-in operator. Because my virtual CPU doesn't have a microcode mapping for laziness."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_vm.cpp.

#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
enum class OpCode { LOAD, ADD, STORE, PRINT, HALT };
struct Instruction { OpCode op; int operand1; int operand2; };
class VirtualMachine {
    std::vector<int> memory;
    std::vector<int> registers;
    size_t pc;
    bool running;
public:
    VirtualMachine(size_t memSize, size_t regCount)
        : memory(memSize, 0), registers(regCount, 0), pc(0),
        running(false) {}
    void execute(const std::vector<Instruction>& program) {
        running = true;
        while (running && pc < program.size()) {
            const auto& instr = program[pc];
            switch (instr.op) {
                case OpCode::LOAD: registers[instr.operand1] =
                instr.operand2; pc++; break;
                case OpCode::ADD: registers[instr.operand1] +=
                registers[instr.operand2]; pc++; break;
                case OpCode::STORE: memory[instr.operand1] =
                 registers[instr.operand2]; pc++; break;
                case OpCode::PRINT:
                std::cout << memory[instr.operand1] << std::endl;
                pc++; break;
                case OpCode::HALT: running = false; break;
                default: throw std::runtime_error("Unknown opcode");
            }
        }
    }
};
int main() {
    try {
        int a, b;
        std::cin >> a >> b;
        VirtualMachine vm(16, 4);
        std::vector<Instruction> program = {
            {OpCode::LOAD, 0, a},
            {OpCode::LOAD, 1, b},
            {OpCode::ADD, 0, 1},
            {OpCode::STORE, 0, 0},
            {OpCode::PRINT, 0, 0},
            {OpCode::HALT, 0, 0}
        };
        vm.execute(program);
        return 0;
    } catch (const std::exception& e) { return 1; }
}
Enter fullscreen mode Exit fullscreen mode

Chapter 6: Esoteric Languages

Bro Chinese Macros — Outsmarting Absolutely Nobody

Bro Chinese Macros spammed #define until C++ looked like native Chinese text.
The compiler had to do all the heavy lifting, translating the macros back into English behind his back just to pass compilation.

"The Compiler: I literally translated every single word back to English for you. Who are you trying to trick?"

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_chinese_macros.cpp.

#include <bits/stdc++.h>
using namespace std;
#define 开始啦 main()
#define 开头 {
#define 结尾 }
#define 和 ,
#define 没了 return 0
#define 整数32位 int
#define 输入 cin
#define 输出 cout
#define 一个 >>
#define 这个 <<
#define 加上 +
#define 换行 '\n'
整数32 开始啦
开头
    整数32 a  b;
    输入 一个 a 一个 b;
    输出 这个 a 加上 b 这个 换行;
    没了;
结尾
Enter fullscreen mode Exit fullscreen mode

Bro Classical Chinese — Confusing the Entire Faculty

Bro Classical Chinese used wenyan-lang to code A+B.
The history teacher thought he was uncovering ancient imperial dynasties, the literature teacher thought he was writing high-brow poetry, and the compiler just stood there sweating:
"Look, I'm just going to transpile this into plain JavaScript, okay?"

"Total System Crash: The history teacher is confused, the literature teacher is baffled, and the compiler is trying to figure things out."

🔒 Secure Deployment: Heavy weaponry isolated for active-active redundancy. Source code fully archived in heavy_artillery/bro_classical_chinese.wy.

施「require('fs').readFileSync」於「「/dev/stdin」」。名之曰「數據」。
施「(buf => buf.toString().trim())」於「數據」。昔之「數據」者。今其是矣。
施「(s => s.split(' '))」於「數據」。昔之「數據」者。今其是矣。
注曰。「「文言尚菜,無對象之操作,故需 JavaScript 之语法」」。
夫「數據」之一。取一以施「parseInt」。名之曰「甲」。
夫「數據」之二。取一以施「parseInt」。名之曰「乙」。
加「甲」以「乙」。書之。
Enter fullscreen mode Exit fullscreen mode

Chapter 7: The Flex Hall of Fame

Epilogue: When the Stars of Humanity Shined

Some of these developers wrote $200$ lines of code, some ran $10$ million iterations, and some architected a custom virtual machine from scratch.
They could have easily written a single line of standard addition—but they chose not to.
They took a simple problem and made it wonderfully complex; they took complex code and made it completely elegant; and they took an elegant algorithm and turned it into pure romance.
They were never just calculating A+B.
They were proving to the world:

A programmer's true romance is taking a beginner's entry problem and writing it into an epic.

The Universe Family Photo

Clan Over-Engineered Masterpiece The Architectural Truth
Bro LCT Dynamic Pointer Addition Meet → Break Up → Reconcile. Full of dramatic twists and turns.
Bro Segment Tree Mega-Warehouse Logistics Parking a 40-foot shipping container just to store a single sesame seed.
Bro BIT The 500k-Slot Locker Room Allocating half a million array slots just to keep track of one number.
Bro Splay Pointer-Flipping Gymnastics Flipping an array to prove addition works both ways; math teacher is crying.
Bro Treap 2 Million Random Node Spams Forcing the CPU fan to spin faster than the code itself.
Bro Dijkstra Graph-Theory Navigation Navigation App: 'I guess my entire engineering team is redundant then?'
Bro Kruskal Forced Drama for INF Creating an infinite edge just to completely ignore it during execution.
Bro Perfect Square BigInt Sqrt Detour Math Teacher: 'That is quite a long way to use an algebraic identity.'
Bro FFT Signal-Processing Overkill Trying to blast numbers into the frequency domain before the CPU hits timeout.
Bro Binary Search Technical Acting Game Checking the final answer key directly, but acting like it's a guess.
Bro Simulated Annealing 10 Million Thermal Dice Rolls Physics Major: 'Please do not use molecular thermodynamics for first-grade math.'
Bro VM Emulated Software CPU Building an entire computer architecture just to get a single digit back.
Bro Chinese Macros #define Translation Layer The Compiler: 'I translated it all back anyway. Nice try.'
Bro Classical Chinese Ancient Imperial Script The history department, literature department, and compiler are all bewildered.

Postscript: When the Stars Finally Rested

One fine day, a complete coding novice clicked into the solution forum for the A+B Problem.
He saw Link-Cut Trees, Splay trees, Simulated Annealing, Custom Virtual Machines, and Ancient Imperial Scripts... He stared at his monitor in absolute, stunned silence. Then, with shaking hands, he typed out his very first lines of code: newbie.cpp.

#include<iostream>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

In that exact moment, enlightenment hit him:

Every blinding star in the algorithmic galaxy was flashing just to illuminate the most basic truth.
And the most basic truth usually requires exactly one single line of code.


THE END

Top comments (0)