[java]
/* @JUDGE_ID: 50020MM 10035 java */
import java.io.*;
import java.util.*;
class Main
{
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
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));
}
public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin()
{
String input;
StringTokenizer idata;
int N;
int stevec = 0;
int M = 0;
int prenesi = 0;
int vsota = 0;
int stevilo = 0;
input = Main.ReadLn (255);
N = Integer.parseInt(input.trim());
input = Main.ReadLn (255);
while (stevec < N)
{
prenesi = 0;
input = Main.ReadLn (255);
M = Integer.parseInt(input.trim());
if((N == 0) &&(M == 0))break;
int tab1 [] = new int[M];
int tab2 [] = new int[M];
int rezultat [] = new int[M];
for (int i=0; i < M; i++)
{
input = Main.ReadLn (255);
idata = new StringTokenizer (input.trim());
int a = Integer.parseInt(idata.nextToken());
int b = Integer.parseInt(idata.nextToken());
vsota = a + b;
rezultat[i] = vsota;
}//for
for(int i = M-1; i >= 0; i--){
rezultat[i] = rezultat[i]+prenesi;
if((rezultat[i]) >= 10){
prenesi = 1;
rezultat[i] = rezultat[i]%10;
}//if
else{
rezultat[i] = rezultat[i];
prenesi = 0;
}//else
}//for
stevec++;
System.out.println();
for(int k = 0; k < M; k++){
System.out.print(rezultat[k]);
}
System.out.println("\n");
} //while
} //Begin()
} //class
/* @END_OF_SOURCE_CODE */
[/java]
Do you have to read all the blocks and then you print the answer, or you read one block at the time. Can you do like this???
input
2
4
1 2
3 4
5 5
4 1
output
3805
input
3
9 8
6 2
1 1
output
782
Thank you for you help!!!!
:D :D :D
thanks sohel i got accepted.
thanks a lot
but problem description says
"The first line of a input file is an integer N, then a blank line followed by N input blocks. The first line of an each input block contains a single number M (1<=M<=1000000)
No, the problem statement and the inputset are correct. My AC program wouldn't work if that was not so.
Sohel, your testcase is illegal. Your program got AC because the input doesn't contain cases like that.
The problem in "emotional blind"'s program is the statement[c] for(j=m;j>=0;j--){
p[j]=p[j]+carry;
carry=p[j]/10;
p[j]=p[j]%10;
}
[/c]which will access p[1000000] for m==1000000, and that is out of bounds and leads to eratic behaviour.
Change it to[c]for(j=m-1;j>=0;j--){
...[/c]
and the above program gets accepted.
Once again you are right...
.. I actually sent emotional blind's code by modifying the part I have mentioned and increasing the array size to 1000100 and got AC and so I thought it was because of amending the former part that caused the code to to get ACed.
I later just increased the array size and that got AC too and it infers that my suggestions/ideas were a little of the track.
Sorry for any confusion that I might have created and thanks to LJ for correcting the wrong information that are posted in this forum.
#include <cstdio>
#include <cmath>
using namespace std;
#define MAXDIGITS 1000101 /* maximum length bignum */
typedef struct {
char digits[MAXDIGITS]; /* represent the number */
long lastdigit; /* index of high-order digit */
} bignum;
void print_bignum(bignum *n)
{
long i;
for (i=n->lastdigit; i>=0; i--)
printf("%c",'0'+ n->digits[i]);
printf("\n");
}
void long_to_bignum(long s, bignum *n)
{
long i; /* counter */
long t; /* long to work with */
for (i=0; i<MAXDIGITS; i++) n->digits[i] = (char) 0;
n->lastdigit = -1;
t = s;
while (t > 0) {
n->lastdigit ++;
n->digits[ n->lastdigit ] = (t % 10);
t = t / 10;
}
if (s == 0) n->lastdigit = 0;
}
void initialize_bignum(bignum *n)
{
long_to_bignum(0,n);
}
long max(long a, long b)
{
if (a > b) return(a); else return(b);
}
void zero_justify(bignum *n)
{
while ((n->lastdigit > 0) && (n->digits[ n->lastdigit ] == 0))
n->lastdigit --;
}
void add_bignum(bignum *a, bignum *b, bignum *c)
{
long carry; /* carry digit */
long i; /* counter */
initialize_bignum(c);
c->lastdigit = max(a->lastdigit,b->lastdigit)+1;
carry = 0;
for (i=0; i<=(c->lastdigit); i++) {
c->digits[i] = (char) (carry+a->digits[i]+b->digits[i]) % 10;
carry = (carry + a->digits[i] + b->digits[i]) / 10;
}
zero_justify(c);
}
int main()
{
long N,digi,temp,first1,first2;
bignum n1,n2,n3;
scanf("%d",&N);
while(N--)
{
first1 = first2 = 1;
initialize_bignum(&n1);
initialize_bignum(&n2);
scanf("%d",&digi);
n1.lastdigit = n2.lastdigit = digi - 1;
for(long i = 0;i < digi;i++)
{
scanf("%d",&temp);
if(temp == 0 && first1) {n1.lastdigit--;first1 = 0;}//delete the pre 0s
else n1.digits[digi - 1 - i] = temp;
scanf("%d",&temp);
if(temp == 0 && first2) {n2.lastdigit--;first2 = 0;}
else n2.digits[digi - 1 - i] = temp;
}
add_bignum(&n1,&n2,&n3);
print_bignum(&n3);
printf("\n");
}
return 0;
}
thanks
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius