結果

問題 No.460 裏表ちわーわ
ユーザー koba-e964koba-e964
提出日時 2017-02-16 03:41:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 2,407 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 74,812 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 17:37:14
合計ジャッジ時間 5,661 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 11 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 83 ms
4,380 KB
testcase_06 AC 82 ms
4,376 KB
testcase_07 AC 82 ms
4,380 KB
testcase_08 AC 182 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 182 ms
4,376 KB
testcase_11 AC 83 ms
4,380 KB
testcase_12 AC 84 ms
4,376 KB
testcase_13 AC 184 ms
4,376 KB
testcase_14 AC 186 ms
4,376 KB
testcase_15 AC 185 ms
4,376 KB
testcase_16 AC 83 ms
4,376 KB
testcase_17 AC 183 ms
4,380 KB
testcase_18 AC 183 ms
4,376 KB
testcase_19 AC 183 ms
4,376 KB
testcase_20 AC 84 ms
4,376 KB
testcase_21 AC 183 ms
4,380 KB
testcase_22 AC 183 ms
4,376 KB
testcase_23 AC 183 ms
4,376 KB
testcase_24 AC 181 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
/*
 * Find an assignment (result) s.t. xor_i a[i] * result[i] = b (in GF(2))
 * Returns true if such an assignment was found.
 */
bool gauss_elim_gf2_i64(VL a, ll b, vector<bool> &result) {
  int n = a.size();
  int c = 0;
  vector<int> revmap;
  REP(r, 0, 64) {
    if (c >= n) {
      break;
    }
    int c2 = -1;
    REP(i, c, n) {
      if (a[i] & (1LL << r)) {
	c2 = i;
	break;
      }
    }
    if (c2 < 0) {
      revmap.push_back(-1);
      continue;
    }
    if (c != c2) {
      swap(a[c], a[c2]);
    }
    ll rm = a[c] & -(1LL << r) << 1;
    a[c] ^= rm;
    REP(k, c + 1, n) {
      if (a[k] & (1LL << r)) {
	a[k] ^= rm;
      }
    }
    if (b & (1LL << r)) {
      b ^= rm;
    }
    revmap.push_back(c);
    c++;
  }
  // recover
  int rank = revmap.size();
  result.assign(n, false);
  for (int i = rank - 1; i >= 0; --i) {
    if (b & 1LL << i) {
      int c = revmap[i];
      if (c < 0) {
	return false;
      }
      b ^= a[c];
      result[c] = true;
    }
  }
  return b == 0;
}

int solve(ll board, int m, int n) {
  VL basis;
  VL varbasis;
  REP(i, 0, m) {
    REP(j, 0, n) {
      ll v = 0;
      REP(dx, -1, 2) {
	REP(dy, -1, 2) {
	  int ni = i + dx;
	  int nj = j + dy;
	  if (ni < 0 || ni >= m || nj < 0 || nj >= n) {
	    continue;
	  }
	  v |= 1LL << (ni * n + nj);
	}
      }
      if (i > 0 && j > 0) {
	basis.push_back(v);
      } else {
	varbasis.push_back(v);
      }
    }
  }
  int q = varbasis.size();
  int mi = 100;
  REP(bits, 0, 1 << q) {
    ll bd = board;
    REP(i, 0, q) {
      if (bits & 1 << i) {
	bd ^= varbasis[i];
      }
    }
    vector<bool> result;
    bool succ = gauss_elim_gf2_i64(basis, bd, result);
    if (succ) {
      int rank = count(result.begin(), result.end(), true);
      mi = min(mi, __builtin_popcount(bits) + rank);
    }
  }
  return mi >= 100 ? -1 : mi;
}

int main(void){
  int m, n;
  cin >> m >> n;
  ll board = 0;
  REP(i, 0, m) {
    REP(j, 0, n) {
      int tmp;
      cin >> tmp;
      board |= (ll)tmp << (i * n + j);
    }
  }
  int result = solve(board, m, n);
  if (result < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << solve(board, m, n) << endl;
  }
}
0