結果
問題 | No.990 N×Mマス計算(Kの倍数) |
ユーザー | tenten |
提出日時 | 2023-02-01 09:19:01 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 621 ms / 2,000 ms |
コード長 | 3,284 bytes |
コンパイル時間 | 2,598 ms |
コンパイル使用メモリ | 95,312 KB |
実行使用メモリ | 70,828 KB |
最終ジャッジ日時 | 2024-07-01 06:41:39 |
合計ジャッジ時間 | 8,717 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
50,192 KB |
testcase_01 | AC | 54 ms
50,164 KB |
testcase_02 | AC | 56 ms
50,428 KB |
testcase_03 | AC | 58 ms
50,448 KB |
testcase_04 | AC | 56 ms
50,508 KB |
testcase_05 | AC | 56 ms
50,332 KB |
testcase_06 | AC | 55 ms
50,380 KB |
testcase_07 | AC | 55 ms
50,176 KB |
testcase_08 | AC | 55 ms
50,180 KB |
testcase_09 | AC | 55 ms
50,112 KB |
testcase_10 | AC | 318 ms
59,024 KB |
testcase_11 | AC | 323 ms
56,648 KB |
testcase_12 | AC | 621 ms
59,032 KB |
testcase_13 | AC | 257 ms
56,888 KB |
testcase_14 | AC | 335 ms
57,296 KB |
testcase_15 | AC | 273 ms
56,772 KB |
testcase_16 | AC | 354 ms
58,856 KB |
testcase_17 | AC | 246 ms
56,304 KB |
testcase_18 | AC | 601 ms
59,368 KB |
testcase_19 | AC | 365 ms
56,480 KB |
testcase_20 | AC | 587 ms
70,828 KB |
ソースコード
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 m = sc.nextInt(); int k = sc.nextInt(); boolean isPlus = (sc.next().charAt(0) == '+'); long ans = 0; if (isPlus) { HashMap<Integer, Long> countsA = new HashMap<>(); for (int i = 0; i < m; i++) { int x = sc.nextInt() % k; countsA.put(x, countsA.getOrDefault(x, 0L) + 1); } HashMap<Integer, Long> countsB = new HashMap<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt() % k; countsB.put(x, countsB.getOrDefault(x, 0L) + 1); } for (int x : countsA.keySet()) { if (x == 0) { ans += countsA.get(x) * countsB.getOrDefault(x, 0L); } else { ans += countsA.get(x) * countsB.getOrDefault(k - x, 0L); } } } else { HashMap<Long, Long> countsA = new HashMap<>(); for (int i = 0; i < m; i++) { long gcd = getGCD(sc.nextInt(), k); countsA.put(gcd, countsA.getOrDefault(gcd, 0L) + 1); } HashMap<Long, Long> countsB = new HashMap<>(); for (int i = 0; i < n; i++) { long gcd = getGCD(sc.nextInt(), k); countsB.put(gcd, countsB.getOrDefault(gcd, 0L) + 1); } for (long x : countsA.keySet()) { for (long y : countsB.keySet()) { if (x * y % k == 0) { ans += countsA.get(x) * countsB.get(y); } } } } System.out.println(ans); } static long getGCD(long x, long y) { if (y == 0) { return x; } else { return getGCD(y, x % y); } } } 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(); } }