結果

問題 No.3 ビットすごろく
ユーザー しらゆきしらゆき
提出日時 2016-01-11 11:48:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 2,796 bytes
コンパイル時間 6,100 ms
コンパイル使用メモリ 104,800 KB
実行使用メモリ 22,924 KB
最終ジャッジ日時 2023-09-13 23:34:22
合計ジャッジ時間 5,636 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
20,776 KB
testcase_01 AC 57 ms
20,684 KB
testcase_02 AC 58 ms
20,800 KB
testcase_03 AC 59 ms
20,672 KB
testcase_04 AC 58 ms
20,832 KB
testcase_05 AC 58 ms
20,900 KB
testcase_06 AC 58 ms
20,776 KB
testcase_07 AC 59 ms
20,876 KB
testcase_08 AC 59 ms
20,836 KB
testcase_09 AC 58 ms
20,784 KB
testcase_10 AC 58 ms
20,908 KB
testcase_11 AC 58 ms
22,840 KB
testcase_12 AC 59 ms
20,668 KB
testcase_13 AC 58 ms
22,832 KB
testcase_14 AC 59 ms
22,924 KB
testcase_15 AC 59 ms
20,704 KB
testcase_16 AC 58 ms
22,768 KB
testcase_17 AC 56 ms
18,836 KB
testcase_18 AC 59 ms
20,680 KB
testcase_19 AC 58 ms
20,876 KB
testcase_20 AC 58 ms
22,892 KB
testcase_21 AC 56 ms
20,712 KB
testcase_22 AC 57 ms
20,660 KB
testcase_23 AC 58 ms
22,836 KB
testcase_24 AC 57 ms
22,840 KB
testcase_25 AC 58 ms
20,752 KB
testcase_26 AC 56 ms
20,724 KB
testcase_27 AC 58 ms
22,728 KB
testcase_28 AC 58 ms
18,628 KB
testcase_29 AC 59 ms
20,720 KB
testcase_30 AC 58 ms
18,616 KB
testcase_31 AC 58 ms
20,712 KB
testcase_32 AC 58 ms
20,900 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.Linq;
using System.Collections.Generic;

class Program
{    
    static void Main()
    {
        var n = ri();

        var cnt = new int[n + 1];
        for (int i = 0; i < n + 1; i++) cnt[i] = -1;
        cnt[1] = 1;

        var q = new Queue<int>();
        q.Enqueue(1);          

        while (q.Count > 0)
        {
            var p = q.Dequeue();
            if (p == n) break;
            int d = bitcount(p);
            if (p - d > 0)
                if (cnt[p - d] == -1 || (cnt[p - d] > cnt[p] + 1))
                {
                    q.Enqueue(p - d);
                    cnt[p - d] = cnt[p] + 1;
                }
            if (p + d <= n)
                if (cnt[p + d] == -1 || (cnt[p + d] > cnt[p] + 1))
                {
                    q.Enqueue(p + d);
                    cnt[p + d] = cnt[p] + 1;
                }            
        }

        Console.WriteLine(cnt[n]);
    }

    static int bitcount(int n)
    {
        int c = 0;
        while (n > 0)
        {
            c++;
            n &= n - 1;
        }
        return c;
    }
    #region Scan
    static int ri() { return int.Parse(Console.ReadLine().Trim()); }
    static long rl() { return long.Parse(Console.ReadLine().Trim()); }
    static double rd() { return double.Parse(Console.ReadLine().Trim()); }
    static string rs() { return Console.ReadLine().Trim(); }
    static int[] ria() { return Console.ReadLine().Trim().Split().Select(int.Parse).ToArray(); }
    static long[] rla() { return Console.ReadLine().Trim().Split().Select(long.Parse).ToArray(); }
    static double[] rda() { return Console.ReadLine().Trim().Split().Select(double.Parse).ToArray(); }
    static string[] rsa() { return Console.ReadLine().Trim().Split(); }
    static void mul(out int a, out int b) { var arr = ria(); a = arr[0]; b = arr[1]; }
    static void mul(out int a, out int b, out int c) { var arr = ria(); a = arr[0]; b = arr[1]; c = arr[2]; }
    static void mul(out int a, out int b, out int c, out int d) { var arr = ria(); a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3]; }
    static void mul(out long a, out long b) { var arr = rla(); a = arr[0]; b = arr[1]; }
    static void mul(out long a, out long b, out long c) { var arr = rla(); a = arr[0]; b = arr[1]; c = arr[2]; }
    static void mul(out long a, out long b, out long c, out long d) { var arr = rla(); a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3]; }
    #endregion
    #region Math
    const int mod = 1000000007;
    static bool isprime(long n){ if (n < 2) return false; for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; }
    static long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); }
    static long lcm(long a, long b) { return a * b / gcd(a, b); }
    #endregion
}
0