// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
leaq 0x1d83(%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 0x14a3
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 0x3d09(%rip), %rsi      # 0x4fd0
movq %r12, %rdi
callq 0x1140
testq %rax, %rax
je 0x14b1
movl %ebp, 0x4(%rsp)
leal -0x2(,%r13,4), %ecx
movl %ecx, (%rsp)
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rax, %r12
callq 0x1090
movslq %ebx, %rax
leaq (%r12,%r13), %rcx
movq %r14, %rdx
addq %r12, %rdx
movq %r13, %rsi
imulq %rax, %rsi
leaq 0x1(%rax), %rdi
movq %rsi, 0x38(%rsp)
incq %rsi
movq %rsi, 0x30(%rsp)
leal -0x2(%r13,%r13,4), %r9d
movb $0x1, %r10b
movq %r12, %r11
movq %r12, 0x28(%rsp)
movq %r14, 0x40(%rsp)
cmpq %r13, %rbp
je 0x1391
xorl %r14d, %r14d
movl %r9d, %ebx
movq %r13, %rsi
xorl %r15d, %r15d
subq $0x1, %r13
jb 0x136b
movslq %ebx, %rbx
leaq (%r11,%rbx), %r8
movb %r10b, (%r12,%r14)
movb %r10b, (%rcx,%r15)
movb %r10b, (%rdx,%r15)
movb %r10b, (%r8,%r14)
addq %rdi, %r15
decl %ebx
addq %rax, %r14
jmp 0x1344
incq %rbp
movq 0x38(%rsp), %r8
addq %r8, %rcx
movq 0x30(%rsp), %rbx
addq %rbx, %rdx
incl %r9d
addq %rbx, %r12
addq %r8, %r11
movq %rsi, %r13
movq 0x40(%rsp), %r14
jmp 0x1333
leaq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x15c4
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl %r14d, %edx
movl (%rsp), %ecx
movq 0x28(%rsp), %r8
callq 0x16ae
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x60(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rcx
pushq $-0x1
popq %r8
callq 0x19c0
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
pushq $0x18
popq %r13
cqto
idivq %r13
leaq 0x1c0c(%rip), %rdi      # 0x3009
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
cmpl $0x0, 0x8(%rsp)
je 0x1481
movq 0x10(%rsp), %rcx
leaq 0x1bf4(%rip), %r14      # 0x300e
leaq 0x1bf2(%rip), %r15      # 0x3013
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
subq %rcx, %rax
cqto
idivq %r13
cmpq %r12, %rax
jbe 0x1481
movq %r14, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1040
imulq $0x18, %r12, %rbp
xorl %ebx, %ebx
movq 0x10(%rsp), %rcx
movq (%rcx,%rbp), %rax
movq 0x8(%rcx,%rbp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x147c
movl (%rax,%rbx,4), %eax
cltd
idivl 0xc(%rsp)
movq %r15, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
incq %rbx
jmp 0x1449
incq %r12
jmp 0x1424
leaq 0x60(%rsp), %rdi
callq 0x158e
leaq 0x10(%rsp), %rdi
callq 0x1500
leaq 0x48(%rsp), %rdi
callq 0x160c
xorl %ebx, %ebx
jmp 0x14c0
leaq 0x1b88(%rip), %rdi      # 0x3032
callq 0x1110
jmp 0x14c0
leaq 0x1b62(%rip), %rdi      # 0x301a
callq 0x1110
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x158e
leaq 0x10(%rsp), %rdi
callq 0x1500
jmp 0x14ed
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x160c
movq %rbx, %rdi
callq 0x1150
nop