結果

問題 No.2449 square_permutation
ユーザー tentententen
提出日時 2023-09-25 21:19:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 731 ms / 2,000 ms
コード長 2,316 bytes
コンパイル時間 2,750 ms
コンパイル使用メモリ 91,196 KB
実行使用メモリ 118,668 KB
最終ジャッジ日時 2023-09-25 21:19:31
合計ジャッジ時間 16,869 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
50,540 KB
testcase_01 AC 57 ms
50,452 KB
testcase_02 AC 56 ms
50,684 KB
testcase_03 AC 58 ms
50,952 KB
testcase_04 AC 58 ms
51,368 KB
testcase_05 AC 65 ms
50,820 KB
testcase_06 AC 292 ms
80,864 KB
testcase_07 AC 327 ms
80,872 KB
testcase_08 AC 362 ms
79,504 KB
testcase_09 AC 360 ms
85,524 KB
testcase_10 AC 478 ms
105,068 KB
testcase_11 AC 512 ms
109,380 KB
testcase_12 AC 487 ms
105,612 KB
testcase_13 AC 731 ms
118,628 KB
testcase_14 AC 708 ms
118,584 KB
testcase_15 AC 685 ms
118,668 KB
testcase_16 AC 657 ms
118,004 KB
testcase_17 AC 685 ms
117,836 KB
testcase_18 AC 638 ms
108,616 KB
testcase_19 AC 679 ms
117,592 KB
testcase_20 AC 669 ms
118,548 KB
testcase_21 AC 662 ms
117,724 KB
testcase_22 AC 55 ms
50,444 KB
testcase_23 AC 643 ms
118,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] nums = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            nums[i] = i;
        }
        for (int i = 2; i * i <= n; i++) {
            for (int j = 1; j * i * i <= n; j++) {
                while (nums[j * i * i] % (i * i) == 0) {
                    nums[j * i * i] /= i * i;
                }
            }
        }
        HashMap<Integer, ArrayDeque<Integer>> alls = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            if (!alls.containsKey(nums[i])) {
                alls.put(nums[i], new ArrayDeque<>());
            }
            alls.get(nums[i]).add(i);
        }
        int[] ans = new int[n];
        for (int i = n; i >= 1; i--)  {
            ans[i - 1] = alls.get(nums[i]).poll();
        }
        System.out.println(String.join(" ", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
    }
}  
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0