結果

問題 No.941 商とあまり
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-04 04:53:26
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 4,108 bytes
コンパイル時間 1,187 ms
コンパイル使用メモリ 132,188 KB
実行使用メモリ 7,284 KB
最終ジャッジ日時 2023-09-04 03:40:41
合計ジャッジ時間 14,882 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 124 ms
4,380 KB
testcase_05 AC 124 ms
4,376 KB
testcase_06 AC 124 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 124 ms
4,376 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 21 ms
4,376 KB
testcase_16 AC 27 ms
4,380 KB
testcase_17 AC 124 ms
4,380 KB
testcase_18 AC 124 ms
4,376 KB
testcase_19 AC 123 ms
4,380 KB
testcase_20 AC 124 ms
4,380 KB
testcase_21 AC 124 ms
4,380 KB
testcase_22 AC 123 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 15 ms
4,376 KB
testcase_30 AC 15 ms
4,376 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 3 ms
4,384 KB
testcase_33 AC 124 ms
4,376 KB
testcase_34 AC 124 ms
4,376 KB
testcase_35 AC 124 ms
4,380 KB
testcase_36 AC 5 ms
4,380 KB
testcase_37 AC 16 ms
4,380 KB
testcase_38 AC 139 ms
6,260 KB
testcase_39 AC 123 ms
4,380 KB
testcase_40 AC 124 ms
4,380 KB
testcase_41 AC 123 ms
4,380 KB
testcase_42 AC 125 ms
4,380 KB
testcase_43 AC 51 ms
6,560 KB
testcase_44 AC 166 ms
6,352 KB
testcase_45 AC 55 ms
4,504 KB
testcase_46 AC 26 ms
4,384 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 125 ms
4,380 KB
testcase_49 AC 12 ms
4,376 KB
testcase_50 AC 180 ms
5,980 KB
testcase_51 AC 146 ms
5,444 KB
testcase_52 AC 256 ms
7,284 KB
testcase_53 AC 81 ms
6,288 KB
testcase_54 AC 64 ms
4,380 KB
testcase_55 AC 116 ms
4,512 KB
testcase_56 AC 29 ms
4,376 KB
testcase_57 AC 123 ms
4,376 KB
testcase_58 AC 124 ms
4,380 KB
testcase_59 AC 3 ms
4,376 KB
testcase_60 AC 3 ms
4,376 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 124 ms
4,380 KB
testcase_63 AC 123 ms
4,376 KB
testcase_64 AC 124 ms
4,376 KB
testcase_65 AC 124 ms
4,376 KB
testcase_66 AC 3 ms
4,976 KB
testcase_67 AC 124 ms
4,376 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 125 ms
4,376 KB
testcase_70 AC 124 ms
4,380 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 3 ms
4,376 KB
testcase_73 AC 124 ms
4,376 KB
testcase_74 AC 124 ms
4,380 KB
testcase_75 AC 124 ms
4,376 KB
testcase_76 AC 124 ms
4,376 KB
testcase_77 AC 124 ms
4,380 KB
testcase_78 AC 124 ms
4,380 KB
testcase_79 AC 124 ms
4,376 KB
testcase_80 AC 124 ms
4,376 KB
testcase_81 AC 123 ms
4,376 KB
testcase_82 AC 124 ms
4,376 KB
testcase_83 AC 124 ms
4,380 KB
testcase_84 AC 124 ms
4,376 KB
testcase_85 AC 123 ms
4,380 KB
testcase_86 AC 125 ms
4,376 KB
testcase_87 AC 127 ms
4,376 KB
testcase_88 AC 125 ms
4,380 KB
testcase_89 AC 124 ms
4,376 KB
testcase_90 AC 124 ms
4,376 KB
testcase_91 AC 124 ms
4,380 KB
testcase_92 AC 124 ms
4,376 KB
testcase_93 AC 124 ms
4,376 KB
testcase_94 AC 124 ms
4,376 KB
testcase_95 AC 123 ms
4,380 KB
testcase_96 AC 125 ms
4,380 KB
testcase_97 AC 124 ms
4,380 KB
testcase_98 AC 123 ms
4,376 KB
testcase_99 AC 123 ms
4,376 KB
testcase_100 AC 124 ms
4,380 KB
testcase_101 AC 123 ms
4,376 KB
testcase_102 AC 123 ms
4,380 KB
testcase_103 AC 124 ms
4,376 KB
testcase_104 AC 2 ms
4,376 KB
testcase_105 AC 2 ms
4,376 KB
testcase_106 AC 2 ms
4,376 KB
testcase_107 AC 2 ms
4,376 KB
testcase_108 AC 1 ms
4,380 KB
testcase_109 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!uint.gcdImpl`

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const X = readInt();
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }
      A.sort;
      
      long prod = 1;
      foreach (i; 0 .. N) {
        prod *= (A[i] + 1);
        if (prod - 1 > X) {
          foreach (x; 1 .. X + 1) {
            write(0);
          }
          writeln();
          goto failed;
        }
      }
      
      if (N >= 2 && A[0] == 1) {
        const lb = cast(int)(prod - 1);
        foreach (x; 1 .. lb) {
          write(0);
        }
        foreach (x; lb .. X + 1) {
          write(1);
        }
        writeln();
        goto failed;
      }
      
      {
        auto ms = new int[1 << N];
        auto dp = new int[][1 << N];
        foreach (u; 0 .. 1 << N) {
          ms[u] = 1;
          foreach (i; 0 .. N) {
            if (u & 1 << i) {
              ms[u] = ms[u] / gcd(ms[u], A[i]) * A[i];
            }
          }
          dp[u] = new int[ms[u]];
          dp[u][] = INF;
          if (!(u & u - 1)) {
            chmin(dp[u][0], ms[u]);
          }
        }
        foreach (u; 1 .. 1 << N) {
          foreach (i; 0 .. N) {
            if (!(u & 1 << i)) {
              const v = u | 1 << i;
              alias Entry = Tuple!(int, "c", int, "s");
              BinaryHeap!(Array!Entry, "a > b") que;
              auto costs = new int[ms[v]];
              costs[] = INF;
              foreach (r; 0 .. ms[u]) {
                if (dp[u][r] < INF) {
                  const z = (dp[u][r] + 1) * (A[i] + 1) - 1;
                  const s = z % ms[v];
                  if (chmin(costs[s], z)) {
                    que.insert(Entry(z, s));
                  }
                }
              }
              for (; !que.empty; ) {
                const c = que.front.c;
                const s = que.front.s;
                que.removeFront;
                if (costs[s] == c) {
                  foreach (d; (u & u - 1) ? [ms[u] * (A[i] + 1), A[i]] : [A[i]]) {
                    const ss = (s + d) % ms[v];
                    if (chmin(costs[ss], c + d)) {
                      que.insert(Entry(c + d, ss));
                    }
                  }
                }
              }
              foreach (s; 0 .. ms[v]) {
                chmin(dp[v][s], costs[s]);
              }
            }
          }
          debug {
            writeln(u, " ", ms[u], " ", dp[u]);
          }
        }
        
        auto ans = new char[X + 1];
        ans[] = '0';
        foreach (r; 0 .. ms[$ - 1]) {
          if (dp[$ - 1][r] < INF) {
            for (int x = dp[$ - 1][r]; x <= X; ) {
              ans[x] = '1';
              if (N == 1) {
                break;
              }
              x += ms[$ - 1];
            }
          }
        }
        writeln(ans[1 .. $]);
      }
     failed:
    }
  } catch (EOFException e) {
  }
}
0