結果

問題 No.3 ビットすごろく
ユーザー tubo28tubo28
提出日時 2015-09-09 17:59:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 4,078 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 201,248 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2024-07-01 07:32:56
合計ジャッジ時間 3,413 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
8,704 KB
testcase_01 AC 7 ms
8,448 KB
testcase_02 AC 7 ms
8,704 KB
testcase_03 AC 8 ms
8,576 KB
testcase_04 AC 7 ms
8,576 KB
testcase_05 AC 7 ms
8,576 KB
testcase_06 AC 7 ms
8,704 KB
testcase_07 AC 7 ms
8,576 KB
testcase_08 AC 8 ms
8,704 KB
testcase_09 AC 8 ms
8,448 KB
testcase_10 AC 7 ms
8,448 KB
testcase_11 AC 7 ms
8,704 KB
testcase_12 AC 7 ms
8,576 KB
testcase_13 AC 7 ms
8,576 KB
testcase_14 AC 8 ms
8,576 KB
testcase_15 AC 7 ms
8,576 KB
testcase_16 AC 8 ms
8,704 KB
testcase_17 AC 8 ms
8,576 KB
testcase_18 AC 7 ms
8,576 KB
testcase_19 AC 7 ms
8,704 KB
testcase_20 AC 7 ms
8,704 KB
testcase_21 AC 7 ms
8,704 KB
testcase_22 AC 8 ms
8,704 KB
testcase_23 AC 7 ms
8,576 KB
testcase_24 AC 7 ms
8,704 KB
testcase_25 AC 8 ms
8,704 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 7 ms
8,576 KB
testcase_28 AC 7 ms
8,576 KB
testcase_29 AC 7 ms
8,704 KB
testcase_30 AC 7 ms
8,704 KB
testcase_31 AC 6 ms
8,576 KB
testcase_32 AC 7 ms
8,576 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘size_t popcount(T) [with T = int; size_t = long unsigned int]’:
main.cpp:103:25:   required from ‘int solve(T) [with T = int]’
main.cpp:119:33:   required from here
main.cpp:86:44: warning: right shift count >= width of type [-Wshift-count-overflow]
   86 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M];
      |                                           ~^~~~
main.cpp:86:58: warning: right shift count >= width of type [-Wshift-count-overflow]
   86 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M];
      |                                                         ~^~~~
main.cpp:88:44: warning: right shift count >= width of type [-Wshift-count-overflow]
   88 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M];
      |                                           ~^~~~
main.cpp:88:58: warning: right shift count >= width of type [-Wshift-count-overflow]
   88 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M];
      |                                                         ~^~~~
main.cpp:88:72: warning: right shift count >= width of type [-Wshift-count-overflow]
   88 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M];
      |                                                                       ~^~~~
main.cpp:88:86: warning: right shift count >= width of type [-Wshift-count-overflow]
   88 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M];
      |                                                                                     ~^~~~
main.cpp:88:100: warning: right shift count >= width of type [-Wshift-count-overflow]
   88 |         return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) begin(c), end(c)
#define range(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) range(i,0,b)
#define rangei(i,a,b) for(ll a=a;i<=ll(b);i++)
#define repi(i,b) rangei(i,1,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> ostream & operator << (ostream &os, vector<T> const &);
template<int n, class...T>
typename enable_if<(n>=sizeof...(T))>::type
_ot(ostream &, tuple<T...> const &){}
template<int n, class...T>
typename enable_if<(n< sizeof...(T))>::type
_ot(ostream &os, tuple<T...> const &t){
    os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t);
}
template<class...T>
ostream & operator << (ostream &os, tuple<T...> const &t){
    _ot<0>(os, t); return os;
}
template<class T, class U>
ostream & operator<<(ostream &os, pair<T,U> const &p){
    return os << "(" << p.first << ", " << p.second << ") ";
}
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
void fastios(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    // #define endl '\n'
}
template<class T>
size_t uniq(vector<T> &v){
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    return v.size();
}
template<class T>
size_t uniq(T *l, size_t n){
    sort(l,l+n);
    return unique(l,l+n) - l;
}
#define mems(arr,val) memset(arr,val,sizeof(arr));
int const mod = 1000000007;
int const inf = numeric_limits<int>::max()/8;

template<typename T> inline size_t popcount(T x){
    static_assert(is_integral<T>::value,
                  "popcount : must be integer.");
    static_assert(sizeof(x) == sizeof(int8_t) ||
                  sizeof(x) == sizeof(int16_t) ||
                  sizeof(x) == sizeof(int32_t) ||
                  sizeof(x) == sizeof(int64_t) ||
                  sizeof(x) == sizeof(__int128_t),
                  "popcount : must be 8, 16, 32, 64, or 128 bits.");
    static const int M = 65535;
    static size_t dp[66000];
    if(dp[M] != 16) for(int i = 0; i <= M; i++) dp[i] = (i&1) + dp[i>>1];
    switch(sizeof(T)){
    case sizeof(int8_t):
        return dp[x];
    case sizeof(int16_t):
        return dp[(int8_t)x];
    case sizeof(int32_t):
        return dp[x&M] + dp[x>>16&M];
    case sizeof(int64_t):
        return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M];
    case sizeof(__int128_t):
        return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M];
    }
}

template<class T>
int solve(T n){
    queue<T> q;
    q.push(1);
    int dist[11111];
    fill_n(dist,11111,1e9);
    dist[1] = 1;
    while(q.size()){
        T c = q.front();
        q.pop();
        if(c == n) return dist[c];
        int d = popcount(c);
        for(int w : {1,-1}){
            T nx = c + d*w;
            if(1 <= nx && nx <= n && dist[nx] > dist[c] + 1){
                dist[nx] = dist[c] + 1;
                q.push(nx);
            }
        }
    }
    return -1;
}

int main(){
    int N;
    while(cin >> N){
        vector<int> ans;
        ans.pb(solve<signed int>(N));
        ans.pb(solve<signed char>(N));
        ans.pb(solve<signed short>(N));
        ans.pb(solve<signed long>(N));
        ans.pb(solve<signed long long>(N));
        ans.pb(solve<unsigned int>(N));
        ans.pb(solve<unsigned char>(N));
        ans.pb(solve<unsigned short>(N));
        ans.pb(solve<unsigned long>(N));
        ans.pb(solve<unsigned long long>(N));
        dump(ans);
        // assert(all_of(ans.begin(),ans.end(),[&](int a){
        //             return a == ans[0];
        //         }));
        cout << ans[0] << endl;
    }
}
0