結果

問題 No.3 ビットすごろく
ユーザー tubo28tubo28
提出日時 2015-09-09 07:01:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,348 bytes
コンパイル時間 1,480 ms
コンパイル使用メモリ 149,752 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-13 23:23:30
合計ジャッジ時間 2,849 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,504 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> istream& operator>>(istream& is, vector<T>& v);
template<class T> ostream& operator<<(ostream& os, vector<T> const& v);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream&, const tuple<T...>&){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream& os, const tuple<T...>& t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream& operator<<(ostream& os, const tuple<T...>& t){ _ot<0>(os, t); return os; }
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _it(istream&, tuple<T...>&){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _it(istream& is, tuple<T...>& t){ is >> get<n>(t); _it<n+1>(is, t); }
template<class...T> istream& operator>>(istream& is, tuple<T...>& t){ _it<0>(is, t); return is; }
template<class T> istream& operator>>(istream& is, vector<T>& v){ rep(i,v.size()) is >> v[i]; return is; }
template<class T> ostream& operator<<(ostream& os, vector<T> const& v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

template<typename T> inline size_t popcount(T x){
    static_assert(sizeof(x)*8 <= 128,    "popcount : must be 128 bits or shrter.");
    static_assert(is_integral<T>::value, "popcount : must be integer.");
    static const int mask = 65535;
    static size_t dp[66000];
    if(dp[mask] != 16) for(int i = 0; i <= mask; i++) dp[i] = (i&1) + dp[i>>1];
    if(false){
    } else if(sizeof(T)*8 <= 16){
        return
            dp[x];
    } else if(sizeof(T)*8 <= 32){
        return
            dp[(uint32_t)x] + dp[(uint32_t)x>>16];
    } else if(sizeof(T)*8 <= 64){
        return
            dp[(uint64_t)x&mask] + dp[((uint64_t)x>>16)&mask] +
            dp[((uint64_t)x>>32)&mask] + dp[((uint64_t)x>>48)&mask];
    } else {
        return
            dp[((__uint128_t)x)&mask] + dp[((__uint128_t)x>>16)&mask] +
            dp[((__uint128_t)x>>32)&mask] + dp[((__uint128_t)x>>48)&mask] +
            dp[((__uint128_t)x>>64)&mask] + dp[((__uint128_t)x>>80)&mask] +
            dp[((__uint128_t)x>>96)&mask] + dp[((__uint128_t)x>>112)&mask];
    }
}

int main(){
    int N;
    while(cin>>N){
        vi d(N+1,100000);
        d[1] = 1;
        queue<int> q;
        q.push(1);
        while(q.size()){
            int t = q.front();
            q.pop();
            dump(t);
            if(t==N){
                cout << d[t] << endl;
                goto END;
            }
            int k = popcount(t);
            if(t-k > 0 && d[t-k] > d[t]+1){
                d[t-k] = d[t]+1;
                q.push(t-k);
            }
            if(t+k <= N && d[t+k] > d[t]+1){
                d[t+k] = d[t]+1;
                q.push(t+k);
            }
        }
        cout << -1 << endl;
    END:;
    }
}
0