编程介的小学生 2017-09-06 06:20 采纳率: 20.5%
浏览 907
已采纳

Kid's Problem

Kid is a famous thief and he is known for his unique habit. Before his attempt, he will always inform the person who he is going to rob beforehand. Though the people have paid much attention to him, he has never failed. This time Kid informed a billionaire, Jack, that he is going to enter Jack��s home to take away his expensive treasure. Jack is very afraid and he asked a brilliant boy, Conan, to help him. Conan designed a special lock for Jack��s home. However, Kid is very tricky and he stole the structure map and the password of the lock.


From the structure map (see Figure-1), Kid knows that the lock contains K dial plates and K gears, and every dial plate controls several gears. There are N teeth on each gear, which is numbered counter-clockwise from 1 to N. When a certain dial plate is dialed, the gears that refer to the plate will rotate counter-clockwise by several teeth (different gears may rotate different numbers of teeth). A dial plate can be dialed more than once. At the beginning, the numbers of the top teeth of the gears are all ��1��. To open the lock, the number of the top tooth of every gear must be a certain number. These K numbers form the password. Take Figure-1 for an example where N = 8, K = 4; if the password is 1-2-8-1, the lock will open.

With the password in hand, Kid wants to know whether he can open the lock; and if he can, he wants to know the least number of dials he has to use to open the lock. You may assume that the lock is always locked at the beginning, which means that the password cannot be K ��1��s.

Input

There are several test cases. In the first line of each case there are two integers K (1 <= K <= 20) and N (2 <= N <= 10). Then follows a line with K integers expressing the password. Each number in the password is between 1 and N. Then come K lines, the i-th of which describes how the i-th dial plate controls the referred gears. These K lines have the following format:

p a1 b1 a2 b2 ... ap bp

Integer p (0 <= p <= K) expresses the number of gears that are referred to the dial plate. ai (1 <= i <= p) is an integer between 1 and K which tells that the ai-th gear is under the control of this dial plate. bi is an integer between 1 and N-1 which tells that when the dial plate is dialed once, the ai-th gear will rotate across by bi gears.

A test case of K = 0 and N = 0 indicates the end of input, and should not be processed.

Output

For each test case, there is a single line. If the lock can be opened, the line contains the least number of times Kid should dial the dial plates; otherwise, output ��No solution��.

Sample Input

1 4
2
1 1 2
4 8
8 8 8 8
4 1 1 2 2 4 1 3 1
2 4 7 3 2
2 1 5 3 5
1 2 7
0 0
Sample Output

No solution
2

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-09-20 15:43
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?