結果

問題 No.2178 Payable Magic Items
ユーザー tnakao0123tnakao0123
提出日時 2023-01-09 22:41:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 4,000 ms
コード長 1,359 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 64,140 KB
実行使用メモリ 17,632 KB
最終ジャッジ日時 2023-08-22 21:49:50
合計ジャッジ時間 4,243 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,664 KB
testcase_01 AC 4 ms
7,648 KB
testcase_02 AC 4 ms
7,704 KB
testcase_03 AC 46 ms
8,188 KB
testcase_04 AC 4 ms
7,712 KB
testcase_05 AC 4 ms
7,676 KB
testcase_06 AC 6 ms
7,836 KB
testcase_07 AC 4 ms
7,736 KB
testcase_08 AC 6 ms
7,924 KB
testcase_09 AC 6 ms
7,772 KB
testcase_10 AC 4 ms
7,660 KB
testcase_11 AC 173 ms
17,476 KB
testcase_12 AC 188 ms
17,592 KB
testcase_13 AC 186 ms
17,580 KB
testcase_14 AC 186 ms
17,632 KB
testcase_15 AC 185 ms
17,576 KB
testcase_16 AC 185 ms
17,576 KB
testcase_17 AC 80 ms
11,156 KB
testcase_18 AC 6 ms
7,760 KB
testcase_19 AC 34 ms
9,988 KB
testcase_20 AC 5 ms
7,720 KB
testcase_21 AC 5 ms
7,748 KB
testcase_22 AC 6 ms
7,740 KB
testcase_23 AC 5 ms
7,696 KB
testcase_24 AC 131 ms
14,684 KB
testcase_25 AC 58 ms
9,388 KB
testcase_26 AC 45 ms
10,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2178.cc:  No.2178 Payable Magic Items - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_K = 8;
const int MAX_M = 390625; // = 5^8

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

char s[MAX_K + 4];
vi vs[MAX_N];
bool used[MAX_M];

/* subroutines */

int v2id(vi &v) {
  int id = 0;
  for (int i = 0, e = 1; i < v.size(); i++, e *= 5) id += v[i] * e;
  return id;
}

void id2v(int id, int k, vi &v) {
  v.resize(k);
  for (int i = 0; i < k; i++, id /= 5) v[i] = id % 5;
}

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    vs[i].resize(k);
    for (int j = 0; j < k; j++) vs[i][j] = s[j] - '0';
  }
  sort(vs, vs + n);
  
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    int st = v2id(vs[i]);
    if (used[st]) cnt++;
    else {
      used[st] = true;
      qi q;
      q.push(st);

      while (! q.empty()) {
	int u = q.front(); q.pop();
	vi uv;
	id2v(u, k, uv);

	for (int j = 0; j < k; j++)
	  if (uv[j] > 0) {
	    uv[j]--;
	    int v = v2id(uv);
	    if (! used[v]) {
	      used[v] = true;
	      q.push(v);
	    }
	    uv[j]++;
	  }
      }
    }
  }

  printf("%d\n", cnt);
  return 0;
}
0