結果

問題 No.1711 Divide LCM
ユーザー chocoruskchocorusk
提出日時 2021-09-12 16:14:02
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,435 bytes
コンパイル時間 4,693 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 137,208 KB
最終ジャッジ日時 2023-10-17 19:57:42
合計ジャッジ時間 31,039 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
62,356 KB
testcase_01 AC 110 ms
62,696 KB
testcase_02 AC 83 ms
61,888 KB
testcase_03 AC 124 ms
62,680 KB
testcase_04 AC 209 ms
64,372 KB
testcase_05 AC 321 ms
67,208 KB
testcase_06 AC 209 ms
66,244 KB
testcase_07 AC 271 ms
66,716 KB
testcase_08 AC 210 ms
63,992 KB
testcase_09 AC 409 ms
71,704 KB
testcase_10 AC 222 ms
66,384 KB
testcase_11 AC 553 ms
73,348 KB
testcase_12 AC 218 ms
66,420 KB
testcase_13 AC 140 ms
63,108 KB
testcase_14 AC 181 ms
63,572 KB
testcase_15 AC 209 ms
63,764 KB
testcase_16 AC 121 ms
62,372 KB
testcase_17 AC 185 ms
63,644 KB
testcase_18 AC 429 ms
79,064 KB
testcase_19 AC 430 ms
78,604 KB
testcase_20 AC 432 ms
79,332 KB
testcase_21 AC 588 ms
84,872 KB
testcase_22 TLE -
testcase_23 AC 3,156 ms
137,208 KB
testcase_24 AC 665 ms
84,992 KB
testcase_25 AC 642 ms
84,984 KB
testcase_26 AC 613 ms
82,052 KB
testcase_27 AC 601 ms
81,992 KB
testcase_28 AC 589 ms
80,712 KB
testcase_29 AC 173 ms
63,476 KB
testcase_30 AC 716 ms
86,396 KB
testcase_31 AC 156 ms
63,292 KB
testcase_32 AC 627 ms
86,400 KB
testcase_33 AC 607 ms
87,304 KB
testcase_34 AC 205 ms
63,952 KB
testcase_35 AC 754 ms
82,812 KB
testcase_36 AC 792 ms
83,976 KB
testcase_37 AC 772 ms
84,648 KB
testcase_38 AC 517 ms
86,624 KB
testcase_39 AC 641 ms
86,788 KB
testcase_40 AC 85 ms
62,072 KB
testcase_41 AC 90 ms
61,892 KB
testcase_42 AC 94 ms
61,876 KB
testcase_43 AC 86 ms
62,080 KB
testcase_44 AC 97 ms
63,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;

public class Main {
	public static void main(String[] args) {
		//Scanner scanner = new Scanner(System.in);
		FastScanner scanner=new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		int n=scanner.nextInt();
		int[][] fp=new int[n][];
		int[][] fe=new int[n][];
		int[] a=new int[n];
		int[] mx=new int[1000010];
		int[] q=new int[1000010];
		List<Integer> ql=new ArrayList<>();
		for(int i=0; i<n; i++) {
			int m=scanner.nextInt();
			fp[i]=new int[m];
			fe[i]=new int[m];
			a[i]=1;
			for(int j=0; j<m; j++) {
			    int p=scanner.nextInt();
			    int e=scanner.nextInt();
				fp[i][j]=p;
				fe[i][j]=e;
				if(mx[p]<e) {
					mx[p]=e;
				}
				for(int k=0; k<e; k++) {
					a[i]*=p;
				}
			}
		}
		for(int i=2; i<1000000; i++) {
			if(mx[i]==0)continue;
			q[i]=1;
			for(int j=0; j<mx[i]; j++) {
				q[i]*=i;
			}
			ql.add(q[i]);
		}
		for(int i=0; i<n; i++) {
		    int m=fe[i].length;
			if(m<ql.size())continue;
			boolean ok=true;
			for(int j=0; j<m; j++) {
				int p=fp[i][j], e=fe[i][j];
				if(mx[p]!=e) {
					ok=false;
					break;
				}
			}
			if(ok) {
				out.println(-1);
				out.close();
				return;
			}
		}
		Set<Integer> st=new TreeSet<>();
		for(int i=0; i<n; i++) {
			int m=fp[i].length;
			for(int j=0; j<(1<<m); j++) {
				int b=1, c=0;
				for(int k=0; k<m; k++) {
				    if((j&(1<<k))!=0){
    					int p=fp[i][k];
    					if(mx[p]!=fe[i][k]) {
    						c=0;
    						break;
    					}
					    b*=q[p];
					    c++;
				    }
				}
				if(c>1) st.add(b);
			}
		}
		Queue<Integer> que=new ArrayDeque<Integer>();
		Queue<Integer> quei=new ArrayDeque<Integer>();
		for(int i=0; i<ql.size(); i++) {
			que.add(ql.get(i));
			quei.add(i);
		}
		while(!que.isEmpty()) {
			int x=que.poll();
			int i=quei.poll();
			for(int j=i+1; j<ql.size(); j++) {
				long x1=(long)x*ql.get(j);
				if(x1>1000000000 || !st.contains((int)x1)) {
					int c=0;
					for(int v:ql) {
						if(x1%v==0)c++;
					}
					out.println(c);
					boolean[] used=new boolean[n];
					for(int v:ql) {
						if(x1%v!=0)continue;
						List<Integer> w=new ArrayList<>();
						for(int k=0; k<n; k++) {
							if(used[k]) continue;
							if(a[k]%v!=0) {
								w.add(k);
								used[k]=true;
							}
						}
						out.print(w.size());
						for(int k:w) {
							out.print(" "+a[k]);
						}
						out.println();
					}
					out.close();
					return;
				}
				que.add((int)x1);
				quei.add(j);
			}
		}
	}
}
class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}
0