結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2019-04-27 23:41:25 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 26 ms / 5,000 ms |
コード長 | 1,559 bytes |
コンパイル時間 | 3,007 ms |
コンパイル使用メモリ | 107,868 KB |
実行使用メモリ | 25,956 KB |
最終ジャッジ日時 | 2024-07-01 09:19:12 |
合計ジャッジ時間 | 3,100 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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); } } }