結果

問題 No.1869 Doubling?
ユーザー planes
提出日時 2022-03-11 22:57:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 863 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 169,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 03:20:24
合計ジャッジ時間 2,952 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)


int main() {
ll N,M;cin>>N>>M;
bool c=false;
ll now=1;
ll count=0;
for(ll i=2;i<=N;i++) {
  now*=2;
  count++;
  if(now>M) {
    c=true;
    break;
  }
}

if(!c) {
  cout<<(1LL<<N)-1<<endl;
  return 0;
}
ll ans=0;
ans+=N-count;
if(1LL<<(count-1)==M) {
  cout<<ans-1+(1LL<<count)<<endl;
return 0;
}


vector<ll> vec(0);
bool a=false;

for(ll i=0;i<count;i++) {
  if(M&(1LL<<i)) {
    if(!a) {
      a=true;
      vec.push_back(i);
    }
}
else {
  if(a) vec.push_back(i);
}
}

reverse(all(vec));

   now=1;
ll ind=0;
for(ll i=1;i<=count;i++) {
  now*=2;
  if(ind<vec.size()) {
    if(vec[ind]==count-i) {
      ind++;
      now--;
    }
  }

  ans+=now;
}
cout<<ans<<endl;
}



0