結果

問題 No.854 公平なりんご分配
ユーザー htensai
提出日時 2020-01-28 08:56:07
言語 Java
(openjdk 23)
結果
RE  
実行時間 -
コード長 2,636 bytes
コンパイル時間 2,293 ms
コンパイル使用メモリ 80,248 KB
実行使用メモリ 555,704 KB
最終ジャッジ日時 2024-09-15 05:24:38
合計ジャッジ時間 59,811 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 8 RE * 41 TLE * 10 MLE * 12 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer>[] maps = new HashMap[n + 1];
        maps[0] = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            maps[i] =(HashMap<Integer, Integer>)(maps[i - 1].clone());
            int x = sc.nextInt();
            for (int j = 2; j <= Math.sqrt(x); j++) {
                while (x % j == 0) {
                    if (maps[i].containsKey(j)) {
                        maps[i].put(j, maps[i].get(j) + 1);
                    } else {
                        maps[i].put(j, 1);
                    }
                    x /= j;
                }
            }
            if (x > 1) {
                if (maps[i].containsKey(x)) {
                    maps[i].put(x, maps[i].get(x) + 1);
                } else {
                    maps[i].put(x, 1);
                }
            }
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int p = sc.nextInt();
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            HashMap<Integer, Integer> target = (HashMap<Integer, Integer>)(maps[right].clone());
            for (Map.Entry<Integer, Integer> entry : maps[left - 1].entrySet()) {
                target.put(entry.getKey(), target.get(entry.getKey()) - entry.getValue());
            }
            boolean flag = true;
            for (int j = 2; j <= Math.sqrt(p) && flag; j++) {
                while (p % j == 0 && flag) {
                    if (target.containsKey(j)) {
                        int x = target.get(j);
                        x--;
                        if (x < 0) {
                            flag = false;
                        } else {
                            target.put(j, x);
                        }
                    } else {
                        flag = false;
                    }
                    p /= j;
                }
            }
            if (flag && p > 1) {
                if (target.containsKey(p)) {
                    int x = target.get(p);
                    x--;
                    if (x < 0) {
                        flag = false;
                    }
                } else {
                    flag = false;
                }
            }
            if (flag) {
                sb.append("Yes");
            } else {
                sb.append("NO");
            }
            sb.append("\n");
        }
       System.out.print(sb);
   }
}
0