結果
問題 | No.310 2文字しりとり |
ユーザー | uwi |
提出日時 | 2015-12-08 05:27:37 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 11,788 bytes |
コンパイル時間 | 4,762 ms |
コンパイル使用メモリ | 90,432 KB |
実行使用メモリ | 181,008 KB |
最終ジャッジ日時 | 2024-09-14 19:17:28 |
合計ジャッジ時間 | 18,946 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
50,248 KB |
testcase_01 | AC | 57 ms
50,044 KB |
testcase_02 | AC | 57 ms
50,432 KB |
testcase_03 | WA | - |
testcase_04 | AC | 57 ms
50,020 KB |
testcase_05 | AC | 57 ms
50,204 KB |
testcase_06 | AC | 57 ms
50,280 KB |
testcase_07 | AC | 56 ms
50,232 KB |
testcase_08 | AC | 57 ms
50,072 KB |
testcase_09 | AC | 57 ms
50,336 KB |
testcase_10 | AC | 56 ms
50,308 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 1,996 ms
178,792 KB |
testcase_22 | AC | 2,211 ms
178,912 KB |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
ソースコード
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_6 { 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] = (int)invl(n, mod); for(int i = 1;i < n+1;i++)MM[0][i] = 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[] 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 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 = mulSparse(M, w, mod); } // 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 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); } 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) { int n = v.length; long[] w = new long[n]; 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 = {0L}; long[] v1 = {1L}; while(n <= R1.length-1){ long[][] QR = divmod(R0, R1, mod); long[] v = sub(v0, mul(QR[0], v1, mod), mod); v0 = v1; v1 = v; R0 = R1; R1 = QR[1]; } // normalize long z = invl(v1[v1.length-1], mod); for(int i = 0;i < v1.length;i++){ v1[i] = v1[i] * z % mod; } return strip(v1); } static long U = 0; public static long[] mul(long[] a, long[] b, int mod) { long[] c = new long[a.length+b.length-1]; long big = 8L*mod*mod; for(int i = 0;i < a.length;i++){ for(int j = 0;j < b.length;j++){ c[i+j] += a[i] * b[j]; if(c[i+j] >= big)c[i+j] -= big; } } for(int i = 0;i < c.length;i++)c[i] %= mod; return strip(c); } public static long[] sub(long[] a, long[] b, int mod) { long[] c = Arrays.copyOf(a, Math.max(a.length, b.length)); for(int i = 0;i < c.length;i++){ c[i] -= b[i]; if(c[i] < 0)c[i] += mod; } return strip(c); } 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 long[][] divmod(long[] A, long[] B, int mod) { assert B[B.length-1] != 0; long[] R = A; long[] Q = new long[Math.max(1, A.length-B.length+1)]; long ib = invl(B[B.length-1], mod); long big = 8L*mod*mod; for(int i = A.length-1, t = Q.length-1;i >= B.length-1;i--,t--){ R[i] %= mod; if(R[i] < 0)R[i] += mod; long m = R[i] * ib % mod; Q[t] = m; for(int j = 0, k = i-B.length+1+j;j < B.length-1;j++,k++){ R[k] -= B[j] * m; if(R[k] < -big)R[k] += big; } R[i] = 0; } for(int i = B.length-2;i >= 0;i--){ R[i] %= mod; if(R[i] < 0)R[i] += mod; } return new long[][]{Q, strip(R)}; } 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) { 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_6().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)); } }