結果

問題 No.2519 Coins in Array
ユーザー ks2mks2m
提出日時 2023-10-27 23:23:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 2,810 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 84,580 KB
実行使用メモリ 74,680 KB
最終ジャッジ日時 2023-10-27 23:23:43
合計ジャッジ時間 11,308 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,252 KB
testcase_01 AC 47 ms
53,252 KB
testcase_02 AC 46 ms
53,348 KB
testcase_03 AC 79 ms
56,032 KB
testcase_04 AC 45 ms
53,260 KB
testcase_05 AC 74 ms
54,448 KB
testcase_06 AC 46 ms
53,252 KB
testcase_07 AC 72 ms
54,468 KB
testcase_08 AC 48 ms
52,360 KB
testcase_09 AC 74 ms
54,476 KB
testcase_10 AC 74 ms
54,468 KB
testcase_11 AC 48 ms
53,252 KB
testcase_12 AC 46 ms
53,260 KB
testcase_13 AC 46 ms
51,220 KB
testcase_14 AC 45 ms
53,264 KB
testcase_15 AC 54 ms
53,360 KB
testcase_16 AC 51 ms
53,256 KB
testcase_17 AC 46 ms
53,252 KB
testcase_18 AC 45 ms
53,252 KB
testcase_19 AC 47 ms
53,356 KB
testcase_20 AC 46 ms
53,360 KB
testcase_21 AC 45 ms
53,352 KB
testcase_22 AC 48 ms
53,256 KB
testcase_23 AC 361 ms
74,680 KB
testcase_24 AC 418 ms
72,996 KB
testcase_25 AC 47 ms
52,692 KB
testcase_26 AC 46 ms
53,360 KB
testcase_27 AC 398 ms
74,452 KB
testcase_28 AC 386 ms
73,512 KB
testcase_29 AC 395 ms
74,508 KB
testcase_30 AC 378 ms
68,056 KB
testcase_31 AC 158 ms
57,996 KB
testcase_32 AC 252 ms
63,668 KB
testcase_33 AC 277 ms
66,040 KB
testcase_34 AC 219 ms
61,308 KB
testcase_35 AC 281 ms
65,580 KB
testcase_36 AC 202 ms
61,368 KB
testcase_37 AC 146 ms
57,744 KB
testcase_38 AC 255 ms
63,688 KB
testcase_39 AC 228 ms
60,944 KB
testcase_40 AC 326 ms
68,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		if (n >= 4) {
			PrintWriter pw = new PrintWriter(System.out);
			pw.println(0);
			for (int i = 0; i < n - 1; i++) {
				pw.println("1 2");
			}
			pw.flush();

		} else if (n == 2) {
			Map<Long, Integer> map0 = bunkai(a[0]);
			Map<Long, Integer> map1 = bunkai(a[1]);
			long m = (a[0] - 1) * (a[1] - 1);
			for (Long k : map0.keySet()) {
				if (map1.containsKey(k)) {
					m = 0;
					break;
				}
			}
			if (a[0] == 0 || a[1] == 0) {
				m = 0;
			}
			System.out.println(m);
			System.out.println("1 2");

		} else {
			Map<Long, Integer> map0 = bunkai(a[0]);
			Map<Long, Integer> map1 = bunkai(a[1]);
			Map<Long, Integer> map2 = bunkai(a[2]);
			Map<Long, Integer> map01 = bunkai((a[0] - 1) * (a[1] - 1));
			Map<Long, Integer> map12 = bunkai((a[1] - 1) * (a[2] - 1));
			Map<Long, Integer> map20 = bunkai((a[2] - 1) * (a[0] - 1));
			for (Long k : map0.keySet()) {
				if (map12.containsKey(k)) {
					System.out.println(0);
					System.out.println("2 3");
					System.out.println("1 2");
					return;
				}
			}
			for (Long k : map1.keySet()) {
				if (map20.containsKey(k)) {
					System.out.println(0);
					System.out.println("1 3");
					System.out.println("1 2");
					return;
				}
			}
			for (Long k : map2.keySet()) {
				if (map01.containsKey(k)) {
					System.out.println(0);
					System.out.println("1 2");
					System.out.println("1 2");
					return;
				}
			}

			PriorityQueue<Obj> que = new PriorityQueue<>((o1, o2) -> o1.a - o2.a);
			for (int i = 0; i < n; i++) {
				Obj o = new Obj();
				o.i = i;
				o.a = (int) a[i];
				que.add(o);
			}
			Obj f = que.poll();
			long m = f.a;
			Obj o1 = que.poll();
			Obj o2 = que.poll();
			if (m != 0) {
				m = (m - 1) * (o1.a - 1);
			}
			if (m != 0) {
				m = (m - 1) * (o2.a - 1);
			}
			System.out.println(m);
			System.out.println((f.i + 1) + " " + (o1.i + 1));
			System.out.println("1 2");
		}
	}

	static class Obj {
		int i, a;
	}

	static Map<Long, Integer> bunkai(long n) {
		Map<Long, Integer> soinsu = new HashMap<>();
		int end = (int) Math.sqrt(n);
		long d = 2;
		while (n > 1) {
			if (n % d == 0) {
				n /= d;
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				end = (int) Math.sqrt(n);
			} else {
				if (d > end) {
					d = n - 1;
				}
				d++;
			}
		}
		return soinsu;
	}
}
0