結果

問題 No.1871 divisXor
ユーザー tenten
提出日時 2022-03-18 12:52:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 2,062 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 78,340 KB
実行使用メモリ 37,932 KB
最終ジャッジ日時 2024-10-02 15:48:48
合計ジャッジ時間 7,701 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        if (n == 0) {
            System.out.println(-1);
            return;
        }
        ArrayList<Long> ans = new ArrayList<>();
        for (int i = 60; i >= 0 && n > 4; i--) {
            if (n < (1L << i)) {
                continue;
            }
            long x = (1L << i) + 1;
            while (!isPrime(x)) {
                x += 2;
            }
            ans.add(x);
            n ^= x + 1;
        }
        if (n == 3) {
            ans.add(2L);
        } else if (n == 2) {
            ans.add(2L);
            ans.add(1L);
        } else if (n == 1) {
            ans.add(1L);
        } else if (n == 4) {
            ans.add(4L);
            ans.add(2L);
        }
        System.out.println(ans.size());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ans.size(); i++) {
            if (i > 0) {
                sb.append(" ");
            }
            sb.append(ans.get(i));
        }
        System.out.println(sb);
    }
    
    static boolean isPrime(long x) {
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }
}
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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0