結果

問題 No.3 ビットすごろく
ユーザー nanaenanae
提出日時 2017-06-16 18:44:06
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,826 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 91,796 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 14:31:00
合計ジャッジ時間 3,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 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 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;

immutable int mod = 10^^9 + 7;
immutable int inf = 20000;

int N;

void main() {
    N = readln.chomp.to!int;

    auto d = new int[](N);
    auto q = Queue!(int)(N + 10);

    fill(d, inf);
    d[0] = 1;
    q.push(0);

    int pos;

    while (!q.empty) {
        pos = q.front; q.pop;

        if (pos == N - 1) break;

        int dx = popcnt(pos + 1);

        if (pos + dx < N && d[pos + dx] == inf) {
            d[pos + dx] = d[pos] + 1;
            q.push(pos + dx);
        }

        if (pos - dx >= 0 && d[pos - dx] == inf) {
            d[pos - dx] = d[pos] + 1;
            q.push(pos - dx);
        }
    }

    debug {
        stderr.writeln(d);
    }

    writeln(d[N - 1] < inf ? d[N - 1] : -1);
}

int popcnt(int x) {
    return x > 0 ? popcnt(x>>1) + (x & 1) : 0;
}

unittest {
    assert(popcnt(0) == 0);
    assert(popcnt(3) == 2);
    assert(popcnt(15) == 4);
    assert(popcnt(24) == 2);
    assert(popcnt(7) == 3);
    assert(popcnt(8) == 1);
}

struct Queue(T) {
private:
    int N, head, tail;
    T[] data;

public:
    this(int size)
    {
        N = size + 1;
        data = new T[](N);
    }

    bool empty() @property
    {
        return head == tail;
    }

    bool full() @property
    {
        return (tail + 1) % N == head;
    }

    void push(T x) @property
    {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() @property
    {
        assert(!empty);
        (head += 1) %= N;
    }

    T front() @property
    {
        assert(!empty);
        return data[head];
    }

    void clear() @property
    {
        head = tail = 0;
    }

    int length() @property
    {
        return (tail - head + N) % N;
    }
}
0