ATMs
Time limit1sMemory limit128 MB
For each amount under 10^30, find a subset of the 100 ATMs whose signed sum equals the amount and another equal to its negation.
- Level
Medium5 of 10
- Topics
- Bit manipulation, Greedy, Math
- Solved
- No attempts yet
Problem
The Credit Society of Byteland lets any member borrow any whole number of Bytelandish ducats below , and the entire sum must be paid back within seven days.
The Society's Client Service Room holds 100 ATMs, numbered through . Each ATM only ever performs one fixed action tied to its number: ATM pays out ducats if is even, and collects ducats if is odd.
A client borrows a given amount by using each ATM at most once so that the net cash flow equals that amount: ducats paid out by even ATMs are gained, and ducats collected by odd ATMs are given up. For each client we must decide whether the requested amount can be borrowed this way and, if it can, which ATMs to use. We ask the same question about returning the amount, where the net cash flow must instead equal the negative of that amount.
For example, to borrow ducats a client takes ducats from ATM and ducat from ATM , then pays ducats into ATM and ducats into ATM ; the net is . To return ducats the client takes ducat from ATM and pays ducats into ATM ; the net is .
Write a program that reads the number of clients and each requested amount, decides for every client whether the amount can be borrowed and whether it can be returned, and reports the ATMs to use in each case.
Input
The first line contains a single positive integer (), the number of clients. Each of the next lines contains one positive integer smaller than (up to 30 decimal digits): the amount the corresponding client wants to borrow.
Output
Print lines, two for each client, in input order. For client :
- the first line lists the numbers of the ATMs, in strictly decreasing order and separated by single spaces, that the client uses to borrow the requested amount, or the single word
NIEif borrowing it under the rules is impossible; - the second line lists, in the same format, the ATMs used to return the amount, or
NIEif returning it is impossible.
Every ATM number lies in the range . (NIE means "no".)