Decrease the Boss Strength
Time limit1.5sMemory limit1024 MB
Count the orderings of spell uses that bring N down to zero, where spell i subtracts a_i and needs N divisible by 2^b_i.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Math
- Solved
- No attempts yet
Problem
Fulano, an avid gamer, has come across an epic challenge in the online game "Boss Challenge". The goal is to defeat a powerful boss, whose power is described by a set of ancient runes. These runes represent a giant binary number , which is the total strength of the enemy.
To defeat the boss, Fulano has different spells at his disposal. The goal is to reduce the total strength of the enemy to zero using these spells. The -th spell is described by two integers and . When used, the -th spell reduces the value of by . A spell can be used as many times as the player wants, as long as two conditions hold:
- The value of is less than or equal to the current value of .
- The current value of is divisible by . In other words, spell can only be used if the last binary digits of are zeros.
Fulano wants to know how many different ways he can combine the spells to reduce the binary number to exactly zero. Two combinations are considered different if the order in which the spells are used is different.
The number of combinations can be very large, so the answer should be printed modulo .
Help Fulano find the answer.
Input
The first line contains a single integer (), the power of the boss.
The second line contains a single integer (), the number of spells.
Each of the next lines contains two integers () and ().
Output
Print the number of different sequences of spell uses that reduce the power of the boss from to , modulo .