結果

問題 No.355 数当てゲーム(2)
ユーザー shi-moshi-mo
提出日時 2016-04-02 23:52:16
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,833 bytes
コンパイル時間 1,796 ms
コンパイル使用メモリ 148,800 KB
実行使用メモリ 24,360 KB
平均クエリ数 15.27
最終ジャッジ日時 2023-09-02 21:01:47
合計ジャッジ時間 6,283 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
23,544 KB
testcase_01 AC 26 ms
24,336 KB
testcase_02 AC 26 ms
23,412 KB
testcase_03 AC 26 ms
24,336 KB
testcase_04 AC 26 ms
24,036 KB
testcase_05 AC 26 ms
23,964 KB
testcase_06 AC 27 ms
23,988 KB
testcase_07 AC 27 ms
23,604 KB
testcase_08 AC 26 ms
24,000 KB
testcase_09 AC 27 ms
23,628 KB
testcase_10 AC 26 ms
23,664 KB
testcase_11 AC 26 ms
24,036 KB
testcase_12 AC 26 ms
23,604 KB
testcase_13 AC 26 ms
24,000 KB
testcase_14 AC 26 ms
23,388 KB
testcase_15 AC 27 ms
23,988 KB
testcase_16 AC 26 ms
23,508 KB
testcase_17 AC 27 ms
24,360 KB
testcase_18 AC 27 ms
23,388 KB
testcase_19 AC 26 ms
24,000 KB
testcase_20 AC 26 ms
23,352 KB
testcase_21 AC 26 ms
23,508 KB
testcase_22 AC 26 ms
23,796 KB
testcase_23 AC 27 ms
23,988 KB
testcase_24 AC 26 ms
23,352 KB
testcase_25 AC 26 ms
24,012 KB
testcase_26 AC 27 ms
24,360 KB
testcase_27 AC 27 ms
23,520 KB
testcase_28 AC 26 ms
23,352 KB
testcase_29 AC 26 ms
23,352 KB
testcase_30 AC 27 ms
23,532 KB
testcase_31 AC 26 ms
23,352 KB
testcase_32 AC 27 ms
23,808 KB
testcase_33 AC 27 ms
23,544 KB
testcase_34 AC 27 ms
23,232 KB
testcase_35 AC 26 ms
23,412 KB
testcase_36 AC 25 ms
23,388 KB
testcase_37 AC 26 ms
23,676 KB
testcase_38 AC 27 ms
23,340 KB
testcase_39 AC 26 ms
23,412 KB
testcase_40 AC 27 ms
24,300 KB
testcase_41 AC 26 ms
23,808 KB
testcase_42 AC 25 ms
23,352 KB
testcase_43 AC 26 ms
23,532 KB
testcase_44 AC 27 ms
24,000 KB
testcase_45 AC 27 ms
24,024 KB
testcase_46 AC 26 ms
23,508 KB
testcase_47 AC 27 ms
23,988 KB
testcase_48 AC 27 ms
23,676 KB
testcase_49 AC 26 ms
23,616 KB
testcase_50 AC 26 ms
23,808 KB
testcase_51 AC 27 ms
23,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.conv;
import std.array;
import std.algorithm;
import core.bitop;
import core.stdc.stdlib;

void main() {
    detectSequence(detectCombination());
}

ushort detectCombination() {
    ushort[] candidates = generatePermutation();
    while (0 < candidates.length) {
        ushort q = candidates.back; candidates.popBack;

        query(decode(q));
        ushort[][] grouped = group(q, candidates);

        int n = reply();
        if (4 == n) return q;
        candidates = grouped[n].dup;
    }
    return 0; // dummy;
}

ushort[] generatePermutation() {
    ushort[] perm;
    foreach (i; 0..10) {
        foreach (j; 0..10) {
            if (j == i) continue;
            foreach (k; 0..10) {
                if (k == i || k == j) continue;
                foreach (l; 0..10) {
                    if (l == i || l == j || l == k) continue;
                    perm ~= encode([i, j, k, l]);
                }
            }
        }
    }
    return perm;
}

ushort encode(int[] n) {
    return to!ushort((1<<n[0]) | (1<<n[1]) | (1<<n[2]) | (1<<n[3]));
}

ushort[][] group(ushort q, ushort[] candidates) {
    ushort[][] grouped = new ushort[][](5, 0);
    foreach (c; candidates) {
        grouped[(c & q).popcnt] ~= c;
    }
    return grouped;
}

void query(int[] n) {
    writef("%s %s %s %s\n", n[0], n[1], n[2], n[3]);
    stdout.flush;
}

int[] decode(ushort b) {
    int[] n;
    n ~= b.bsr; b &= ~(1<<n.back);
    n ~= b.bsr; b &= ~(1<<n.back);
    n ~= b.bsr; b &= ~(1<<n.back);
    n ~= b.bsr;
    return n;
}

int reply() {
    int x, y;
    readf("%s %s\n", &x, &y);
    if (4 == x) exit(0);
    return x + y;
}

void detectSequence(ushort b) {
    if (0 == b) exit(1);

    int[] n = decode(b);
    sort(n);
    do {
        query(n);
        reply();
    } while (nextPermutation(n));
}
0