結果

問題 No.3 ビットすごろく
ユーザー taktak
提出日時 2019-04-27 23:41:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 1,559 bytes
コンパイル時間 951 ms
コンパイル使用メモリ 61,316 KB
実行使用メモリ 22,796 KB
最終ジャッジ日時 2023-09-14 01:17:25
合計ジャッジ時間 4,043 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
20,660 KB
testcase_01 AC 53 ms
22,796 KB
testcase_02 AC 53 ms
18,588 KB
testcase_03 AC 55 ms
20,760 KB
testcase_04 AC 56 ms
18,680 KB
testcase_05 AC 55 ms
20,664 KB
testcase_06 AC 54 ms
20,676 KB
testcase_07 AC 52 ms
20,748 KB
testcase_08 AC 56 ms
22,732 KB
testcase_09 AC 56 ms
20,684 KB
testcase_10 AC 56 ms
20,840 KB
testcase_11 AC 56 ms
22,716 KB
testcase_12 AC 56 ms
20,640 KB
testcase_13 AC 55 ms
22,780 KB
testcase_14 AC 57 ms
18,672 KB
testcase_15 AC 56 ms
20,744 KB
testcase_16 AC 56 ms
20,660 KB
testcase_17 AC 55 ms
18,616 KB
testcase_18 AC 54 ms
20,604 KB
testcase_19 AC 56 ms
18,612 KB
testcase_20 AC 54 ms
20,588 KB
testcase_21 AC 55 ms
20,636 KB
testcase_22 AC 56 ms
20,752 KB
testcase_23 AC 57 ms
20,720 KB
testcase_24 AC 56 ms
22,732 KB
testcase_25 AC 56 ms
20,812 KB
testcase_26 AC 54 ms
20,740 KB
testcase_27 AC 53 ms
20,712 KB
testcase_28 AC 55 ms
20,636 KB
testcase_29 AC 55 ms
20,656 KB
testcase_30 AC 54 ms
20,736 KB
testcase_31 AC 54 ms
22,708 KB
testcase_32 AC 55 ms
20,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YukiCoderCS
{
    class Program
    {
        public static void Main(string[] args) => new Program().Solve();

        int bitCount(int x)
        {
            x = x - ((x >> 1) & 0x55555555);
            x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
            x = (x + (x >> 4)) & 0x0f0f0f0f;
            x = x + (x >> 8);
            x = x + (x >> 16);
            return x & 0x3f;
        }

        void Solve()
        {
            var N = int.Parse(Console.ReadLine());
            var minCosts = new int[N + 1];
            var start = 1;
            var que = new Queue<int>();
            var ans = -1;
            minCosts[start] = 1;
            que.Enqueue(start);
            while (que.Count > 0)
            {
                var x = que.Dequeue();
                if (x == N)
                {
                    ans = minCosts[x];
                    break;
                }
                var bit = bitCount(x);
                var f = x - bit;
                var b = x + bit;
                if (f >= 1 && minCosts[f] == 0)
                {
                    que.Enqueue(f);
                    minCosts[f] = minCosts[x] + 1;
                }
                if (b <= N && minCosts[b] == 0)
                {
                    que.Enqueue(b);
                    minCosts[b] = minCosts[x] + 1;
                }
            }
            Console.WriteLine(ans);                
        }
    }
}
0