Elementary Arithmetic

Limits 1s, 512 MB

When Shakib was little, his mother used to help him with his studies. Usually, around 7 PM in the evening, Shakib would jump to his bed, keeping the small exercise books and a green pen in his hands. His mother would stay around and would help him when it is necessary. In one such says, Shakib came to know about adding numbers. First, he learned to add 1 to any single digit numbers. So, 1 and 5 becomes 6, seven and one becomes 8 and so on. Then he learned how to add two one digit numbers. Soon his exercise books became filled with addition of two one digit numbers. Problems like what is 5 plus 7 became bread and butter for Shakib. Shakib continued his journey of acquiring knowledge for a long time and now he has come to mental state where he can learn things himself. Shakib now know programming and solves many difficulty problems.


This is a companion discussion topic for the original entry at https://toph.co/p/elementary-arithmetic

Exactly what data type do we have to use here?

You need to use array.

Can anyone tell me why am I getting runtime error?
Here’s my code:
#include
#include<string.h>
using namespace std;

string A,B;
int main(){

cin>>A>>B;
long long int i,k=0;
long long int m,n,l,t[999999],f=0;
m=A.size();
n =B.size();
if(m>n){
    l=n-1;
    for(i=m-1;i>=m-n;i--){
      B[i]=B[l];
        l--;
        k++;
    }
    for(i=0;i<m-n;i++){
    B[i]='0';
}
    l=m;
}else{
    l=m-1;
    for(i=n-1;i>=n-m;i--){
        A[i]=A[l];
        l--;
        k++;
    }
    l=n;
    for(i=0;i<n-m;i++){
    A[i]='0';
}
}
for(i=l-1;i>=0;i--){
    
    t[i]=A[i]-'0'+B[i]-'0'+f;
    f=0;
    if(t[i]>9){
        t[i]%=10;
        f=1;
    }
}
if(f==1){
    printf("1");
}
for(i=0;i<l;i++){
    printf("%lld",t[i]);
}
return 0;

}

Well actually the range of long long int is 10^18 while in the problem the range is 10^1000000
i hope you understand now

Increase the runtime for python. Else it can’t be solved by python. @hjr265

Have you tried using Bignum?.

@touhidur
I use python 3.7 and python 3.7 uses bignum or int based on the input as far as I know.

Yes, I know the same.
However, I tried to use the Bignum utility of both Java and Python but it seems that the code will not work unless you store the number in a string or an array. You will get TLE instead.
Maybe, the bignum algo they use is too time-consuming.
So, you have to learn bignum yourself. forget those,

We’re you able to solve this bro @touhidur??

Yup.
With C++.
Using handwritten algo for bignum.

No I mean can you solve this with python?

Why my code get CLE?

def findSum(str1, str2):  
    if (len(str1) > len(str2)): 
        t = str1; 
        str1 = str2; 
        str2 = t
    str = ""
  
    # Calculate length of both string  
    n1 = len(str1) 
    n2 = len(str2)  
  
    # Reverse both of strings  
    str1 = str1[::-1] 
    str2 = str2[::-1]
  
    carry = 0
    for i in range(n1):
        sum = ((ord(str1[i]) - 48) + 
              ((ord(str2[i]) - 48) + carry)) 
        str += chr(sum % 10 + 48) 
  
        # Calculate carry for next step  
        carry = int(sum / 10);  
  
    # Add remaining digits of larger number  
    for i in range(n1, n2):  
        sum = ((ord(str2[i]) - 48) + carry) 
        str += chr(sum % 10 + 48) 
        carry = (int)(sum / 10) 
  
    # Add remaining carry  
    if (carry):  
        str += chr(carry + 48) 
  
    # reverse resultant string  
    str = str[::-1]
    return str

a,b = map(int,input().split())
str1 = str(a)  
str2 = str(b)  
print(findSum(str1, str2));  

I tried to use bignum too. but it didn’t bring me AC.

Why wrong answer?


#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<climits>
#include<cstring>
#include<cmath>
#include<unordered_map>
#include<sstream>
#include<cmath>
#include<iomanip>
#include<numeric>
using namespace std;

int main(){
	long long int a,b;
	cin>>a>>b;
	long long int c=a+b;
	cout<<c;
}

edit:I use C++ latest version in my mac but this code executes but here this doesn't!!!