Password of a lock

0

0 votes
Algorithms, Chinese Remainder Theorem, Number Theory, Modules, Math, Number theory
Problem

A lock contains n wheels where each wheel contains ki numbers in a specific order. You have m sets and each set contains ci wheels. At each move, you select a set and turn all of its wheels one unit up.

For example, this is the wheel s1,s2,s3,s4 and after one move, it becomes s2,s3,s4,s1.

Your task is to unlock the lock.

Input format

  • The first line contains n denoting the number of the wheels and m denoting the number of the sets.
  • For the next n lines, each line contains ki denoting the size of the ith wheel and ki numbers (s1,s2,s3ski) denoting the numbers of the ith wheel.
    Note: In each wheel, the initial state of the wheel is s1.
  • Each of the next m lines contains ci denoting the size of the ith set and ci numbers (a1,a2,a3aci) denoting the wheels of the ith list.
  • The last line contains n numbers showing the password of the lock.

Output format

The first line contain the ans denoting the number of moves. Each of the next m lines contains ui denoting the number of the moves with the ith set.

Constraints

1n10 000

1m100 000

1ki40 000

1si,pi109

1ci,ain

ni=1ki40 000

mi=1ci400 000

Note: For each input, at least one solution exist.

Time Limit: 2.5
Memory Limit: 512
Source Limit:
Explanation

wheel number 3 is always on 2.

wheel number 1 progress :

5 -> 10 -> 2

wheel number 2 progress :

1 -> 4 -> 2 -> 1

wheel number 4 progress :

10 -> 7 -> 10 -> 7

the final state of wheels :

2, 1, 2, 7

Editor Image

?