結果

問題 No.3 ビットすごろく
ユーザー nanophoto12nanophoto12
提出日時 2015-12-30 22:53:29
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,157 bytes
コンパイル時間 1,078 ms
コンパイル使用メモリ 89,684 KB
実行使用メモリ 814,852 KB
最終ジャッジ日時 2023-10-19 12:43:55
合計ジャッジ時間 5,677 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

#define FOR(x,y) for(int x = 0;x < y;x++)
#define LLI long long int

template<int um> class UF {
public:
    vector<int> par,rank,cnt;
    UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
    void reinit() {FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
    int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
    int count(int x) { return cnt[operator[](x)];}
    int operator()(int x,int y) {
        if((x=operator[](x))==(y=operator[](y))) return x;
        cnt[y]=cnt[x]=cnt[x]+cnt[y];
        if(rank[x]>rank[y]) return par[x]=y;
        rank[x]+=rank[x]==rank[y]; return par[y]=x;
    }
};

static int CountBit(unsigned int i)
{
    int count = 0;
    while(i > 0)
    {
        if((i & 1) == 1)
        {
            count++;
        }
        i >>= 1;
    }
    return count;
}

int main() {
    int n;
    cin >> n;
    queue<int> buffer;
    vector<bool> visited(n + 1);
    buffer.push(1);
    FOR(i, n + 1)
    {
        visited[i] = false;
    }
    visited[1] = true;
    if(n == 1)
    {
        cout << 1 << endl;
        return 0;
    }
    int count = 1;
    while(!buffer.empty())
    {
        queue<int> next;
        count++;
        while(!buffer.empty())
        {
            int top = buffer.front();
            buffer.pop();
            int bits = CountBit(top);
            int plus = top + bits;
            if(plus == n)
            {
                cout << count << endl;
                return 0;
            }
            if(plus >= 1 && plus <= n)
            {
                if(!visited[plus])
                {
                    next.push(plus);
                }
            }
            int minus = top - bits;
            if(minus >= 1 && minus <= n)
            {
                if(!visited[minus])
                {
                    next.push(minus);
                }
            }
        }
        buffer = next;
    }
    cout << -1 << endl;
    return 0;
}
0