結果

問題 No.262 面白くないビットすごろく
ユーザー eitahoeitaho
提出日時 2015-08-07 19:55:52
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 3,426 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 109,728 KB
実行使用メモリ 23,948 KB
最終ジャッジ日時 2023-09-25 05:53:46
合計ジャッジ時間 4,282 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 210 ms
22,016 KB
testcase_01 AC 303 ms
23,948 KB
testcase_02 AC 297 ms
22,068 KB
testcase_03 AC 297 ms
19,896 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.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static int K = 18;
    static int MaxLeftBits = 40 - K;

    public void Solve()
    {
        long N = Reader.Long();
        var count = new int[MaxLeftBits + 1, 64];
        var diff = new long[MaxLeftBits + 1, 64];
        var sw = Stopwatch.StartNew();

        for (int bits = 0; bits <= MaxLeftBits; bits++)
            for (int mod = 0; mod < 64; mod++)
            {
                if (bits == 0 && mod == 0) continue;
                int x = mod;
                while (x < (1 << K))
                {
                    x += bits + BitCount32(x);
                    count[bits, mod]++;
                }
                diff[bits, mod] = x - mod;
            }
        long ans = 1;
        long i = 1;
        for (int left = 0; left < N / (1 << K); left++)
        {
            ans += count[BitCount32(left), i % 64];
            i += diff[BitCount32(left), i % 64];
        }
        while (i < N)
        {
            ans++;
            i += BitCount64(i);
        }
        Console.WriteLine((i == N) ? ans : -1);
    }

    static int BitCount32(int x)
    {
        x -= x >> 1 & 0x55555555;
        x = (x & 0x33333333) + (x >> 2 & 0x33333333);
        return ((x + (x >> 4) & 0x0f0f0f0f) * 0x01010101) >> 24;
    }
    static int BitCount64(long x)
    {
        x -= x >> 1 & 0x5555555555555555;
        x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
        return (int)((x + (x >> 4) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101 >> 56);
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0