結果

問題 No.460 裏表ちわーわ
ユーザー koba-e964koba-e964
提出日時 2017-02-15 17:28:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,512 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 95,360 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 17:22:42
合計ジャッジ時間 3,413 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#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;
typedef pair<int, int> PI;

int gauss_elim_i64(VL a, ll b) {
  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();
  ll x = 0;
  for (int i = rank - 1; i >= 0; --i) {
    if (b & 1LL << i) {
      int c = revmap[i];
      if (c < 0) {
	return -1;
      }
      b ^= a[c];
      x |= 1LL << c;
    }
  }
  return b == 0 ? __builtin_popcountll(x) : -1;
}

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];
      }
    }
    int rank = gauss_elim_i64(basis, bd);
    if (rank >= 0) {
      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 |= tmp << (i * n + j);
    }
  }
  int result = solve(board, m, n);
  if (result < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << solve(board, m, n) << endl;
  }
}
0