## 10013 - Super long sums

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am
I'm sorry.
Above my response may be wrong because of this problem description :
Each of the two given integers is not less than 1
Best regards.

Andisheh
New poster
Posts: 5
Joined: Fri Aug 26, 2005 11:29 pm

### 10013 Why RTE??

this is my code ,I dont know Why I got RunTimeError!!!
can you help me!?

#include <iostream.h>
//#include <fstream.h>
#include <string.h>

char a[3][10000000];

void main()
{
int i,j,k,n,m,carry;
//ifstream f("10013.in");
cin>>m;
for(j=0 ; j<m ; j++)
{
cin>>n;
for(k=0 ; k<n ; k++)
{
cin>>a[0][k]>>a[1][k];
}
carry=0;
for(i=n-1;i>=0;i--)
{
k=a[0]-'0'+a[1]-'0'+carry;
a[3]=k%10+'0';
carry=k/10;
}
a[3][n]=0;
cout<<a[3]<<endl;
}
// cin>>i;
}

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

### Re: 10013 Why RTE??

Your code tried to access the out of memory.
After you change the code like following, you will be able to aboid RTE.

a[3] -----> a[2]

And, don't forget to print blank lines for avoiding Presentation Error.
There is a blank line between output blocks.
Best regards.

Andisheh
New poster
Posts: 5
Joined: Fri Aug 26, 2005 11:29 pm
,what a fully mistake I had done,thank you so much

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Location: bangladesh
Contact:

### 10013:pls help RTE

i don't understand why RTE i used array size larger than 1000000 :

#include<stdio.h>
#include<string.h>

int main()
{
char ch[1000010];
long int i,j[1000010],k[1000010],n,carry,m;

scanf("%ld",&n);

for(;n>0;n--){

scanf("%ld",&m);
for(i=0;i<=m;i++) ch='0';
carry=0;
for(i=0;i<m;i++) scanf("%ld %ld",&j,&k);
for(i=m;i>0;i--){ch=((j[i-1]+k[i-1]+carry)%10)+'0';carry=(j[i-1]+k[i-1]+carry)/10;}
ch[0]=carry+'0';
if(carry==0) i=1;
else i=0;

for(;i<=m;i++) printf("%c",ch);
printf("\n");
}

return 0;

}
aaa

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

### Re: 10013:pls help RTE

59557RC wrote:i don't understand why RTE i used array size larger than 1000000 :

- - - - -

char ch[1000010];
long int i,j[1000010],k[1000010],n,carry,m;

- - - - -
Your code outputs "Segmentation fault" on my local machine.
I think that it secures the memory too much.
Try to decrease the amount of use of the memory.
There is a blank line between output blocks.
And, add above.

Best regards.

royjp
New poster
Posts: 2
Joined: Sun Dec 05, 2004 1:18 am
try combining these functions

static final String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
static final String ReadText (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (System.in.available()>0)
{
car = System.in.read();
if ((car < 0)) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

something like
String input;
int numberOfSums;

String linha ;
linha = ReadLn(50);
numberOfSums=Integer.parseInt(linha.trim());

input = Main.ReadText(5000000);
StringTokenizer linhas= new StringTokenizer(input,"\n");

athlon19831
New poster
Posts: 20
Joined: Thu Jan 19, 2006 2:32 pm
how to get P.E

SHAHADAT
New poster
Posts: 23
Joined: Thu Jun 22, 2006 8:55 am
Location: sust,bangladesh

### Re: 10013 Accepted (P.E.)

[quote="JuaingFall"]who can help me? Thanks

[code
deleted

Uttam Dwivedi
New poster
Posts: 5
Joined: Fri Jun 30, 2006 10:02 pm

### 10013

the code has solved the problem but still showing presentation error...
plzzz find it out ..Bcoz some other problems too have this error....

#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N,i=0;
cin>>N;
while(i<N)
{int carry=0,M,A,B,j=0;
cin>>M;
vector<int> a,b;
while(j<M)
{cin>>A>>B;
a.push_back(A);
b.push_back(B);
j++;
}
int s;
for(j=M-1;j>=0;j--)
{s=a[j]+b[j]+carry;
a[j]=s%10;
if(s>9)
carry=1;
else
carry=0;
}
for(j=0;j<M;j++)
cout<<a[j];
cout<<"\n\n";
i++;
}

return 0;
}

plzzz help....
thanx in advance....

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
edited
Last edited by helloneo on Wed Sep 17, 2008 6:43 am, edited 1 time in total.

Uttam Dwivedi
New poster
Posts: 5
Joined: Fri Jun 30, 2006 10:02 pm

### @dhello neo

Thanx a lot dude.....
i got all my probs wid P.E. submitted.

aswin34
New poster
Posts: 4
Joined: Tue Nov 07, 2006 8:32 am

### 10013 WA

I have tried all the test cases on this forum for 10013, but I still keep getting WA. Does anyone have any suggestions on as to how I could fix this code?

[code]
#include <stdio.h>

int num1[1000001], num2[1000001];
int result[1000001];

void addNumbers()
{
for(int i = 0; i < 1000001; i++)
{
result[i] = -1;
}

int value = 0;
int carry = 0;
for(int i = 1000000; i >= 0; i--)
{
if(num1[i] == -1 && num2[i] != -1)
{
value = num2[i] + carry;
}
else if(num1[i] != -1 && num2[i] == -1)
{
value = num1[i] + carry;
}
else if(num1[i] != -1 && num2[i] != -1)
{
value = num1[i] + num2[i] + carry;
}
else
{
if(carry == 1)
{
result[i] = 1;
}

break;
}

if(value > 9)
{
result[i] = value - 10;
carry = 1;
}
else
{
result[i] = value;
carry = 0;
}
}

for(int h = 0 ; h < 1000000; h++)
{
if(result[h] != -1)
{
printf("%d", result[h]);
}
}

printf("\n");
}

int main()
{
int numTests;
int length;

scanf("%d", &numTests);
printf("num tests %d\n", numTests);

for(int i = 0; i < numTests; i++)
{
for(int j = 0; j < 1000000; j++)
{
num1[j] = -1;
num2[j] = -1;
}

scanf("%d", &length);

int first, second;
for(int k = 0; k < length; k++)
{
scanf("%d", &first);
scanf("%d", &second);
num1[1000000 - length + k] = first;
num2[1000000 - length + k] = second;
}

addNumbers();
}
}
[/code][/code]

Steven Luck
New poster
Posts: 13
Joined: Sat Dec 02, 2006 7:51 am
Location: Indonesia
Contact:

### Re: 10013 WA

I've tried using:

Code: Select all

``````2

4
0 4
4 2
6 8
3 7

3
3 0
7 9
2 8
``````
Ur code gives the output:

Code: Select all

``````num tests 2
4750
470
``````
Expected output:

Code: Select all

``````4750

470
``````
"The Only Thing For The Triumph of Evil is For a Good Man to Do Nothing"

nlr
New poster
Posts: 1
Joined: Fri Feb 16, 2007 4:10 am

### 10013 - Toplist Runtimes

I am just wondering if the top solutions to Problem (130013) are clean solutions (that is, every valid Judge (that is a Judge that only refuses wrong answers) would accept them) or if it are "hacks" that try to trick the official Judge - i.e. because they know something about the testcases and take advantage of it?

more precisely: would their code score well with other test cases as well?