結果

問題 No.3 ビットすごろく
ユーザー hisataka7hisataka7
提出日時 2020-11-30 21:41:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 3,023 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 172,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:54:59
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define reps(i,n) for(ll i=1;i<ll(n);i++)
#define rrep(i,n) for(ll i=ll(n);i>=0;i--)
#define ALL(x) x.begin(),x.end()
#define FOR(i,a,b) for(ll i=a;i<=b;i++)//使いにくかったら消す
#define len(x) x.length()//配列の長さ
#define SIZE(x) x.size()//vectorのサイズ
#define NPOS string::npos//findで検索失敗した場合の戻り値(findは文字列内に特定の文字列があるかを判定)
typedef long long ll;
using namespace std;
const ll INF=1LL<<60;//無限大
ll ans=0;            //解答用の変数

bool comp_len(const std::string &lh,const std::string &rh){return lh.length() < rh.length();}  //文字列を長さでソートする時の比較関数
ll gcd(ll a, ll b) {if (b==0) return a;else return gcd(b, a%b);}                               //最大公約数
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}                                                 //最小公倍数
ll digit_sum(ll a,ll b){ll ans=0;while(a>0){ans+=a%b;a/=b;}return ans;}                        //n進桁和
ll digit_num(ll a,ll b){ll ans=0;while(a>0){ans++;a/=b;}return ans;}                           //n進桁数
ll fact(ll n){if(n==1)return 1;else return n*fact(n-1);}                                       //階乗
ll array_sum(ll *a,ll n){ll ans=0;rep(i,n){ans+=a[i];}return ans;}                             //配列総和
ll combi(ll n, ll k){if(n==k||k==0){return 1;}else{return combi(n-1,k-1)+combi(n-1,k);}}       //nCkの組み合わせ
bool is_prime(ll x){for(ll i=2;i*i<=x;i++){if(x%i==0)return false;}return true;}               //素数判定
// for(long long i=1;i*i<= n;i++){if(n%i==0){ans.push_back(i);if(i*i!=n)ans.push_back(n/i);}}  //約数列挙(vector型の変数の用意が必要)

/*dp(動的計画法)*/
template <typename T>bool chmax(T &a, const T& b){if(a<b){a=b;return true;}return false;}//a<bならaをbに置き換え
template <typename T>bool chmin(T &a, const T& b){if(a>b){a=b;return true;}return false;}//a>bならaをbで置き換え

/*Union-Find(素集合データ構造)*/
vector<ll> par(100010);//親の値を保持
ll root(ll x){if(par[x]==x){return x;}else{return par[x]=root(par[x]);}}//根の値を返す
bool same(ll x,ll y){return (root(x)==root(y));}//根が一致しているか判定
void unite(ll x,ll y){x=root(x);y=root(y);if(x==y)return;par[x]=y;}//グループを併合

ll bit(ll n){
  ll tmp=0;
  while(n>0){
    if(n%2==1)tmp++;
    n/=2;
  }
  return tmp;
}


int main()
{
  ll n;
  cin>>n;
  ll done[10010]={0};
  queue<ll> x;
  x.push(1);
  done[1]=1;
  while(!x.empty()){
    ll tmp=x.front();
    x.pop();
    if(tmp-bit(tmp)>=0&&tmp-bit(tmp)<=n&&done[tmp-bit(tmp)]==0){
      x.push(tmp-bit(tmp));
      done[tmp-bit(tmp)]=done[tmp]+1;
    }
    if(tmp+bit(tmp)>=0&&tmp+bit(tmp)<=n&&done[tmp+bit(tmp)]==0){
      x.push(tmp+bit(tmp));
      done[tmp+bit(tmp)]=done[tmp]+1;
    }
  }
  // cout<<bit(1)<<endl;
  if(done[n]==0)puts("-1");
  else cout<<done[n]<<endl;
  return 0;
}
0