177 条题解

  • -3
    @ 2024-7-15 22:58:04
    终于可以写一份A+B这么难的题的题解了。
    咦?竟然没有人写LCT的题解?
    Link-Cut Tree 会很伤心的!
    ORZ为了不让LCT伤心于是我来一份LCT的A+B题解吧!
    送上代码:
    
    #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;
    }
    

    本蒟蒻第一次写题解,请原谅

    • @ 2024-7-25 19:06:25

      又一个抄洛谷的🤣👉

  • -3
    @ 2024-4-21 10:05:30

    A+B问题居然没有高精度题解,发一篇吧!

    #include <bits/stdc++.h> using namespace std; const int maxn=250; char sa[maxn],sb[maxn];//定义两个字符串 int a[maxn],b[maxn],c[maxn];//a,b位数字,c进位 int main() { scanf ("%d",sa); scanf ("%d",sb); memset (c,0,sizeof(c));//初始化赋值为0 int la=strlen (sa);//取字符串长度 int lb=strlen (sb); for (int i=0;i<la;i++) a[la-i-1]=sa[i]-'0'; for (int i=0;i<lb;i++) b[lb-i-1]=sb[i]-'0';//将两个字符串类型的数转换成整数 int lc=la>lb?la:lb;//三目运算符,取两个数字中数位最长的数字的数位 for (int i=0;i<lc;i++) { c[i] += a[i]+b[i];//加法运算 if (c[i]>=10) { c[i+1]=a[i]/10; c[i]=c[i]%10;//进位 } } if (c[lc]>0) lc++; int cnt=lc-1; while (c[cnt]==0) cnt--; for (int i=cnt;i>=0;i--) printf ("%d",c[i]); return 0; }

    • -3
      @ 2023-12-26 9:30:44

      这是全站最简单的题,也是所有萌新都会做到的题。

      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      int a,b;
      cin>>a>>b;
      cout<<a+b;
      return 0;
      }
      
      • -3
        @ 2023-12-23 11:24:00
        #include<iostream>
        using namespace std; 
        int main()
        {
            int a,b;
            cin>a>b;
            cout<a+b;
            return 0;
        }
        
        • -3
          @ 2023-12-7 0:26:43
          line = list(map(int,input().split()))
          print(line[0]+line[1])
          
          • -3
            @ 2023-10-27 22:50:04
            #include<bits/stdc++.h>
            using namespace std;
            const int N=100001;
            int a[N],b[N],ml;
            void add(int a[],int b[])
            {
            	int cnt=0;
            	for(int i=1;i<=ml+1;i++)
            	{
            		cnt+=a[i]+b[i];
            		a[i]=cnt%10;
            		cnt/=10;
            	}
            	return;
            }
            void print(int a[])
            {
            	int i;
            	for(i=ml+1;i>0&&!a[i];i--);
            	if(!i)
            	{
            		cout<<0;
            		return;
            	}
            	while(i)
            	{
            		cout<<a[i--];
            	}
            }
            int main()
            {
            	string sa,sb;
            	cin>>sa>>sb;
            	int la=sa.size(),lb=sb.size();
            	ml=la>lb?la:lb;
            	for(int i=1;i<=la;i++)
            	{
            		a[i]=sa[la-i]-'0';
            	}
            	for(int i=1;i<=lb;i++)
            	{
            		b[i]=sb[lb-i]-'0';
            	}
            	add(a,b);
            	print(a);
            	return 0;
            }
            
            • -3
              @ 2023-9-16 12:19:29
              #include <bits/stdc++.h>
              
              using namespace std;
              
              int main(){
                  int a;
                  int b;
                  cin>>a>>b;
                  printf("%d",a+b);
                  return 0;
              
              }
              
              • -3
                @ 2023-9-15 20:33:08

                何不试一试这种C++代码呢?

                #include <iostream>
                #include <string>
                
                // 定义 A+B 的函数
                std::string add(std::string num1, std::string num2) {
                    // 将两个数字字符串转换为数字数组
                    int len1 = num1.length();
                    int len2 = num2.length();
                    int maxLen = std::max(len1, len2);
                    int* arr1 = new int[maxLen];
                    int* arr2 = new int[maxLen];
                    for (int i = 0; i < maxLen; i++) {
                        if (i < len1) {
                            arr1[i] = num1[len1 - i - 1] - '0';
                        } else {
                            arr1[i] = 0;
                        }
                        if (i < len2) {
                            arr2[i] = num2[len2 - i - 1] - '0';
                        } else {
                            arr2[i] = 0;
                        }
                    }
                
                    // 进行加法运算
                    int carry = 0;
                    std::string result = "";
                    for (int i = 0; i < maxLen; i++) {
                        int sum = arr1[i] + arr2[i] + carry;
                        carry = sum / 10;
                        result = std::to_string(sum % 10) + result;
                    }
                    if (carry > 0) {
                        result = std::to_string(carry) + result;
                    }
                
                    delete[] arr1;
                    delete[] arr2;
                
                    return result;
                }
                
                int main() {
                    // 读取输入的两个数字
                    std::string num1, num2;
                    std::cout << "请输入两个数字:" << std::endl;
                    std::cin >> num1 >> num2;
                
                    // 计算并输出结果
                    std::string sum = add(num1, num2);
                    std::cout << "结果为:" << sum << std::endl;
                
                    return 0;
                }
                

                但愿这种代码不会出现TLE或者是MLE 这个代码使用了字符串来表示大数,通过将字符串转换为数字数组,然后进行逐位相加的方式来实现 A+B 的功能。由于使用了字符串和动态数组,代码量较大。但是在实际应用中,这种方式并不是最高效的解决方案。在实际情况下,我们可以使用更简洁和高效的算法来解决这个问题。


                我们可以用更简洁的方法做这道题

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

                上面的代码是进行变量a和b直接相加的代码吗,这种也许就是标准代码。

                好了,这道题的题解到此为止,走过路过,不要错过,点点赞吧~~~么么哒😗🥰

                • -3
                  @ 2023-9-5 17:08:14
                  #include<iostream> //头文件
                  using namespace std; //命名空间
                  int main(){ //主函数,程序从这里开始
                      int a,b; //定义变量
                      cin>>a>>b; //输入
                      cout<<a+b<<endl; //输出他们的和
                      return 0; //主函数需要返回0
                  }
                  
                  • -3
                    @ 2023-7-18 18:53:00
                    #include <bits/stdc++.h>
                    using namespace std;
                    int main() {
                        //以下三行为输入输出超大数据时标准输入输出流的加速
                        ios::sync_with_stdio(false);
                        cin.tie(0);
                        cout.tie(0);
                        long long a,b;
                        cin>>a>>b;
                        cout<<a+b<<endl;
                        return 0;
                    }
                    
                    • -3
                      @ 2023-4-8 20:33:53

                      很简单,线段树模板题。

                      #include <bits/stdc++.h>
                      #define int long long
                      using namespace std;
                      int x, y, a[100005];
                      struct node {
                      	int l, r, w, tag;
                      } tr[400005];
                      inline bool inrange(int l1, int r1, int l2, int r2) {
                      	return (l1 >= l2) && (r1 <= r2);
                      }
                      inline bool outofrange(int l1, int r1, int l2, int r2) {
                      	return (l1 > r2) || (l2 > r1);
                      }
                      inline void pushup(int u) {
                      	tr[u].w = tr[u * 2].w + tr[u * 2 + 1].w;
                      }
                      inline void build(int u, int l, int r) {
                      	if (l == r) {
                      		tr[u].w = a[l];
                      		return;
                      	}
                      	int mid = l + r >> 1;
                      	build(u * 2, l, mid); build(u * 2 + 1, mid + 1, r);
                      	pushup(u);
                      }
                      inline int query1(int u, int l, int r, int p) {
                      	if (l == r) {
                      		return tr[u].w;
                      	} else {
                      		int mid = l + r >> 1;
                      		if (p <= mid) return query1(u * 2, l, mid, p);
                      		else return query1(u * 2 + 1, mid + 1, r, p);
                      	}
                      }
                      inline void update1(int u, int l, int r, int p, int x) {
                      	if (l == r) {
                      		tr[u].w += x;
                      	} else {
                      		int mid = l + r >> 1;
                      		if (mid >= p) update1(u * 2, l, mid, p, x);
                      		else update1(u * 2 + 1, mid + 1, r, p, x);
                      	}
                      }
                      inline void maketag(int u, int len, int x) {
                      	tr[u].tag += x;
                      	tr[u].w += len * x;
                      }
                      inline void pushdown(int u, int l, int r) {
                      	int mid = l + r >> 1;
                      	maketag(u * 2, mid - l + 1, tr[u].tag);
                      	maketag(u * 2 + 1, r - mid, tr[u].tag);
                      	tr[u].tag = 0;
                      }
                      inline int query(int u, int l1, int r1, int l2, int r2) {
                      	if (inrange(l1, r1, l2, r2)) {
                      		return tr[u].w;
                      	} else if (!outofrange(l1, r1, l2, r2)) {
                      		int mid = l1 + r1 >> 1;
                      		pushdown(u, l1, r1);
                      		return query(u * 2, l1, mid, l2, r2) + query(u * 2 + 1, mid + 1, r1, l2, r2);
                      	}
                      }
                      inline void update(int u, int l1, int r1, int l2, int r2, int x) {
                      	if (inrange(l1, r1, l2, r2)) {
                      		maketag(u, r1 - l1 + 1, x);
                      	} else if (!outofrange(l1, r1, l2, r2)) {
                      		int mid = l1 + r1 >> 1;
                      		pushdown(u, l1, r1);
                      		update(u * 2, l1, mid, l2, r2, x);
                      		update(u * 2 + 1, mid + 1, r1, l2, r2, x);
                      		pushup(u);
                      	}
                      }
                      signed main() {
                          cin >> x >> y;
                          build(1, 1, 1);
                          update1(1, 1, 1, 1, x);
                          update1(1, 1, 1, 1, y);
                          cout << query1(1, 1, 1, 1);
                          return 0;
                      }
                      
                    • -3
                      @ 2023-3-23 20:06:20
                      #include<bits/stdc++.h>//万能头文件
                      using namespace std;//使用标准命名空间
                      int main()//主函数
                      {
                      	int a,b;
                      	cin>>a>>b;
                      	cout<<a+b;
                      	return 0;
                      }
                      ~~太简单了~~
                      
                      • -3
                        @ 2023-3-20 15:48:05

                        很简单,先用字符串存放数据,在从低位开始算,两两相加,逢十进一。

                        ~代码才40行,不多~

                        #include<bits/stdc++.h>
                        using namespace std;
                        int a[1000001],b[1000001],c[1000001],j;
                        bool x=false;
                        char s[1000001],ss[1000001];
                        int main() 
                        {
                        	memset(a,0,sizeof(a));
                        	memset(b,0,sizeof(b));
                        	memset(c,0,sizeof(c));
                        	scanf("%s%s",s,ss);
                        	a[0]=strlen(s);
                        	b[0]=strlen(ss);
                        	for(int i=1; i<=a[0]; i++) 
                        	a[i]=s[a[0]-i]-'0';
                        	for(int i=1; i<=b[0]; i++) 
                        	b[i]=ss[b[0]-i]-'0';
                        	for(j=1; j<=max(a[0],b[0])+1; j++) 
                        	{
                        		c[j]=a[j]+b[j];
                        		if(c[j]>=10) 
                        		{
                        			c[j]%=10;
                        			a[j+1]++;
                        		}
                        	}
                        	c[0]=j;
                        	if(c[j+1]>0) c[0]++;
                        	for(int i=c[0]; i>=1; i--) 
                        	{
                        		if(x==false&&c[i]==0) 
                        		continue;
                        		x=true;
                        		cout<<c[i];
                        	}
                        	if(x==false) 
                        	cout<<0;
                        	printf("\n");
                        	return 0;
                        }
                        

                        管理员大大求过QAQ

                        彩蛋

                        文言版

                        #及充窦融之女孙侍中垒兴矣。
                        用命名为帝喾咨d
                        甲戌,武军二千人[71]以五十吏二千人,吏二千人五十余年;
                        &lt;
                        甲戌,封府库二千二千余家,帝喾二千余家;
                        &lt
                        {
                        更老女喜刑名。帝喾寿梦有子;
                        更典帝喾嘉伯喈①,吾寿梦有子②;
                        更典帝喾嘉伯喈。
                        帝喾八月壬戌岂可怒哉!帝喾聿怀金玉,为帝喾次妃。
                        [47]陈民典诰。
                        [47]民义并行。
                        猫一窦秋开二水中;
                        [88]唐赛明之后[82]--][57]
                        猫一窦秋开二水中;
                        [88]唐广明年金涂海伯萧][57]-
                        刘保好心,好梦回漠北;无如之何。
                        {
                        [88]刘宇深,[88];
                        吾乃龌龊。
                        {
                        [88]徐卢=;
                        [88]唐武爱吾;
                        }
                        }
                        [88]
                        [88]聚野草花,疏刑部伍胥靡];
                        了窦融一窦通漾场;
                        {
                        若孝明旦唱为帝喾做成何人,
                        引兵久之。
                        蜥蜴_然;
                        功名吟泽,无片云;
                        }
                        若(旦旦唱)
                        co mán)42 30。
                        乐则灵惨凄部(lya n.
                        还。
                        }
                        
                        • @ 2023-7-29 12:46:32

                          a,b106a,b\le 10^6,用传统数据类型不能存吗?为什么用高精度加?

                        • @ 2023-10-3 19:26:30

                          @ 他炫技呗,别管这种人

                      • -3
                        @ 2023-3-3 18:49:59

                        这题很简单,输入 aa 和 bb,然后输出他们的和即可。

                        #include <stdio.h>
                        
                        int main()
                        {
                            int a,b;
                            scanf("%d%d",&a,&b);
                            printf("%d\n", a+b);
                            return 0;
                        }
                        
                        • -3
                          @ 2023-3-3 14:47:23

                          很简单,适合刚学信息的同学。

                          代码:

                          
                          
                          #include<iostream>//头文件
                          using namespace std;
                          
                          int main()//主函数
                          {
                          int a,b;//定义a和b
                          cin>>a>>b;//输入
                          cout<<a+b<<endl;//输出a+b并换行
                          return 0;//程序结束后,返回0,也可以不写```}
                          • -3
                            @ 2022-12-29 20:42:45
                            #include<iostream>
                            #define I int a,b;
                            #define AK cin>>a>>b;
                            #defing IOI cout<<a+b
                            using namespace std;
                            int main()
                            {
                                I AK IOI;
                                return 0;
                            }
                            
                            • -3
                              @ 2022-8-18 17:11:48

                              甚至连变量都不用的快读。

                              代码如下:

                              #include<bits/stdc++.h>
                              using namespace std;
                              inline int read()
                              {
                                  int x=0;
                                  bool flag=1;
                                  char c=getchar();
                                  while(c<'0'||c>'9')
                                  {
                                      if(c=='-')
                                          flag=0;
                                      c=getchar();
                                  }
                                  while(c>='0'&&c<='9')
                                  {
                                      x=(x<<1)+(x<<3)+c-'0';
                                      c=getchar();
                                  }
                                  return (flag?x:~(x-1));
                              }
                              int main()
                              {
                              	cout<<read()+read();
                              	return 0;
                              }
                              
                              • -3
                                @ 2022-8-17 16:04:20
                                #include<bits/stdc++.h>//万能头文件
                                using namespace std;
                                int main()//主函数
                                {
                                	int a,b;
                                	cin>>a>>b;
                                	cout<<a+b;
                                }
                                
                                • -3
                                  @ 2022-5-7 10:36:51

                                  这题很简单,输入 a 和 b,然后输出他们的和即可。

                                  #include <bits/stdc++.h>
                                  using namespace std;
                                  
                                  int main()
                                  {
                                          int a,b;
                                          cin >> a >> b;
                                          cout << a + b;
                                  	
                                  	
                                  
                                  	return 0;
                                  }
                                  
                                  • -3
                                    @ 2022-3-22 12:54:54

                                    这道题目就是输出 a+b 的和就OK了! AC Code:

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

                                    注:我是新手,请多多请教!

                                    • @ 2022-6-8 9:14:48

                                      我不理解这种题解为什么会有人踩

                                    • @ 2022-7-25 16:29:54

                                      @因为没解释!

                                  信息

                                  ID
                                  56
                                  时间
                                  1000ms
                                  内存
                                  1024MiB
                                  难度
                                  1
                                  标签
                                  递交数
                                  10369
                                  已通过
                                  4685
                                  上传者