Page not loading? Try clicking here.
Placeholder

#1761

Number Baseball 1s 32MB

Problems

Yeongsu and Minhyuk, members of the gifted information club, are playing Number Baseball during a break.

Rules of the game:

  • Yeongsu thinks of a 3-digit number consisting of distinct digits from 1 to 9 (e.g., 324).

  • Minhyuk guesses a 3-digit number (also with distinct digits from 1 to 9) and asks Yeongsu (e.g., 123).

  • Yeongsu counts the result as follows:

    • If a digit in Minhyuk’s number matches both value and position, it counts as a strike.

    • If a digit is in Yeongsu’s number but in a different position, it counts as a ball.

Examples:

  • Yeongsu has 324:

    • Guess 429 → 1 strike, 1 ball

    • Guess 241 → 0 strikes, 2 balls

    • Guess 924 → 2 strikes, 0 balls

The game ends when Minhyuk guesses the number exactly (3 strikes). Otherwise, he continues guessing.


Task

Given the history of guesses and Yeongsu’s responses, determine how many numbers could still be Yeongsu’s number.

Example:

  • Minhyuk: 123 → 1 strike, 1 ball

  • Minhyuk: 356 → 1 strike, 0 balls

  • Minhyuk: 327 → 2 strikes, 0 balls

  • Minhyuk: 489 → 0 strikes, 1 ball

Possible answers: 324 and 328 → total 2 numbers.

Yeongsu always answers honestly, so there is no contradiction in the input.


Input

The first line contains N (1 ≤ N ≤ 100), the number of guesses.

The next N lines each contain:

  • A 3-digit number guessed by Minhyuk

  • The number of strikes

  • The number of balls

All three integers are separated by spaces.


Output

Print a single integer: the total number of numbers that could possibly be Yeongsu’s number.


Example

4 

123 1 1
356 1 0
327 2 0
489 0 1
2


Source

KOI 본선 2008 초3

You must sign in to write code.