|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/** |
| 5 | + * Facebook Hacker Cup 2017 Round 3 |
| 6 | + * Problem B. Sluggish Security |
| 7 | + */ |
| 8 | +public class Main { |
| 9 | + |
| 10 | + private static final long MOD = 1000000007L; |
| 11 | + |
| 12 | + private static long[] x, y; |
| 13 | + |
| 14 | + public String solve(Scanner scanner) throws Exception { |
| 15 | + calMod(); |
| 16 | + int n=scanner.nextInt(); |
| 17 | + long[] a=input(scanner, n), b=input(scanner, n); |
| 18 | + |
| 19 | + int ai=0, bi=0; |
| 20 | + long ans=1; |
| 21 | + |
| 22 | + while (ai<n || bi<n) { |
| 23 | + |
| 24 | + ////// CASE 1: X,X |
| 25 | + if (ai<n && bi<n && a[ai]==b[bi]) { |
| 26 | + ai++; bi++; |
| 27 | + } |
| 28 | + |
| 29 | + ////// CASE 2: XY,YX |
| 30 | + else if (ai+1<n && bi+1<n && a[ai]==b[bi+1] && a[ai+1]==b[bi]) { |
| 31 | + ans*=2; ans%=MOD; |
| 32 | + ai+=2; bi+=2; |
| 33 | + } |
| 34 | + |
| 35 | + ////// CASE 3: XYX,Y |
| 36 | + else if (ai+2<n && bi<n && a[ai]==a[ai+2] && a[ai+1]==b[bi]) { |
| 37 | + ans*=2; ans%=MOD; |
| 38 | + ai+=3; bi++; |
| 39 | + } |
| 40 | + |
| 41 | + ////// CASE 4: X,YXY |
| 42 | + else if (bi+2<n && ai<n && b[bi]==b[bi+2] && b[bi+1]==a[ai]) { |
| 43 | + ans*=2; ans%=MOD; |
| 44 | + ai++; bi+=3; |
| 45 | + } |
| 46 | + |
| 47 | + ////// CASE 5: XX|XYXY|... |
| 48 | + else { |
| 49 | + int al = 0, bl = 0; |
| 50 | + while (true) { |
| 51 | + if (ai+1<n && a[ai]==a[ai+1]) { |
| 52 | + ai+=2; |
| 53 | + al++; |
| 54 | + } |
| 55 | + else if (ai+3<n && a[ai]==a[ai+2] && a[ai+1]==a[ai+3]) { |
| 56 | + ai+=4; |
| 57 | + al++; |
| 58 | + } |
| 59 | + else break; |
| 60 | + } |
| 61 | + while (true) { |
| 62 | + if (bi+1<n && b[bi]==b[bi+1]) { |
| 63 | + bi+=2; |
| 64 | + bl++; |
| 65 | + } |
| 66 | + else if (bi+3<n && b[bi]==b[bi+2] && b[bi+1]==b[bi+3]) { |
| 67 | + bi+=4; |
| 68 | + bl++; |
| 69 | + } |
| 70 | + else break; |
| 71 | + } |
| 72 | + |
| 73 | + // Not IMPOSSIBLE |
| 74 | + if (al==0 && bl==0) return "0"; |
| 75 | + |
| 76 | + ans=ans*x[al+bl]%MOD*y[al]%MOD*y[bl]%MOD; |
| 77 | + } |
| 78 | + } |
| 79 | + return String.valueOf(ans); |
| 80 | + } |
| 81 | + |
| 82 | + private long[] input(Scanner scanner, int n) { |
| 83 | + long[] a=new long[n], da=new long[n]; |
| 84 | + a[0]=scanner.nextLong(); |
| 85 | + int k=scanner.nextInt(), index=0; |
| 86 | + for (int i=0;i<k;i++) { |
| 87 | + int r=scanner.nextInt(), l=scanner.nextInt(); |
| 88 | + ArrayList<Long> list=new ArrayList<>(); |
| 89 | + for (int j=0;j<l;j++) list.add(scanner.nextLong()); |
| 90 | + for (int j=0;j<r;j++) { |
| 91 | + for (long u: list) { |
| 92 | + a[index+1]=a[index]+u; |
| 93 | + index++; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return a; |
| 98 | + } |
| 99 | + |
| 100 | + private static void calMod() { |
| 101 | + if (x!=null) return; |
| 102 | + x=new long[5000000]; y=new long[5000000]; |
| 103 | + |
| 104 | + // x |
| 105 | + x[0]=1; |
| 106 | + y[0]=1; |
| 107 | + for (int i=1;i<5000000;i++) { |
| 108 | + x[i]=x[i-1]*i%MOD; |
| 109 | + y[i]=quickPow(x[i], MOD-2); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // x^n%p |
| 114 | + private static long quickPow(long x, long n) { |
| 115 | + if (n==0) return 1; |
| 116 | + if (n==1) return x; |
| 117 | + long v=quickPow(x, n/2); |
| 118 | + return v*v%MOD*(n%2!=0?x:1)%MOD; |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) throws Exception { |
| 122 | + System.setIn(new FileInputStream("input.txt")); |
| 123 | + System.setOut(new PrintStream("output.txt")); |
| 124 | + Scanner scanner=new Scanner(System.in); |
| 125 | + int times=Integer.parseInt(scanner.nextLine()); |
| 126 | + long start=System.currentTimeMillis(); |
| 127 | + for (int t=1;t<=times;t++) { |
| 128 | + try { |
| 129 | + System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); |
| 130 | + } |
| 131 | + catch (Throwable e) { |
| 132 | + System.err.println("ERROR in case #"+t); |
| 133 | + e.printStackTrace(); |
| 134 | + } |
| 135 | + } |
| 136 | + long end=System.currentTimeMillis(); |
| 137 | + System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); |
| 138 | + |
| 139 | + } |
| 140 | +} |
0 commit comments