atcoder#ARC160F. [ARC160F] Count Sorted Arrays

[ARC160F] Count Sorted Arrays

Score : 900900 points

Problem Statement

There are an integer NN and MM pairs of integers: (a1,b1),(a2,b2),,(aM,bM)(a_1, b_1), (a_2, b_2), \dots, (a_M, b_M). Each pair (ai,bi)(a_i, b_i) satisfies 1ai<biN1 \leq a_i \lt b_i \leq N.

Initally, you have all N!N! permutations of (1,2,,N)(1,2,\dots,N). You will perform MM operations. The ii-th operation is as follows.

  • Do the following for each of your N!N! permutations.- Compare the aia_i-th and bib_i-th elements. If the former is greater, swap the two elements.
  • Compare the aia_i-th and bib_i-th elements. If the former is greater, swap the two elements.

For each 1iM1 \leq i \leq M, let SiS_i be the number of permutations of yours that are already sorted in ascending order at the end of the ii-th operation. Print S1,S2,,SMS_1, S_2, \dots, S_M.

Here, the input gives you pairs of integers (xi,yi)(x_i, y_i) instead of (ai,bi)(a_i, b_i). The values of (ai,bi)(a_i, b_i) can be obtained using xix_i, yiy_i, and Si1S_{i-1} as follows. (Let S0=1S_0 = 1 for convenience.)

  • Let ci=((xi+Si1)modN)+1c_i = ((x_i + S_{i-1}) \bmod N) + 1.
  • Let di=((yi+Si1×2)modN)+1d_i = ((y_i + S_{i-1} \times 2) \bmod N) + 1. (Here, it is guaranteed that cidic_i \neq d_i.)
  • Let ai=min(ci,di)a_i = \min(c_i, d_i).
  • Let bi=max(ci,di)b_i = \max(c_i, d_i).

Constraints

  • 2N152 \leq N \leq 15
  • 1M5×1051 \leq M \leq 5 \times 10^5
  • 1ai<biN1 \leq a_i \lt b_i \leq N
  • 0xi,yiN10 \leq x_i, y_i \leq N - 1

Input

The input is given from Standard Input in the following format:

NN MM

x1x_1 y1y_1

x2x_2 y2y_2

\vdots

xMx_M yMy_M

Output

Print MM lines. The ii-th line should contain SiS_i.

2 1
1 1
2

You start with the permutations (1,2)(1, 2) and (2,1)(2, 1). We have (a1,b1)=(1,2)(a_1, b_1) = (1, 2). At the end of the first operation, you have two copies of (1,2)(1, 2), so you should print 22.

3 4
0 1
2 1
1 1
0 1
2
4
4
6

(ai,bi)(a_i, b_i) in order are (1,2),(2,3),(1,3),(1,2)(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)(a_i, b_i) in order are (1,2),(3,4),(1,5),(2,3),(4,5)(1, 2), (3, 4), (1, 5), (2, 3), (4, 5).