結果

問題 No.2751 429-like Number
ユーザー tenten
提出日時 2024-05-13 15:19:50
言語 Java
(openjdk 23)
結果
AC  
実行時間 2,202 ms / 4,000 ms
コード長 2,507 bytes
コンパイル時間 3,906 ms
コンパイル使用メモリ 84,672 KB
実行使用メモリ 65,536 KB
最終ジャッジ日時 2024-12-20 09:28:38
合計ジャッジ時間 23,711 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static List<Integer> primes = new ArrayList<>();
    static List<Long> squares = new ArrayList<>();
    static List<Long> cubes = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        boolean[] isNotPrimes = new boolean[100000];
        for (int i = 2; i < isNotPrimes.length; i++) {
            if (!isNotPrimes[i]) {
                primes.add(i);
                squares.add((long)i * i);
                cubes.add((long)i * i * i);
            }
        }
        int q = sc.nextInt();
        String result = IntStream.range(0, q).mapToObj(i -> is429(sc.nextLong()) ? "Yes" : "No")
            .collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static boolean is429(long x) {
        for (int i = 0; i < primes.size() && cubes.get(i) <= x; i++) {
            if (x % primes.get(i) == 0 ) {
                return isDouble(i, x / primes.get(i));
            }
        }
        return false;
    }
    
    static boolean isDouble(int idx, long x) {
        for (int i = idx; i < primes.size() && squares.get(i) <= x; i++) {
            if (x % primes.get(i) == 0) {
                return isPrime(i, x / primes.get(i));
            }
        }
        return false;
    }
    
    static boolean isPrime(int idx, long x) {
        for (int i = idx; i < primes.size() && squares.get(i) <= x; i++) {
            if (x % primes.get(i) == 0) {
                return false;
            }
        }
        return true;
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0