Road Connectivity
Time limit2sMemory limit256 MB
Each day one of the 10 roads is picked at random and its open or closed state is flipped. For each query [l, r], find the probability the country is connected on some day in that range.
- Level
Hard8 of 10
- Topics
- Probability, Matrix, Bit manipulation, Graph
- Solved
- No attempts yet
Statement
There are cities in the country of Byteland. Every two cities are connected by a bidirectional road. For every road, the passage through it is either permitted (open) or prohibited (closed). On day , only roads are open.
Every day (starting from day ), the government of Byteland chooses a road. Each road has the same probability to be chosen. Then, if the passage through the road is permitted, the government closes the road; otherwise, if it is prohibited, the government opens it.
The government of Byteland is very concerned about communication between cities. There are periods of days for which the government wants to know the probability that there is a day within the period such that in this day the country is connected. We say that the country is connected if one can travel from any city to any other using one or several permitted roads.
It can be shown that the required probabilities can be expressed as rational numbers of the form where and are coprime and . Calculate them and print in the form .
Input
The first line contains two integers and (, ), the number of cities in Byteland and the number of roads for which passage is permitted on day .
Each of the following lines contains two integers and (, ), the cities which are connected by the -th permitted road. It is guaranteed that all permitted roads are pairwise different.
The next line contains one integer (), the number of queries.
Each of the following lines contains two integers and (), the periods of days about which the government is concerned.
Output
Output lines, one integer per line. The -th line must contain the answer for the -th query in the form , where is the corresponding probability.
Hint
In the first example, the government has not chosen any road in the first query, and the country is not connected from the beginning, so the probability is . In the 2nd to 9th queries, the probabilities are equal to , , , , , , , and , respectively.