結果
| 問題 | No.1598 4×4 Grid | 
| コンテスト | |
| ユーザー |  tnakao0123 | 
| 提出日時 | 2021-07-12 14:45:50 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 176 ms / 4,000 ms | 
| コード長 | 1,792 bytes | 
| コンパイル時間 | 771 ms | 
| コンパイル使用メモリ | 94,188 KB | 
| 実行使用メモリ | 115,328 KB | 
| 最終ジャッジ日時 | 2024-07-02 03:26:42 | 
| 合計ジャッジ時間 | 2,696 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 7 | 
ソースコード
/* -*- coding: utf-8 -*-
 *
 * 1598.cc:  No.1598 4×4 Grid - yukicoder
 */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;
/* constant */
const int N = 4;
const int NN = N * N;
const int NNBITS = 1 << NN;
const int MAX_K = 216;
const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };
/* typedef */
typedef long long ll;
/* global variables */
int bnums[NNBITS], nbs[NN], phs[NNBITS];
ll dp[NNBITS][MAX_K + 1];
/* subroutines */
inline int xy2p(int x, int y) { return y * N + x; }
inline void p2xy(int p, int &x, int &y) { x = p % N, y = p / N; }
/* main */
int main() {
  bnums[0] = 0;
  for (int bits = 1, msb = 1; bits < NNBITS; bits++) {
    if ((msb << 1) <= bits) msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
  }
  for (int u = 0; u < NN; u++) {
    int x, y;
    p2xy(u, x, y);
    for (int di = 0; di < 4; di++) {
      int vx = x + dxs[di], vy = y + dys[di];
      if (vx >= 0 && vx < N && vy >= 0 && vy < N)
	nbs[u] |= (1 << xy2p(vx, vy));
    }
  }
  for (int bits = 0; bits < NNBITS; bits++)
    for (int i = 0, bi = 1; i < NN; i++, bi <<= 1)
      if (bits & bi)
	phs[bits] += bnums[nbs[i]] - bnums[bits & nbs[i]];
    
  int k;
  scanf("%d", &k);
  dp[0][0] = 1;
  for (int bits = 0; bits < NNBITS; bits++)
    for (int j = 0, bj = 1; j < NN; j++, bj <<= 1)
      if (! (bits & bj)) {
	int bits1 = (bits | bj);
	int di1 = phs[bits1];
	for (int i = 0; i + di1 <= k; i++)
	  dp[bits1][i + di1] += dp[bits][i];
      }
  printf("%lld\n", dp[NNBITS - 1][k]);
  return 0;
}
            
            
            
        