結果

問題 No.3 ビットすごろく
ユーザー keymoon
提出日時 2020-02-05 17:35:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,755 bytes
コンパイル時間 1,913 ms
コンパイル使用メモリ 113,268 KB
実行使用メモリ 27,148 KB
最終ジャッジ日時 2024-07-01 09:38:33
合計ジャッジ時間 2,948 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
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