One Hit
Time limit2sMemory limit1024 MB
Each skill maps attack x to a*x+b, and the task asks for the fewest skill uses that take starting attack x to at least boss HP y, or -1.
- Level
Hard8 of 10
- Topics
- Greedy, Math, Binary search
- Solved
- No attempts yet
Problem

MapleStory has buff skills such as Blessing of Maha and Howling that change your attack power. Ims, a veteran player with 20 years of experience, is tired of attack-only builds. He wants to take down bosses in his own way, using buff skills like the ones above.
Ims has buff skills. The -th skill multiplies attack power by and then adds . Here can be negative. If and , and the current attack power is , then after using skill the attack power becomes . A skill can be used any number of times, and skills are applied in the order they are used.
Ims is clearing dungeons today. Each dungeon has a boss, and each boss has a different HP. The bosses also differ in level, defense rate, and so on, so Ims's starting attack power depends on the boss he faces.
If Ims's attack power is equal to or higher than the boss's HP, he can defeat the boss in one hit. What is the minimum number of skills he must use to defeat the boss in one hit?
Input
The first line contains the number of skills and the number of dungeons , separated by a space. ()
From the second line, the next lines each contain and , the information for skill , separated by a space. (, )
The next lines each contain , the starting attack power in dungeon , and , the boss HP in that dungeon. ()
Output
For each dungeon, print on its own line the minimum number of skills that must be used to defeat the boss in one hit.
If the boss cannot be defeated in one hit with the given skills, print -1.