結果
| 問題 | No.1450 nahco314's First Problem | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2021-04-12 18:07:33 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,389 bytes | 
| コンパイル時間 | 4,915 ms | 
| コンパイル使用メモリ | 76,628 KB | 
| 実行使用メモリ | 37,260 KB | 
| 最終ジャッジ日時 | 2024-11-08 14:28:17 | 
| 合計ジャッジ時間 | 7,144 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 WA * 2 | 
ソースコード
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long n = sc.nextLong();
        int count = getCount(n);
        for (long i = Math.max(1, n - count); i <= n + count; i++) {
            if ((i ^ getHumming(i)) == n) {
                System.out.println(i);
                return;
            }
        }
        System.out.println(-1);
    }
    
    static int getHumming(long x) {
        long count = 0;
        while (x > 0) {
            count += x % 2;
            x /= 2;
        }
        return (int)count;
    }
    
    static int getCount(long x) {
        int count = 0;
        while (x > 0) {
            count++;
            x >>= 1;
        }
        return count;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
            
            
            
        