結果

問題 No.3 ビットすごろく
コンテスト
ユーザー MoonOnRain
提出日時 2018-05-21 12:21:38
言語 C#(csc)
(csc 3.9.0)
コンパイル:
csc -langversion:latest -unsafe -warn:0 -o+ /r:System.Numerics.dll _filename_ -out:a.exe
実行:
/usr/bin/mono a.exe
結果
WA  
実行時間 -
コード長 1,312 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 790 ms
コンパイル使用メモリ 110,992 KB
実行使用メモリ 27,600 KB
最終ジャッジ日時 2026-03-17 11:50:32
合計ジャッジ時間 1,912 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #
raw source code

using System;
using System.Collections.Generic;
using System.Linq;

public class Amatsuki{
    public static void Main(){
        var target = int.Parse(Console.ReadLine());
        
        var pass = new int[0];
        var result = new List<int>();
        
        check(1, target, pass, ref result);
        
        if(result.Any()){
            Console.WriteLine(result.Count);
        }
        else{
            Console.WriteLine(-1);
        }
    }
    
    public static void check(int now, int target, int[] pass, ref List<int> result){
        var passList = pass.ToList();
        passList.Add(now);
        
        if(now == target && passList.Count < result.Count){
            result = passList;
        }
        
        if(passList.Contains(now)){
            return;
        }
        
        var bit = bitCount(now);
        
        if(now + bit <= target){
            check(now + bit, target, passList.ToArray(), ref result);
        }
        
        if(now - bit > 1){
            check(now - bit, target, passList.ToArray(), ref result);
        }
    }
    
    public static int bitCount(int dec){
        var result = 0;
        while(dec > 0){
            if(dec % 2 == 1){
                result++;
            }
            dec /= 2;
        }
        return result;
    }
}
0