Exponent Exchange
Time limit1sMemory limit1024 MB
Alice and Bob must gather all b^p dollars with transfers of powers of b, and the task asks for the fewest transactions the busiest player needs.
- Level
Medium7 of 10
- Topics
- Math, Greedy, Binary search
- Solved
- No attempts yet
Problem
Alice and Bob are playing a cooperative game. Together they hold dollars, where and are given integers. Alice initially holds dollars, and Bob holds dollars. Alice and Bob want to consolidate their money so that one person holds all of it.
In each transaction, one player can send the other player dollars, for some integer with . Each player wants to initiate as few transactions as possible. They cooperate so that the player who initiates the most transactions (the busiest player) initiates as few as possible.
Alice and Bob want to know the fewest number of transactions that the busiest player must initiate to transfer all the money.
Input
The first line contains two integers () and (), where is the base and is the number of digits.
The next line contains integers , separated by spaces, with and . These are the base- digits of , given from the highest power to the lowest. That is, . For example, with , the digits 4 2 7 8 6 represent the number 42786.
Output
Output a single integer: the minimum number of transactions that the busiest player must initiate to transfer all the money to either Alice or Bob.