結果

問題 No.3 ビットすごろく
コンテスト
ユーザー ryo ryo
提出日時 2023-08-15 16:21:20
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 893 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 272 ms
コンパイル使用メモリ 39,640 KB
最終ジャッジ日時 2026-02-22 11:10:34
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<stdio.h>

int func(int x){
    int cnt=0;
    
    while(x){
        cnt+=x%2;
        x/=2;
    }
    
    return cnt;
}
int main(){
    
    int n;
    scanf("%d",&n);
    
    int dp[10002]={};
    dp[1]=1;
    
    int q[10002]={};
    int ql=0,qr=1;
    q[0]=1;
    
    
    
    while(qr!=ql){
        int p=q[ql]; ql++;
        
        int t=func(p);
        //printf("[%d %d]",p,t);
        
        for(int i=-t;i<=t;i++){
            for(int j=-t;j<=t;j++){
                if(i+j!=t && i+j!=-t)continue;
                
                if(p+i+j<=n && (dp[p+i+j]==0 || dp[p+i+j] > dp[p]+1)){
                  //  printf("[%d %d]",p,p+i+j);
                    dp[p+i+j]=dp[p]+1;
                    q[qr]=p+i+j;
                    qr++;
                }
            }
        }
    }
    
    if(dp[n]!=0)printf("%d",dp[n]);
    else printf("-1");
    
    return 0;
}
0