Unpleasant pairs of digits

2.3

7 votes
Binary Search, Implementation, Dynamic Programming, Algorithms, 4D dynamic programming
Problem

Alice has a set of N unpleasant pairs of digits.

Consider a number bad if it satisfies the following conditions:

  • Contains a pair of digits that goes in a row
  • It is unpleasant

Otherwise, the number is considered good.

Your task is to determine K ascending good numbers.

Input format

  • The first line contains one integer T that denotes the number of test cases.
  • The first line of the test case contains two integers N and K.
  • The next N lines contain two digits d1,d2.

Output format

Print T lines. The ith line must contain an answer for the ith test case.

Note: If answer is more than 5×1018, print -1.

Constraints

1T1000

0n<100

0d1,d29

1K1018

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The first 25 numbers that are good for the first test -> (1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,24,25,26,27)

 

Editor Image

?