24 条题解

  • 0
    @ 2025-6-2 19:04:16

    记得一键三连哦!

    超级详细的!

    注意(打个广告:我们班的xhchenyr超级有实力的!!!)

    c++

    #include<bits/stdc++.h>
    using namespace std;
    int a[2000],b[2000],f[2000],l,p,o,g;
    int main()
    {
    	string n,m;
    	cin>>n>>m;
    	int a1=n.size(),a2=m.size(),a3=max(a1,a2)+1;
    	for (int i=1;i<=a1;i++) a[i]=n[a1-i]-'0';
    	for (int i=1;i<=a2;i++) b[i]=m[a2-i]-'0';
    	for (int i=1;i<=a3;i++)
    	{
    		f[i]+=a[i]+b[i];
    		if (f[i]>=10)
    		{
    			f[i]-=10;
    			f[i+1]++;
    		}
    	}
    	if (f[a3]==0) a3--;
    	for (int i=a3;i>=1;i--) cout<<f[i];
    }
    

    c

    #include <stdio.h>
    int main() 
    {
        int a,b;
        scanf ("%d %d",&a,&b);
        printf ("%d",a+b);
     }
    

    Python 2

    a=input()
    b=input()
    print(a+b)
    

    Python 3

    try:
        while True:
    	    a,b=map(int,input().split(' '))
    	    print(a+b)
    except:
    	pass
    

    还有c++方法!!!

    最短路之Dijkstra c++

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 200020;
    int n, m, h[N], e[N], w[N], ne[N], idx;
    int dist[N];
    bool st[N];
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
    }
    int dijkstra()
    {
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
        heap.push({0, 1});
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        while(!heap.empty())
        {
            auto t = heap.top();
            heap.pop();
            int vel = t.second;
            if(st[vel]) continue;
            st[vel] = true;
            for(int i = h[vel]; i != -1; i = ne[i])
            {
                int j = e[i];
                if(dist[j] > dist[vel] + w[i])
                {
                    dist[j] = dist[vel] + w[i];
                    heap.push({dist[j], j});
                }
            }
        }
        if(dist[3] == 0x3f3f3f3f) return -1;
        return dist[3];
    }
    int main()
    {
        int a, b;
        memset(h, -1, sizeof h);
        cin >> a >> b;
        add(1, 2, a);
        add(2, 3, b);
        int t = dijkstra();
        cout << t;
        return 0;
    }
    

    矩阵乘法 c++

    #include<bits/stdc++.h>
    using namespace std;
    int q, b;
    int a[2][2] = 
    {
        {0, 1},
        {1, 1}
    };
    int n;
    void mo(int f[])
    {
        int ans[2] = {0};
        for(int i = 0; i < 2; i ++)
        {
            for(int j = 0; j < 2; j ++)
            {
                ans[i] += f[j] * a[i][j];
            }
        }
        for(int i = 0; i < 2; i ++) f[i] = ans[i];
    }
    int main()
    {
        cin >> q >> b;
        int f[3] = {q, b};
        for(int i = 1; i <= 1; i ++) mo(f);
        cout << f[1];
        return 0;
    }
    

    递推解法 c++

    #include<bits/stdc++.h>
    using namespace std;
    int f[3];
    int main()
    {
        int a, b;
        cin >> a >> b;
        f[1] = a;
        f[2] = b;
        for(int i = 3; i <= 3; i ++ ) f[i] = f[i - 1] + f[i - 2];
        cout << f[3] << endl;
        return 0;
    }
    

    二分 c++

    #include <bits/stdc++.h>
    using namespace std;
    int a, b;
    int main() {
        scanf("%d%d", &a, &b);
        int l = 0, r = 200000000;
        while (l < r) {
            int mid = l + r >> 1;
            if (mid == a + b) {printf("%d\n", mid); return 0;}
            if (mid <  a + b) l = mid + 1;
            if (mid >  a + b) r = mid - 1;
        }
        cout << l << endl;
        return 0;
    }
    

    前缀和

    #include <bits/stdc++.h>
    using namespace std;
    int a[5], s[5];
    int main() {
        for (int i = 1;i <= 2; i++) scanf("%d", &a[i]), s[i] += a[i] + s[i - 1];
        printf("%d\n", s[2]);
        return 0;
    }
    

    后缀和

    #include <bits/stdc++.h>
    using namespace std;
    int a[5], s[5];
    int main() {
        for (int i = 2;i >= 1; i--) scanf("%d", &a[i]), s[i] += a[i] + s[i + 1];
        printf("%d\n", s[1]);
        return 0;
    }
    

    网络流 c++

    #include<bits/stdc++.h>
    using namespace std;
    #define set(x) Set(x)
    #define REP(i,j,k) for (int i=(j),_end_=(k);i<=_end_;++i)
    #define DREP(i,j,k) for (int i=(j),_start_=(k);i>=_start_;--i)
    #define mp make_pair
    #define x first
    #define y second
    #define pb push_back
    template<typename T> inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0; }
    template<typename T> inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0; }
    typedef long long LL;
    typedef pair<int,int> node;
    const int dmax = 1010, oo = 0x3f3f3f3f;
    int n, m;
    int a[dmax][dmax] , ans;
    int d[dmax], e[dmax];
    priority_queue <node> q;
    inline bool operator >(node a,node b){ return a.y>b.y; }
    bool p[dmax];
    void Set(int x){ p[x] = 1; }
    void unset(int x){ p[x] = 0; }
    bool check(int x){ return x != 1 && x != n && !p[x] && e[x] > 0; }
    void preflow(){
        e[1] = oo;
        d[1] = n - 1;
        q.push(mp(1, n - 1));
        set(1);
        while (!q.empty()) {
            bool flag = 1;
            int k = q.top().x;
            q.pop(), unset(k);
            DREP(i, n, 1)
            if ((d[k] == d[i] + 1 || k == 1) && a[k][i] > 0){
                flag = 0;
                int t = min(a[k][i], e[k]);
                e[k] -= t;
                a[k][i] -= t;
                e[i] += t;
                a[i][k] += t;
                if (check(i)) {
                    q.push(mp(i, d[i]));
                    set(i);
                }
                if (e[k] == 0) break;
            }
            if (flag) {
                d[k] = oo;
                REP(i, 1, n)
                if (a[k][i] > 0) chkmin(d[k], d[i] + 1);
            }
            if (check(k)) {
                q.push(mp(k, d[k]));
                set(k);
            }
        }
        ans = e[n];
    }
    int main() {
        n = 2, m = 2;
        int x, y;
        scanf("%d%d", &x, &y);
        a[1][2] += x + y;
        preflow();
        printf("%d\n", ans);
        return 0;
    }
    

    分块 c++

    #include<bits/stdc++.h>
    using namespace std;
    long long a[50000010], sum[50000010], add[50000010];
    int L[50000010], R[50000010];
    int pos[50000010];
    int n = 1, m = 2, t;
    
    void change(int l, int r, long long d) {
        int p = pos[l], q = pos[r];
        if (p == q) {
            for (int i = l; i <= r; i++) a[i] += d;
            sum[p] += d*(r - l + 1);
        }
        else {
            for (int i = p + 1; i <= q - 1; i++) add[i] += d;
            for (int i = l; i <= R[p]; i++) a[i] += d;
            sum[p] += d*(R[p] - l + 1);
            for (int i = L[q]; i <= r; i++) a[i] += d;
            sum[q] += d*(r - L[q] + 1);
        }
    }
    
    long long ask(int l, int r) {
        int p = pos[l], q = pos[r];
        long long ans = 0;
        if (p == q) {
            for (int i = l; i <= r; i++) ans += a[i];
            ans += add[p] * (r - l + 1);
        }
        else {
            for (int i = p + 1; i <= q - 1; i++)
                ans += sum[i] + add[i] * (R[i] - L[i] + 1);
            for (int i = l; i <= R[p]; i++) ans += a[i];
            ans += add[p] * (R[p] - l + 1);
            for (int i = L[q]; i <= r; i++) ans += a[i];
            ans += add[q] * (r - L[q] + 1);
        }
        return ans;
    }
    
    int main() {
        a[1] = 0;
        t = sqrt(n*1.0);
        for (int i = 1; i <= t; i++) {
            L[i] = (i - 1)*sqrt(n*1.0) + 1;
            R[i] = i*sqrt(n*1.0);
        }
        if (R[t] < n) t++, L[t] = R[t - 1] + 1, R[t] = n;
        for (int i = 1; i <= t; i++)
            for (int j = L[i]; j <= R[i]; j++) {
                pos[j] = i;
                sum[i] += a[j];
            }
        while (m--) {
            int d;
            scanf("%d", &d);
            change(1, 1, d);
        }
        printf("%lld\n", ask(1, 1));
    }
    

    LCT c++

    #include<bits/stdc++.h>
    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",query(A,B));
        return 0;
    }
    

    暴力枚举优化版 c++

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        int a, b; scanf("%d%d", &a, &b);
        for (int i = min(2 * a, 2 * b);i <= max(2 * a, 2 * b); i++)
            if (a + b == i) {
                printf("%d", i); //注意要输出i,不然如果输出a+b循环就没意义了……
                return 0;
            }
    }
    

    极限卡点 c++

    #include <bits/stdc++.h>
    using namespace std;
    int st = clock();
    int main() {
        int a, b; scanf("%d%d", &a, &b);
        while (clock() - st < 995000) {}
        printf("%d", a + b);
        return 0;
    }
    

    Treap(平衡树)c++

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+5,INF=(1ll<<31)-1;
    struct BST
    {
        int l,r,x,p,siz,cnt;
    }a[N];
    int q,p,idx,root;
    int New(int x)
    {
        a[++idx].x=x;
        a[idx].p=rand();
        a[idx].siz=a[idx].cnt=1;
        return idx;
    }
    void Update(int u)
    {
        a[u].siz=a[a[u].l].siz+a[a[u].r].siz+a[u].cnt;
    }
    void Build()
    {
        New(-INF),New(INF);
        root=1,a[1].r=2;
        Update(root);
    }
    int GetRank(int u,int x)
    {
        if(!u) return 0;
        if(x==a[u].x) return a[a[u].l].siz;
        if(x<a[u].x) return GetRank(a[u].l,x);
        else return GetRank(a[u].r,x)+a[a[u].l].siz+a[u].cnt;
    }
    int GetValue(int u,int x)
    {
        if(!u) return INF;
        if(a[a[u].l].siz>=x) return GetValue(a[u].l,x);
        if(a[a[u].l].siz+a[u].cnt>=x) return a[u].x;
        return GetValue(a[u].r,x-a[a[u].l].siz-a[u].cnt);
    }
    void zig(int &u)
    {
        int v=a[u].l;
        a[u].l=a[v].r,a[v].r=u,u=v;
        Update(a[u].r),Update(u);
    }
    void zag(int &u)
    {
        int v=a[u].r;
        a[u].r=a[v].l,a[v].l=u,u=v;
        Update(a[u].l),Update(u);
    }
    void Insert(int &u,int x)
    {
        if(!u)
        {
            u=New(x);
            return;
        }
        if(x==a[u].x)
        {
            a[u].cnt++,Update(u);
            return;
        }
        if(x<a[u].x) 
        {
            Insert(a[u].l,x);
            if(a[u].p<a[a[u].l].p)
                zig(u);
        }
        else
        {
            Insert(a[u].r,x);
            if(a[u].p<a[a[u].r].p)
                zag(u);
        }
        Update(u);
    }
    int GetPrev(int u,int x,int res)
    {
        if(a[u].x>=x)
        {
            if(!a[u].l) return res;
            return GetPrev(a[u].l,x,res);
        }
        if(!a[u].r) return a[u].x;
        return GetPrev(a[u].r,x,a[u].x);
    }
    int GetNext(int u,int x,int res)
    {
        if(a[u].x<=x)
        {
            if(!a[u].r) return res;
            return GetNext(a[u].r,x,res);
        }
        if(!a[u].l) return a[u].x;
        return GetNext(a[u].l,x,a[u].x);
    }
    void Remove(int &u,int x)
    {
        if(!u) return;
        if(x==a[u].x)
        {
            if(a[u].cnt>1)
            {
                a[u].cnt--,Update(u);
                return;
            }
            if(a[u].l||a[u].r)
            {
                if(!a[u].r||a[a[u].l].p>a[a[u].r].p)
                    zig(u),Remove(a[u].r,x);
                else zag(u),Remove(a[u].l,x);
                Update(u);
                return;
            }
            u=0;
            return;
        }
        if(x<a[u].x) Remove(a[u].l,x);
        else Remove(a[u].r,x);
        Update(u);
    }
    int main()
    {
        cin>>q>>p;
        if(q>p) swap(q,p);
        Build();
        Insert(root,q);Insert(root,p);
        Remove(root,p);Insert(root,p);
        cout<<(GetPrev(root,p,-INF)+GetNext(root,q,INF)+GetValue(root,GetRank(root,q)+1)+GetValue(root,GetRank(root,p)+1))/2<<endl;
        return 0;
    }
    

    马占武看我写的代码

    这就是我的实力!哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

    信息

    ID
    1
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    (无)
    递交数
    415
    已通过
    201
    上传者