## 12765 - Factorial Products

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

Moderator: Board moderators

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

### 12765 - Factorial Products

I am struggling to get my programming to read the input for this seemingly simple problem. Here is my Java code that throws an Exception while only trying to read the input. Are there any tricks to reading the input? Any insights would be much appreciated.

Code: Select all

``````import java.io.*;
import java.util.*;

class Main
{
public static void main(String [] args) throws Exception
{
Main main = new Main();

boolean done = false;
while(!done)
{
String line = in.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
int numA = Integer.parseInt(tokenizer.nextToken());
int numB = Integer.parseInt(tokenizer.nextToken());
if((numA == 0) && (numB == 0))
done = true;
else
{
tokenizer = new StringTokenizer(line);
int [] countsA = new int [10];
for(int i=0; i<numA; i++)
countsA[Integer.parseInt(tokenizer.nextToken())]++;

tokenizer = new StringTokenizer(line);
int [] countsB = new int [10];
for(int i=0; i<numB; i++)
countsB[Integer.parseInt(tokenizer.nextToken())]++;

//main.solve(countsA, countsB);
}
}

in.close();
}

public Main()
{
}
}
``````
LL Cool Jay
The Formula Wizard
Jason Winokur

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 12765 - Factorial Products

Remove line 39:
in.close();
Check input and AC output for thousands of problems on uDebug!

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

### Re: 12765 - Factorial Products

Thanks for the reply, but it still throws an exception parsing the input. Any other suggestions?
LL Cool Jay
The Formula Wizard
Jason Winokur

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

### Re: 12765 - Factorial Products

I think my code handles any integers for N and M. Should I terminate the program if either N or M is outside the [1..1000] range? Do I have to parse N and M as if they could contain commas, e.g. "1,000 1,000"? The problem description says that the input will end with a line containing two zeros. I assume that means something line this: "0 0". Is there anything special about parsing the N and M values in Java? How would you parse the input in Java? Thanks again.
Jason Winokur
LL Cool Jay
The Formula Wizard
Jason Winokur

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 12765 - Factorial Products

The judge's input may be missing the two zeros following the final line. I got AC using C++ and scanf("%d") to read all input values. Try reading tokens from the input rather than reading line by line.

Code: Select all

``````import java.io.*;
import java.util.*;

class Main
{
static StringTokenizer tokenizer;
static int get_int() throws Exception {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = in.readLine();
if(line == null)
return 0;
StringTokenizer tokenizer = new StringTokenizer(line);
}
return Integer.parseInt(tokenizer.nextToken());
}

public static void main(String [] args) throws Exception
{
Main main = new Main();
tokenizer = null;

boolean done = false;
while(!done)
{
int numA = get_int();
int numB = get_int();
if((numA == 0) && (numB == 0))
done = true;
else
{
int [] countsA = new int [10];
for(int i=0; i<numA; i++)
countsA[get_int()]++;

int [] countsB = new int [10];
for(int i=0; i<numB; i++)
countsB[get_int()]++;

//main.solve(countsA, countsB);
}
}

//in.close();
}

public Main()
{
}
}
``````
Check input and AC output for thousands of problems on uDebug!

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

### Re: 12765 - Factorial Products

You are a genius! I modified my program the way you suggested and now I get Accepted. I wonder what the judges input really looks like. Should I mention it to the administrators in case other people are getting tripped up? Thanks again!
Jason Winokur
LL Cool Jay
The Formula Wizard
Jason Winokur

Repon kumar Roy
Learning poster
Posts: 96
Joined: Tue Apr 23, 2013 12:54 pm

### Re: 12765 - Factorial Products

Give me some critical test cases .... I am getting WA ..
I used prime factorization of the factorial ..

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

### Re: 12765 - Factorial Products

Here's mine. I think the input does not follow the description 100%, so make sure you follow Brian's advice.
Input

Code: Select all

``````1 1
9
9
1 2
5
3 6
1 3
9
1 9 0
1 4
9
7 3 3 2
2 4
6 6
5 3 5 3
10 17
0 1 2 3 4 5 6 7 8 9
7 7 7 5 5 3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ard
Jason Winokur

Repon kumar Roy
Learning poster
Posts: 96
Joined: Tue Apr 23, 2013 12:54 pm

### Re: 12765 - Factorial Products

Thanks .... jaywinyeah
got AC