結果

問題 No.262 面白くないビットすごろく
ユーザー mamekinmamekin
提出日時 2016-01-30 15:50:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,339 bytes
コンパイル時間 925 ms
コンパイル使用メモリ 99,744 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 17:58:11
合計ジャッジ時間 1,654 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 111 ms
4,348 KB
testcase_02 AC 111 ms
4,348 KB
testcase_03 AC 110 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int main()
{
    long long n;
    cin >> n;

    long long a = 1;
    long long cnt = 1;
    map<pair<int, int>, pair<long long, int> > memo;
    while(a < n){
        int high = bitset<32>(a >> 20).count();
        int low = a & ((1 << 20) - 1);

        if(memo.find(make_pair(high, low)) != memo.end()){
            long long b = a + memo[make_pair(high, low)].first;
            if(b <= n){
                a = b;
                cnt += memo[make_pair(high, low)].second;
                continue;
            }
        }

        long long b = a;
        int add = 0;
        while(b < n && (a >> 20) == (b >> 20)){
            ++ add;
            b += high + bitset<32>(b & ((1 << 20) - 1)).count();
        }
        if((a >> 20) != (b >> 20))
            memo[make_pair(high, low)] = make_pair(b - a, add);
        a = b;
        cnt += add;
    }

    if(a == n)
        cout << cnt << endl;
    else
        cout << -1 << endl;

    return 0;
}
0