// File: /workspace/github_cpp_makefile_star2_O1/razimantv[P]DancingLinks/nqueens/nqueens.cpp
// Function: main
int main() {
  // Read N and whether the user wants the full list of solutions
  //  or just the count
  int N, getall;
  scanf("%d%d", &N, &getall);
  if (N < 1) {
    printf("Invalid board size\n");
    return 0;
  }

  // Every position of a queen is a subset. Hence R=N*N
  // Every row and every column should have exactly one queen. This gives 2*N
  //  essential constraints
  // Every diagonal should have at most one queen. This gives 4*N-2
  //  non-essential constraints
  int R = N * N, C1 = 2 * N, C2 = 4 * N - 2, C = C1 + C2;

  // Matrix to store the constraint adjacency matrix
  char *mat;
  mat = new (nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      // Queen position (i,j) corresponds to row# i*N+j of adjacency matrix
      int r = i * N + j;

      // Constraint of row i is column# i of adjacency matrix
      // Constraint of column i is column# N+i of adjacency matrix
      // Constraint of upward diagonal i is column# 2*N+i of adjacency matrix
      // Constraint of downward diagonal i is column# 4*N-1+i of adjacency
      // matrix
      int c1 = i, c2 = N + j, c3 = 2 * N + i + j, c4 = 5 * N - 2 + i - j;
      mat[r * C + c1] = 1;
      mat[r * C + c2] = 1;
      mat[r * C + c3] = 1;
      mat[r * C + c4] = 1;
    }
  }

  // Now just solve it with dancing links
  DancingLinks D;
  D.Create(R, C1, C2, mat);
  std::vector<std::vector<int> > all;
  std::vector<int> cur;
  D.solve(all, cur, true);

  // Number of solutions
  printf("%lu\n", all.size());

  if (getall != 0) {
    // User wants the locations of all the queens
    for (int i = 0; i < all.size(); i++) {
      printf("%d:\n", i);
      for (int j = 0; j < all[i].size(); j++) {
        printf("%d %d\n", all[i][j] / N, all[i][j] % N);
      }
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x1d50(%rip), %rdi      # 0x3004
xorl %ebx, %ebx
leaq 0xc(%rsp), %r14
leaq 0x8(%rsp), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x10e0
movl (%r14), %r13d
testl %r13d, %r13d
jle 0x1504
movl %r13d, %ebp
imull %r13d, %ebp
leaq (,%r13,2), %r14
leal (%r14,%r13,4), %ebx
addl $-0x2, %ebx
movl %ebx, %r12d
imull %ebp, %r12d
movq 0x3cd6(%rip), %rsi      # 0x4fd0
movq %r12, %rdi
callq 0x1140
testq %rax, %rax
je 0x1512
movq %rax, %r15
movl %ebp, 0x4(%rsp)
leal -0x2(,%r13,4), %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1090
movslq %ebx, %rax
leaq (%r15,%r13), %rcx
movq %r14, 0x38(%rsp)
movq %r14, %rdx
addq %r15, %rdx
movq %rax, %rsi
imulq %r13, %rsi
leaq 0x1(%rax), %rdi
movq %rsi, 0x48(%rsp)
incq %rsi
movq %rsi, 0x40(%rsp)
leal -0x2(%r13,%r13,4), %r9d
movb $0x1, %r10b
movq %r15, %r11
movq %r15, 0x30(%rsp)
movq %r15, %r12
movq %r13, %rsi
xorl %ebx, %ebx
movl %r9d, %r15d
xorl %r14d, %r14d
movslq %r15d, %r15
leaq (%r11,%r15), %r8
movb %r10b, (%r12,%rbx)
movb %r10b, (%rcx,%r14)
movb %r10b, (%rdx,%r14)
movb %r10b, (%rbx,%r8)
addq %rdi, %r14
decl %r15d
addq %rax, %rbx
decq %r13
jne 0x1374
incq %rbp
movq 0x48(%rsp), %r8
addq %r8, %rcx
movq 0x40(%rsp), %rbx
addq %rbx, %rdx
incl %r9d
addq %rbx, %r12
addq %r8, %r11
movq %rsi, %r13
cmpq %rsi, %rbp
jne 0x1369
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x15c0
movq %r12, %rdi
movl 0x4(%rsp), %esi
movq 0x38(%rsp), %rdx
movl (%rsp), %ecx
movq 0x30(%rsp), %r8
callq 0x16b8
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0x50(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0x70(%rsp), %rdi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x19f6
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r12, %rsi
leaq 0x1bd1(%rip), %rdi      # 0x3009
xorl %eax, %eax
callq 0x1040
cmpl $0x0, 0x8(%rsp)
je 0x14d5
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x14d5
leaq 0x1bb1(%rip), %rbx      # 0x300e
leaq 0x1baf(%rip), %r14      # 0x3013
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
movq 0x10(%rsp), %rcx
leaq (%r15,%r15,2), %r13
movq (%rcx,%r13,8), %rax
cmpq %rax, 0x8(%rcx,%r13,8)
je 0x14bb
xorl %ebp, %ebp
movl (%rax,%rbp,4), %eax
cltd
idivl 0xc(%rsp)
movq %r14, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
incq %rbp
movq 0x10(%rsp), %rcx
movq (%rcx,%r13,8), %rax
movq 0x8(%rcx,%r13,8), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbp, %rcx
ja 0x148a
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
incq %r15
sarq $0x3, %rax
imulq %r12, %rax
cmpq %r15, %rax
ja 0x1467
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x14ec
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x10(%rsp), %rdi
callq 0x1572
leaq 0x70(%rsp), %rdi
callq 0x160c
xorl %ebx, %ebx
jmp 0x1523
leaq 0x1b27(%rip), %rdi      # 0x3032
callq 0x1110
jmp 0x1523
leaq 0x1b01(%rip), %rdi      # 0x301a
callq 0x1110
movl $0x1, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1551
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x10(%rsp), %rdi
callq 0x1572
jmp 0x1560
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x160c
movq %rbx, %rdi
callq 0x1150