10013 - Super long sums
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10013 - Super Long Sums
There is a blank line between output blocks.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 33
- Joined: Tue Apr 27, 2004 7:41 pm
- Location: Santa Clara / Mountain View, CA, USA
- Contact:
Re:
This is an invalid input, becausetenshi wrote:check this case:
1
4
0 9
0 9
0 9
1 9
Each of the two given integers is not less than 1, and the length of their sum does not exceed M.
I Believe I Can - leestime.com
Re: 10013 - Super long sums
Getting RE. Please help:
#include <iostream>
using namespace std;
int main()
{
long long int a,b,c,num[10000],num1[10000],i=0,sum=0,k=0,res[10000];
cin>>a;
while(a--)
{
cin>>b;
while(b--)
{
cin>>num;
cin>>num1;
i++;
}
for(int j=i-1;j>-1;j--)
{
c=num[j]+num1[j];
if(c>9 && j!=0)
{
res[k]=c-10;
k++;
num[j-1]=num[j-1]+1;
}
else
{
res[k]=c;
k++;
}
}
for(int j=k-1;j>-1;j--)
cout<<res[j];
cout<<endl;
if(a!=0)
cout<<endl;
sum=0;i=0,k=0;
}
return 0;
}
Thanks in advance
#include <iostream>
using namespace std;
int main()
{
long long int a,b,c,num[10000],num1[10000],i=0,sum=0,k=0,res[10000];
cin>>a;
while(a--)
{
cin>>b;
while(b--)
{
cin>>num;
cin>>num1;
i++;
}
for(int j=i-1;j>-1;j--)
{
c=num[j]+num1[j];
if(c>9 && j!=0)
{
res[k]=c-10;
k++;
num[j-1]=num[j-1]+1;
}
else
{
res[k]=c;
k++;
}
}
for(int j=k-1;j>-1;j--)
cout<<res[j];
cout<<endl;
if(a!=0)
cout<<endl;
sum=0;i=0,k=0;
}
return 0;
}
Thanks in advance

-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10013 - Super long sums
1<=M<=1000000
Check input and AC output for thousands of problems on uDebug!
Re: 10013 - Super long sums
is using java BigInteger TLE? I can't seem to optimize a java solution enough to pass the time limit.
all that matters is AC
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10013 - Super long sums
Try solving it without using BigInteger, just sum and print using int arrays.
Check input and AC output for thousands of problems on uDebug!
Re: 10013 - Super long sums
I getting WA...
Please help me...
Please help me...
Code: Select all
#include<iostream>
//#include<cstdio>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int n,m,carry,j;
short temp;
cin>>n;
for(int i=0;i<n;i++)
{
if(i) cout<<endl;
carry=0;
cin>>m;
short int a[m],b[m],ans[m];
for(j=m-1;j>=0;j--)
cin>>a[j]>>b[j];
for(j=0;j<m;j++)
{
temp=a[j]+b[j];
ans[j]=temp%10+carry;
carry=temp>9;
}
for(j=m-1;j>=0;j--)
cout<<ans[j];
cout<<endl;
}
return 0;
}
Re: 10013 - Super long sums
Input
Acc Output
Code: Select all
1
3
1 2
7 2
2 8
Code: Select all
400
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 10013 - Super long sums
Thanks I got AC.. It was a silly mistake though.. I wasn't carefull..
Thanks again..
Thanks again..

-
- New poster
- Posts: 8
- Joined: Mon Mar 10, 2014 8:18 am
Re: 10013 - Super long sums
Removed. Solved.



-
- New poster
- Posts: 10
- Joined: Sat Sep 06, 2014 12:44 pm
Re: 10013 - Super long sums
Please help me. I got time limit.
thanks in advance. 

Code: Select all
import java.util.Scanner;
public class Main {
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
int n, m;
short temp, carry = 0;
char ch;
short[] firstAra = new short[1000005];
short[] secondAra = new short[1000005];
while (scanner.hasNext()) {
n = scanner.nextInt();
for (int j = 0; j < n; j++) {
m = scanner.nextInt();
for (int k = 0; k < m; k++) {
firstAra[k] = scanner.nextShort();
secondAra[k] = scanner.nextShort();
}
String string = "";
carry = 0;
for (int k = m - 1; k >= 0; k--) {
temp = (short) (firstAra[k] + secondAra[k] + carry);
if (temp > 9) {
temp = (short) (temp % 10);
carry = 1;
} else {
carry = 0;
}
ch = (char) temp;
ch += 48;
string = string + ch;
}
if (carry == 1) {
string = string + 1;
}
StringBuffer buffer = new StringBuffer(string);
System.out.println(buffer.reverse() + "\n");
}
}
}
}

-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10013 - Super long sums
Try using BufferedReader and BufferedWriter.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 4
- Joined: Thu Aug 21, 2014 2:59 am
Re: 10013 - Super long sums
I don't understand why my code is giving me a submission error. It seems to work on the sample cases and what I can find in this forum. Can someone please explain why this is occuring? Thank you for your help.
Code: Select all
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int trials = in.nextInt();
StringBuilder a;
StringBuilder b;
int l;
for (int tr = 1; tr <= trials; tr++) {
a=new StringBuilder("");
b=new StringBuilder("");
l=in.nextInt();
for(int j=0;j<l;j++)
{
a=a.append(in.next());
b=b.append(in.next());
}
BigInteger z = new BigInteger(a.toString());
BigInteger x = new BigInteger(b.toString());
System.out.println(z.add(x));
if(tr!=trials)
System.out.println("");
}
}
}
Re: 10013 - Super long sums
Sometimes it happens. Try it again. Judge gives TLE for your code.
brianfry713 wrote:Try using BufferedReader and BufferedWriter.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 10013 - Super long sums
it is said that "each of the two given integers is not less than 1"..... but in sample input there is given 0 as one of the integers..... is there anything i understood wrong???