結果
問題 | No.310 2文字しりとり |
ユーザー | uwi |
提出日時 | 2015-12-08 05:23:14 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 2,543 ms / 6,000 ms |
コード長 | 11,619 bytes |
コンパイル時間 | 4,385 ms |
コンパイル使用メモリ | 89,252 KB |
実行使用メモリ | 150,588 KB |
最終ジャッジ日時 | 2024-09-14 19:17:01 |
合計ジャッジ時間 | 20,273 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,856 KB |
testcase_01 | AC | 52 ms
36,976 KB |
testcase_02 | AC | 52 ms
36,852 KB |
testcase_03 | AC | 53 ms
36,884 KB |
testcase_04 | AC | 54 ms
36,824 KB |
testcase_05 | AC | 52 ms
36,672 KB |
testcase_06 | AC | 52 ms
36,904 KB |
testcase_07 | AC | 53 ms
36,536 KB |
testcase_08 | AC | 53 ms
36,536 KB |
testcase_09 | AC | 54 ms
36,904 KB |
testcase_10 | AC | 56 ms
36,884 KB |
testcase_11 | AC | 53 ms
36,756 KB |
testcase_12 | AC | 52 ms
36,644 KB |
testcase_13 | AC | 53 ms
36,764 KB |
testcase_14 | AC | 52 ms
37,092 KB |
testcase_15 | AC | 51 ms
36,744 KB |
testcase_16 | AC | 55 ms
37,012 KB |
testcase_17 | AC | 56 ms
37,220 KB |
testcase_18 | AC | 73 ms
38,320 KB |
testcase_19 | AC | 90 ms
38,292 KB |
testcase_20 | AC | 85 ms
38,444 KB |
testcase_21 | AC | 2,281 ms
150,588 KB |
testcase_22 | AC | 2,543 ms
150,352 KB |
testcase_23 | AC | 2,541 ms
150,256 KB |
testcase_24 | AC | 2,537 ms
150,396 KB |
testcase_25 | AC | 2,313 ms
150,344 KB |
testcase_26 | AC | 659 ms
57,892 KB |
testcase_27 | AC | 824 ms
67,244 KB |
ソースコード
package q8xx; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Random; public class Q874_5F { InputStream is; PrintWriter out; String INPUT = ""; // String INPUT = "10 0"; // String INPUT = "4 7 1 2 2 1 2 3 3 2 2 4 4 2 2 2"; boolean[][] shrink(boolean[][] ng) { int n = ng.length; int[] outdeg = new int[n]; int[] indeg = new int[n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ outdeg[i]++; indeg[j]++; } } } int[] map = new int[n]; Arrays.fill(map, -1); int p = 0; for(int i = 0;i < n;i++){ if(!(outdeg[i] == 0 && indeg[i] == 0)){ map[i] = p++; } } boolean[][] nng = new boolean[p][p]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(ng[i][j] && map[i] != -1 && map[j] != -1){ nng[map[i]][map[j]] = true; } } } return nng; } long S,G; int mod = 1000000007; void solve() { int n = ni(), m = ni(); if(n*n == m){ out.println(1); return; } int on = n; boolean[][] ng = new boolean[n][n]; for(int i = 0;i < m;i++){ int f = ni()-1, t = ni()-1; ng[f][t] = true; } ng = shrink(ng); n = ng.length; int[] outdeg = new int[n]; int[] indeg = new int[n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ outdeg[i]++; indeg[j]++; } } } // connectivity DJSet ds = new DJSet(n); for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ ds.union(i, j); } } } for(int i = 0;i < n;i++){ for(int j = i+1;j < n;j++){ if( (outdeg[i] >= 1 || indeg[i] >= 1) && (outdeg[j] >= 1 || indeg[j] >= 1) ){ if(!ds.equiv(i, j)){ out.println(0); return; } } } } int src = -1, sink = -1; for(int i = 0;i < n;i++){ if(outdeg[i] == indeg[i])continue; if(outdeg[i] == indeg[i]+1 && src == -1){ src = i; continue; } if(outdeg[i] == indeg[i]-1 && sink == -1){ sink = i; continue; } out.println(0); return; } int[][] MM = new int[n+1][n+1]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(i == j){ MM[i+1][i+1]++; }else if(!ng[i][j]){ MM[i+1][j+1] = 0; MM[i+1][i+1]++; }else{ MM[i+1][j+1] = 1; } } } MM[0][0] = 1; for(int i = 1;i < n+1;i++)MM[0][i] = mod-1; for(int i = 1;i < n+1;i++)MM[i][0] = mod-1; if(src == -1){ out.println(numEulerianCircuit(MM, outdeg, indeg) * (on*on-m) % mod); }else{ // int u = MM[sink+1][src+1] == 0 ? 2 : 1; if(--MM[sink+1][src+1] < 0)MM[sink+1][src+1] += mod; MM[sink+1][sink+1]++; outdeg[sink]++; indeg[src]++; long nec = numEulerianCircuit(MM, outdeg, indeg); out.println(nec); } } long numEulerianCircuit(int[][] M, int[] outdeg, int[] indeg) { int n = M.length; long[] F = new long[n+2]; F[0] = 1; for(int i = 1;i <= n;i++)F[i] = F[i-1] * i % mod; long ec = 1; for(int i = 0;i < outdeg.length;i++){ ec = ec * F[outdeg[i]-1] % mod; } long[][][] QQ = denseToSparse(M, n-1); Random gen = new Random(0); long det = det(QQ, mod, gen); return ec * det % mod; } public long det(long[][][] A, int mod, Random gen) { int n = A.length; long div = 1L; long[] diag = new long[n]; for(int i = 0;i < n;i++){ diag[i] = gen.nextInt(mod-1)+1; div = div * diag[i] % mod; } for(int i = 0;i < n;i++){ for(long[] e : A[i]){ e[1] = e[1] * diag[(int)e[0]] % mod; } } long[] u = new long[n]; for(int i = 0;i < n;i++)u[i] = gen.nextInt(mod); long[] v = new long[n]; for(int i = 0;i < n;i++)v[i] = gen.nextInt(mod); long[] f = minPolynomialSparse(A, v, u, mod); if(n % 2 == 1)f[0] = mod-f[0]; return f[0] * invl(div, mod) % mod; } public static long[][][] denseToSparse(int[][] A, int n) { long[][][] ret = new long[n][][]; for(int i = 0;i < n;i++){ int nz = 0; for(int j = 0;j < n;j++)if(A[i][j] != 0)nz++; ret[i] = new long[nz][]; nz = 0; for(int j = 0;j < n;j++){ if(A[i][j] != 0){ ret[i][nz++] = new long[]{j, A[i][j]}; } } } return ret; } // public static long[] minPolynomial(long[][] M, long[] v, long[] u, int mod) // { // int n = M.length; // long[] co = new long[2*n]; // long[] w = Arrays.copyOf(v, n); // long big = 8L*mod*mod; // for(int i = 0;i < 2*n;i++){ // co[i] = 0L; // for(int j = 0;j < n;j++){ // co[i] += w[j] * u[j]; // if(co[i] >= big)co[i] -= big; // } // co[i] %= mod; // w = mul(M, w, mod); // } // // return modifiedBerlekampMassey(co, mod); // } public static long[] minPolynomialSparse(long[][][] M, long[] v, long[] u, int mod) { int n = M.length; // U -= System.nanoTime(); long[] co = new long[2*n]; long[] w = Arrays.copyOf(v, n); long[] x = new long[n]; long big = 8L*mod*mod; for(int i = 0;i < 2*n;i++){ co[i] = 0L; for(int j = 0;j < n;j++){ co[i] += w[j] * u[j]; if(co[i] >= big)co[i] -= big; } co[i] %= mod; x = mulSparse(M, w, mod, x); long[] dum = w; w = x; x = dum; } // U += System.nanoTime(); // tr("U", U); // long S = System.nanoTime(); long[] res = modifiedBerlekampMassey(co, mod); // 0.53/1.18 // long G = System.nanoTime(); // tr(G-S); return res; } public static long[] minPolynomialSparse(long[][][] M, long[] v, long[] u, int mod, int step) { int n = M.length; long[] co = new long[step]; long[] w = Arrays.copyOf(v, n); long[] x = new long[n]; long big = 8L*mod*mod; for(int i = 0;i < step;i++){ co[i] = 0L; for(int j = 0;j < n;j++){ co[i] += w[j] * u[j]; if(co[i] >= big)co[i] -= big; } co[i] %= mod; w = mulSparse(M, w, mod, x); long[] dum = w; w = x; x = dum; } return modifiedBerlekampMassey(co, mod); } static long[] mul(long[][] M, long[] v, int mod) { int n = v.length; long[] w = new long[n]; for(int i = 0;i < n;i++){ long sum = 0; for(int j = 0;j < n;j++){ sum += M[i][j] * v[j]; sum %= mod; } w[i] = sum; } return w; } static long[] mulSparse(long[][][] M, long[] v, int mod, long[] w) { int n = v.length; long big = 8L*mod*mod; for(int i = 0;i < n;i++){ long sum = 0; for(long[] e : M[i]){ sum += e[1] * v[(int)e[0]]; if(sum >= big)sum -= big; } w[i] = sum % mod; } return w; } static int ct = 0; public static long[] modifiedBerlekampMassey(long[] a, int mod) { assert a.length % 2 == 0; int n = a.length/2; int m = 2*n-1; long[] R0 = new long[2*n+1]; R0[2*n] = 1; long[] R1 = new long[2*n]; for(int i = 0;i < 2*n;i++)R1[i] = a[m-i]; R1 = strip(R1); long[] v0 = new long[n+1]; long[] v1 = new long[n+1]; v1[0] = 1; int dr0 = 2*n, dr1 = R1.length-1; while(n < dr0){ long irhead = invl(R1[dr1], mod); for(int i = dr0;i >= dr1;i--){ long mul = (mod-R0[i]) * irhead % mod; for(int j = 0;j < dr1;j++){ R0[j+i-dr1] += R1[j] * mul; R0[j+i-dr1] %= mod; } R0[i] = 0; for(int j = 0;j < v1.length;j++){ if(v1[j] == 0)continue; v0[j+i-dr1] += mul*v1[j]; v0[j+i-dr1] %= mod; } } {long[] dum = v0; v0 = v1; v1 = dum;} {long[] dum = R0; R0 = R1; R1 = dum;} dr0 = dr1; while(dr1 > 0 && R1[dr1] == 0)dr1--; } // normalize long z = invl(v1[v1.length-1], mod); for(int i = 0;i < v1.length;i++){ v1[i] = v1[i] * z % mod; } return v1; } public static long[] strip(long[] a) { int i; for(i = a.length-1;i > 0 && a[i] == 0;i--); if(i + 1 == a.length)return a; return Arrays.copyOf(a, i+1); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } public int count() { int ct = 0; for (int u : upper) if (u < 0) ct++; return ct; } } public static long invl(long a, long mod) { if(a == 0)throw new RuntimeException(); long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } void run() throws Exception { // int n = 4000, m = 99999; // Random gen = new Random(0); // StringBuilder sb = new StringBuilder(); // sb.append(n+ " "); // sb.append(1 + " "); // for(int i = 0;i < 1;i++){ // sb.append(gen.nextInt(n)+1 + " "); // sb.append(gen.nextInt(n)+1 + " "); // } // INPUT = sb.toString(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new Q874_5F().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }