結果

問題 No.197 手品
ユーザー tnakao0123
提出日時 2016-03-27 01:33:00
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,667 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 85,576 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 03:47:37
合計ジャッジ時間 2,004 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 197.cc: No.197 手品 - 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 BITS = 1 << 8;

/* typedef */

/* global variables */

int fs[BITS], cs[BITS], used[BITS];

/* subroutines */

int s2i(string &s) {
  int r = 0;
  for (int i = 0; i < 3; i++)
    if (s[i] == 'o') r |= (1 << i);
  return r;
}

int swapbit(int bits, int i, int j) {
  int bi = ((bits >> i) & 1);
  int bj = ((bits >> j) & 1);
  return (bits & ~(1 << i) & ~(1 << j)) | (bi << j) | (bj << i);
}

/* main */

int main() {
  for (int bits = 0; bits < BITS; bits++) {
    fs[bits] = 0;
    for (int i = 0, bi = 1; i < 8; i++, bi <<= 1)
      if (bits & bi)
	fs[bits] |= (1 << swapbit(i, 0, 1)) | (1 << swapbit(i, 1, 2));
  }

  string sst, sgl;
  int n;
  cin >> sst >> n >> sgl;

  int st = s2i(sst);
  int gl = s2i(sgl);

  cs[0] = 1 << st;
  memset(used, -1, sizeof(used));
  used[1 << st] = 0;

  int i0 = 0, i1;
  for (i1 = 1; i1 < BITS; i1++) {
    int nc = fs[cs[i1 - 1]];
    if (used[nc] >= 0) {
      i0 = used[nc];
      break;
    }
    cs[i1] = nc;
    used[nc] = i1;
  }
  //printf("%d,%d\n", i0, i1);
  //for (int i = 0; i < i1; i++) printf("%02x ", cs[i]); putchar('\n');

  int k = (n < i0) ? n : (n - i0) % (i1 - i0) + i0;
  bool ok = ((cs[k] & (1 << gl)) == 0);
  cout << (ok ? "SUCCESS" : "FAILURE") << endl;
  
  return 0;
}
0