結果

問題 No.2449 square_permutation
ユーザー tentententen
提出日時 2024-04-03 14:07:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,483 ms / 2,000 ms
コード長 1,856 bytes
コンパイル時間 2,989 ms
コンパイル使用メモリ 84,052 KB
実行使用メモリ 116,488 KB
最終ジャッジ日時 2024-04-03 14:08:08
合計ジャッジ時間 27,383 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
54,652 KB
testcase_01 AC 80 ms
54,756 KB
testcase_02 AC 78 ms
54,628 KB
testcase_03 AC 76 ms
54,860 KB
testcase_04 AC 79 ms
54,876 KB
testcase_05 AC 87 ms
55,144 KB
testcase_06 AC 555 ms
79,480 KB
testcase_07 AC 650 ms
82,680 KB
testcase_08 AC 579 ms
80,920 KB
testcase_09 AC 685 ms
85,240 KB
testcase_10 AC 967 ms
98,596 KB
testcase_11 AC 1,077 ms
107,460 KB
testcase_12 AC 1,124 ms
109,836 KB
testcase_13 AC 1,420 ms
116,160 KB
testcase_14 AC 1,448 ms
116,184 KB
testcase_15 AC 1,441 ms
116,372 KB
testcase_16 AC 1,444 ms
116,376 KB
testcase_17 AC 1,443 ms
116,372 KB
testcase_18 AC 1,310 ms
109,840 KB
testcase_19 AC 1,424 ms
113,976 KB
testcase_20 AC 1,465 ms
116,376 KB
testcase_21 AC 1,483 ms
116,488 KB
testcase_22 AC 74 ms
54,872 KB
testcase_23 AC 1,436 ms
114,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        Map<Integer, Deque<Integer>> positions = new HashMap<>();
        int[] values = new int[n + 1];
        for (int i = n; i >= 1; i--) {
            values[i] = i;
            for (int j = 2; j <= Math.sqrt(values[i]); j++) {
                while (values[i] % (j * j) == 0) {
                    values[i] /= j * j;
                }
            }
            if (!positions.containsKey(values[i])) {
                positions.put(values[i], new ArrayDeque<>());
            }
            positions.get(values[i]).add(i);
        }
        String result = IntStream.rangeClosed(1, n).mapToObj(i -> {
            return positions.get(values[i]).poll().toString();
        }).collect(Collectors.joining(" "));
        System.out.println(result);
    }
}
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