Score : 900 points
Problem Statement
There are an integer N and M pairs of integers: (a1,b1),(a2,b2),…,(aM,bM). Each pair (ai,bi) satisfies 1≤ai<bi≤N.
Initally, you have all N! permutations of (1,2,…,N).
You will perform M operations. The i-th operation is as follows.
- Do the following for each of your N! permutations.- Compare the ai-th and bi-th elements. If the former is greater, swap the two elements.
- Compare the ai-th and bi-th elements. If the former is greater, swap the two elements.
For each 1≤i≤M, let Si be the number of permutations of yours that are already sorted in ascending order at the end of the i-th operation.
Print S1,S2,…,SM.
Here, the input gives you pairs of integers (xi,yi) instead of (ai,bi).
The values of (ai,bi) can be obtained using xi, yi, and Si−1 as follows. (Let S0=1 for convenience.)
- Let ci=((xi+Si−1)modN)+1.
- Let di=((yi+Si−1×2)modN)+1. (Here, it is guaranteed that ci=di.)
- Let ai=min(ci,di).
- Let bi=max(ci,di).
Constraints
- 2≤N≤15
- 1≤M≤5×105
- 1≤ai<bi≤N
- 0≤xi,yi≤N−1
The input is given from Standard Input in the following format:
N M
x1 y1
x2 y2
⋮
xM yM
Output
Print M lines. The i-th line should contain Si.
2 1
1 1
2
You start with the permutations (1,2) and (2,1).
We have (a1,b1)=(1,2). At the end of the first operation, you have two copies of (1,2), so you should print 2.
3 4
0 1
2 1
1 1
0 1
2
4
4
6
(ai,bi) in order are (1,2),(2,3),(1,3),(1,2).
5 5
4 4
0 4
1 1
2 4
1 2
2
4
4
8
16
(ai,bi) in order are (1,2),(3,4),(1,5),(2,3),(4,5).