The Locksmith
Time limit1sMemory limit1024 MB
Maintain a dynamic set of arithmetic progressions (a mod b) and answer point queries asking whether any active progression contains a given lock number.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Implementation, Brute force
- Solved
- No attempts yet
Problem
The locksmith Lårs has been put in charge of handing out keys to the locks of Mattelandet. The country has locks, numbered . Every inhabitant has a key that opens certain locks in the country (those the inhabitant is allowed to open).
Whenever a person moves into the country, Lårs gives them a key consisting of two numbers . The person can then open every lock with a number satisfying . (Here denotes congruence. Two numbers are said to be congruent modulo , written , if , that is, is evenly divisible by . This is the same as and having the same remainder when divided by . In most programming languages it can be written as .) When a person moves out of the country, Lårs takes back their key.
There are three types of events that Lårs, as the person in charge of locks, has to handle: answering the question of whether some inhabitant can open a specific lock, someone moving into the country, and someone moving out of the country. For every question about whether there is some inhabitant who can open a specific lock, Lårs must answer "ja" or "nej". Initially nobody lives in the country.
Input
The first line contains two integers , the number of locks and the number of events ().
Then come lines, each in one of the following forms:
- , a question about whether there is some inhabitant who can open lock ().
- , someone moves into the country and receives key which works as described above ().
- , someone with key moves out of the country and Lårs takes back their key. It is guaranteed that a person with key has previously moved into the country ().
Output
For every question about whether someone can unlock a specific lock (lines whose first number is 1), print "ja" if the lock can be opened by some inhabitant currently living in the country, or "nej" if the lock cannot be opened.
Hint
In the first example there is initially no key, so lock cannot be opened. Then a key is added that makes every integer with open locks, so lock can be opened. Finally the key is removed, and lock can no longer be opened.
In the second example two identical keys are handed out. When one of them is taken back, lock can still be opened (that is, all identical keys are not taken back at the same time).