結果
問題 | No.1711 Divide LCM |
ユーザー |
![]() |
提出日時 | 2021-09-12 16:14:02 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 2,897 ms / 4,000 ms |
コード長 | 4,435 bytes |
コンパイル時間 | 2,063 ms |
コンパイル使用メモリ | 87,404 KB |
実行使用メモリ | 121,344 KB |
最終ジャッジ日時 | 2024-09-17 17:09:49 |
合計ジャッジ時間 | 24,178 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
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++];elsereturn -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());}}