結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
hanbei_dayo
|
| 提出日時 | 2020-07-26 02:22:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,539 bytes |
| コンパイル時間 | 908 ms |
| コンパイル使用メモリ | 101,888 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-01 09:52:49 |
| 合計ジャッジ時間 | 1,915 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
const int inf = (int)1e9;
ll gcd(ll a, ll b) {
if (b > a) {
ll tmp = b;
b = a;
a = tmp;
}
if (a%b == 0)return b;
else return gcd(b, a%b);
}
unsigned long long popcount(unsigned long long x) {
x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) +
(x & 0x5555555555555555UL);
x = ((x & 0xccccccccccccccccUL) >> 2) +
(x & 0x3333333333333333UL);
x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) +
(x & 0x0f0f0f0f0f0f0f0fUL);
x = ((x & 0xff00ff00ff00ff00UL) >> 8) +
(x & 0x00ff00ff00ff00ffUL);
x = ((x & 0xffff0000ffff0000UL) >> 16) +
(x & 0x0000ffff0000ffffUL);
x = ((x & 0xffffffff00000000UL) >> 32) +
(x & 0x00000000ffffffffUL);
return x;
}
ll dp[20010];
int main(void){
for(ll i=0;i<20010;i++)dp[i]=(ll)INF;
dp[1]=1;
int n;
cin>>n;
if(n==1){
cout<<1<<endl;
return 0;
}
queue<ll>que;
que.push(1);
while(!que.empty()){
ll i = que.front();
que.pop();
if(i>n)continue;
ll t = popcount(i);
if(dp[i+t]>dp[i]+1){
dp[i+t]=dp[i]+1;
que.push(i+t);
}
if((i-t>=1)&&dp[i-t]>dp[i]+1){
dp[i-t]=dp[i]+1;
que.push(i-t);
}
}
if(dp[n]==(ll)INF)cout<<-1<<endl;
else cout<<dp[n]<<endl;
return 0;
}
hanbei_dayo