// File: /workspace/github_cpp_makefile_star2_O3/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 0x1d30(%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 0x1522
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 0x3cb6(%rip), %rsi      # 0x4fd0
movq %r12, %rdi
callq 0x1140
testq %rax, %rax
je 0x1530
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 0x1394
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 0x1389
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x15e4
movq %r12, %rdi
movl 0x4(%rsp), %esi
movq 0x38(%rsp), %rdx
movl (%rsp), %ecx
movq 0x30(%rsp), %r8
callq 0x16da
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 0x1a04
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r12, %rsi
leaq 0x1bb1(%rip), %rdi      # 0x3009
xorl %eax, %eax
callq 0x1040
cmpl $0x0, 0x8(%rsp)
je 0x14f3
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x14f3
leaq 0x1b91(%rip), %rbx      # 0x300e
leaq 0x1b8f(%rip), %r14      # 0x3013
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
movq 0x10(%rsp), %rax
leaq (%r15,%r15,2), %r13
movq (%rax,%r13,8), %rcx
cmpq %rcx, 0x8(%rax,%r13,8)
je 0x14db
xorl %ebp, %ebp
movl (%rcx,%rbp,4), %eax
cltd
idivl 0xc(%rsp)
movq %r14, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
incq %rbp
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rcx
movq 0x8(%rax,%r13,8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbp, %rdx
ja 0x14aa
incq %r15
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
cmpq %r15, %rcx
ja 0x1487
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x150a
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x10(%rsp), %rdi
callq 0x1590
leaq 0x70(%rsp), %rdi
callq 0x1630
xorl %ebx, %ebx
jmp 0x1541
leaq 0x1b09(%rip), %rdi      # 0x3032
callq 0x1110
jmp 0x1541
leaq 0x1ae3(%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 0x156f
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x10(%rsp), %rdi
callq 0x1590
jmp 0x157e
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x1630
movq %rbx, %rdi
callq 0x1150