Gift Before Tax
Time limit2sMemory limit512 MB
Given progressive tax bands and each friend's prior income, find the smallest pre-tax gift that leaves the friend with exactly the required after-tax amount.
- Level
Medium6 of 10
- Topics
- Binary search, Math, Implementation
- Solved
- No attempts yet
Problem
George has won the lottery and wants to share the money with his friends. A gift is taxed once it gets large enough, and the tax depends on how much the friend has already earned that year.
Tax is worked out with tax bands. The bands start at zero and follow one another. Each band taxes the part of the income that falls inside it at its own rate. The last band applies to all income above the sum of the sizes of the other bands.
If a friend has already earned this year and George hands over a pre-tax amount , that friend's income for the year rises from to . The tax due is the total tax on income minus the total tax on income , and the friend keeps minus that tax.
You are given the number of tax bands, how much each friend has earned, and how much each friend has to keep after tax. Work out how much George gives each friend before tax.
Input
- One line with an integer (), the number of tax bands.
- lines, each with two real numbers () and (): the size in pounds of the -th tax band and its tax rate in percent.
- One line with a real number (), the tax rate in percent on all income above the bands.
- One line with an integer (), the number of friends.
- lines, each with two real numbers and (, ): what the -th friend has earned, and what that friend has to keep after tax.
The tax bands are given from the lowest band to the highest.
Output
Print lines, one per friend, in the order the friends are given.
Each line holds the pre-tax amount George gives that friend, rounded to exactly six decimal places. When several amounts leave the friend with exactly , print the smallest one.