結果

問題 No.3 ビットすごろく
ユーザー keymoonkeymoon
提出日時 2020-02-05 17:35:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 5,000 ms
コード長 1,755 bytes
コンパイル時間 3,015 ms
コンパイル使用メモリ 106,680 KB
実行使用メモリ 23,904 KB
最終ジャッジ日時 2023-09-14 01:35:44
合計ジャッジ時間 5,414 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
21,616 KB
testcase_01 AC 59 ms
21,712 KB
testcase_02 AC 60 ms
21,908 KB
testcase_03 AC 60 ms
23,624 KB
testcase_04 AC 58 ms
23,700 KB
testcase_05 AC 60 ms
21,844 KB
testcase_06 AC 60 ms
21,556 KB
testcase_07 AC 59 ms
21,824 KB
testcase_08 AC 59 ms
21,772 KB
testcase_09 AC 60 ms
23,644 KB
testcase_10 AC 59 ms
19,832 KB
testcase_11 AC 59 ms
19,576 KB
testcase_12 AC 60 ms
21,788 KB
testcase_13 AC 60 ms
21,540 KB
testcase_14 AC 60 ms
21,708 KB
testcase_15 AC 60 ms
19,680 KB
testcase_16 AC 60 ms
21,652 KB
testcase_17 AC 61 ms
23,620 KB
testcase_18 AC 60 ms
23,832 KB
testcase_19 AC 61 ms
23,896 KB
testcase_20 AC 59 ms
21,916 KB
testcase_21 AC 60 ms
21,704 KB
testcase_22 AC 60 ms
21,796 KB
testcase_23 AC 61 ms
21,664 KB
testcase_24 AC 59 ms
21,692 KB
testcase_25 AC 59 ms
21,544 KB
testcase_26 AC 58 ms
21,772 KB
testcase_27 AC 60 ms
21,640 KB
testcase_28 AC 59 ms
21,704 KB
testcase_29 AC 60 ms
21,704 KB
testcase_30 AC 60 ms
23,904 KB
testcase_31 AC 61 ms
23,604 KB
testcase_32 AC 61 ms
21,924 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

static class P
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int[] step = Enumerable.Repeat(int.MaxValue, n + 1).ToArray();
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(1);
        step[1] = 1;
        while (queue.Count > 0)
        {
            var elem = queue.Dequeue();
            var popCount = PopCount(elem);

            if (1 <= elem - popCount) 
                if (step[elem] + 1 < step[elem - popCount])
                {
                    queue.Enqueue(elem - popCount);
                    step[elem - popCount] = step[elem] + 1;
                }

            if (elem + popCount < step.Length)
                if (step[elem] + 1 < step[elem + popCount])
                {
                    queue.Enqueue(elem + popCount);
                    step[elem + popCount] = step[elem] + 1;
                }
        }
        if (step.Last() == int.MaxValue)
        {
            Console.WriteLine(-1);
            return;
        }
        Console.WriteLine(step.Last());
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static int PopCount(int n)
    {
        n = n - ((n >> 1) & 0x55555555);
        n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
        return (int)(((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
    }
}
0