Example #1
1
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int p = in.nextInt();
   int m = in.nextInt();
   Query[] qs = new Query[m];
   int[] z = new int[4 * m + 10];
   int cn = 0;
   for (int i = 0; i < m; i++) {
     int time = in.nextInt();
     int type = in.next().equals("save") ? 0 : 1;
     int a = in.nextInt();
     int b = in.nextInt();
     qs[i] = new Query(time, type, a, b);
     if (type == 0) {
       z[cn++] = a - 1;
       z[cn++] = b;
     } else {
       --a;
       z[cn++] = a - b + 1;
       z[cn++] = a;
       z[cn++] = a + b;
     }
   }
   z[cn++] = 0;
   z[cn++] = n;
   z = Arrays.copyOf(z, cn);
   z = ArrayUtils.sortAndUnique(z);
   int[] cnt = new int[z.length - 1];
   {
     for (int i = 0; i < z.length - 1; i++) {
       cnt[i] = z[i + 1] - z[i];
     }
   }
   SegmentTree tree = new SegmentTree(cnt);
   int last = 0;
   double all = 0;
   for (int i = 0; i < m; i++) {
     int time = qs[i].time;
     tree.addValue(0, cnt.length, 1. * p * (time - last), 0);
     last = time;
     if (qs[i].type == 0) {
       int l = qs[i].a - 1;
       int r = qs[i].b;
       l = Arrays.binarySearch(z, l);
       r = Arrays.binarySearch(z, r);
       double sum = tree.getSum(l, r);
       all += sum;
       out.println(all);
       tree.setZero(l, r);
     } else {
       int index = qs[i].a - 1;
       int d = qs[i].b;
       double x = all / (2 * SegmentTree.sum(d) + d);
       int id = Arrays.binarySearch(z, index);
       tree.addValue(Arrays.binarySearch(z, index - d + 1), id, x, x);
       tree.addValue(id, Arrays.binarySearch(z, index + d), d * x, -x);
       all = 0;
     }
   }
 }
Example #2
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   String s = in.next();
   f = new int[primes.length];
   add = new int[10][primes.length];
   for (int d = 2; d < 10; d++) {
     int[] f = add[d];
     for (int i = 0; i < primes.length; i++) {
       int p = primes[i];
       int x = d;
       while (x > 0) {
         x /= p;
         f[i] += x;
       }
     }
   }
   for (char c : s.toCharArray()) {
     int d = c - '0';
     if (d <= 1) continue;
     for (int i = 0; i < primes.length; i++) {
       f[i] += add[d][i];
     }
   }
   g = new int[primes.length];
   x = new int[100];
   if (go(0, 9)) {
     for (int i = 0; i < len; i++) {
       out.print(x[i]);
     }
     out.println();
   } else {
     throw new AssertionError();
   }
 }
Example #3
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   Point[] p1 = new Point[4];
   Point[] p2 = new Point[4];
   for (int i = 0; i < 4; i++) {
     p1[i] = new Point(in.nextInt(), in.nextInt());
     p2[i] = new Point(in.nextInt(), in.nextInt());
   }
   int[] first = new int[4];
   int[] second = new int[4];
   Arrays.fill(first, -1);
   Arrays.fill(second, -1);
   for (int i = 0; i < 4; i++) {
     for (int j = 0; j < 4; j++) {
       if (i == j) {
         continue;
       }
       if (p1[i].equals(p2[j])
           || p1[i].equals(p1[j])
           || p2[i].equals(p1[j])
           || p2[i].equals(p2[j])) {
         if (first[i] < 0) {
           first[i] = j;
         } else if (second[i] < 0) {
           second[i] = j;
         } else {
           out.println("NO");
           return;
         }
       }
     }
   }
   for (int i = 0; i < 4; i++) {
     if (second[i] < 0) {
       out.println("NO");
       return;
     }
   }
   int[] p = new int[4];
   p[1] = first[0];
   for (int i = 2; i < 4; i++) {
     p[i] = p[i - 2] ^ first[p[i - 1]] ^ second[p[i - 1]];
   }
   Point[] v = new Point[4];
   for (int i = 0; i < 4; i++) {
     v[i] = p2[p[i]].sub(p1[p[i]]);
   }
   for (int i = 0; i < 4; i++) {
     if (v[i].smul(v[(i + 1) & 3]) != 0
         || v[i].abs2() != v[i + 2 & 3].abs2()
         || v[i].abs2() == 0
         || (v[i].x != 0 && v[i].y != 0)) {
       out.println("NO");
       return;
     }
   }
   out.println("YES");
 }
Example #4
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int m = in.nextInt();
   int b = n % m;
   for (int i = 0; i < m - b; i++) {
     out.print(n / m + " ");
   }
   for (int i = 0; i < b; i++) {
     out.print(n / m + 1 + " ");
   }
 }
Example #5
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   for (int mask = 0; mask < 1 << 8; mask++) {
     boolean ok1 = (mask & 1) == 1;
     boolean ok2 = ((mask >> 7) & 1) == 0;
     boolean ok3 = false;
     for (int m2 = 0; m2 < 8; m2++) {
       if (((mask >> m2) & 1) == ((mask >> (m2 ^ 7)) & 1)) {
         ok3 = true;
         break;
       }
     }
     boolean ok4 = false;
     for (int m2 = 0; m2 < 8; m2++) {
       for (int m3 = 0; m3 < 8; m3++) {
         boolean greater = true;
         for (int j = 0; j < 3; j++) {
           if ((1 & (m2 >> j)) < (1 & (m3 >> j))) {
             greater = false;
             break;
           }
         }
         if (!greater) continue;
         if ((1 & (mask >> m2)) < (1 & (mask >> m3))) {
           ok4 = true;
         }
       }
     }
     boolean ok5 = true;
     for (int m2 = 0; m2 < 8; m2++) {
       boolean ok = true;
       for (int m3 = 0; m3 < 8; m3++) {
         int val = 0;
         for (int j = 0; j < 3; j++) {
           val ^= (1 & (m2 >> j)) & (1 & (m3 >> j));
         }
         if (val != ((mask >> m3) & 1)) ok = false;
       }
       if (ok) {
         ok5 = false;
       }
     }
     if (ok1 && ok2 && ok3 && ok4 && ok5) {
       for (int m = 0; m < 8; m++) {
         if (m > 0) out.print(' ');
         out.print((mask >> m) & 1);
       }
       out.println();
     }
   }
 }
Example #6
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   List<Integer> answer = new ArrayList<>();
   int last = -1;
   for (int i = n; i >= 0; i--) {
     List<Integer> d = get(n, last == -1 ? (1 << i) - 1 : (last & (last - 1)));
     answer.addAll(d);
     last = answer.get(answer.size() - 1);
   }
   for (int i : answer) {
     for (int j = 0; j < n; j++) {
       out.print((i >> j) & 1);
     }
     out.println();
   }
 }
Example #7
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int[] a = new int[n];
   int[] b = new int[n];
   int[] c = new int[n];
   int[] d = new int[n];
   for (int i = 0; i < n; i++) {
     a[i] = in.nextInt();
     b[i] = in.nextInt();
     c[i] = in.nextInt();
     d[i] = in.nextInt();
   }
   boolean[] bad = new boolean[n];
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
       if (a[i] > a[j] && b[i] > b[j] && c[i] > c[j]) {
         bad[j] = true;
       }
     }
   }
   int best = -1;
   for (int i = 0; i < n; i++) {
     if (bad[i]) {
       continue;
     }
     if (best < 0 || d[best] > d[i]) {
       best = i;
     }
   }
   out.println(best + 1);
 }
Example #8
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int k = in.nextInt();
   MOD = in.nextInt();
   int[][] dp = new int[2][k];
   int ten = 1;
   int ans = 0;
   dp[0][0] = 1;
   for (int i = 0; i < n; i++, ten = ten * 10 % k) {
     int[][] next = new int[2][k];
     for (int mod = 0; mod < k; mod++) {
       for (int d = 0; d < 10; d++) {
         int nMod = (mod + ten * d) % k;
         for (int has = 0; has < 2; has++) {
           int val = dp[has][mod];
           if (val == 0) continue;
           int nHas = has;
           if (nMod == 0 && d > 0) nHas = 1;
           next[nHas][nMod] = add(next[nHas][nMod], val);
           if (i + 1 == n && d > 0 && nHas == 1) ans = add(ans, val);
         }
       }
     }
     dp = next;
   }
   out.println(ans);
 }
Example #9
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   long n = in.nextLong();
   int c = in.nextInt();
   int m = in.nextInt();
   int[][] a = new int[c][c];
   for (int[] d : a) {
     Arrays.fill(d, 1);
   }
   for (int i = 0; i < m; i++) {
     int x = in.nextInt() - 1;
     int y = in.nextInt() - 1;
     a[x][y] = 0;
     a[y][x] = 0;
   }
   Matrix matrix = new Matrix(a);
   final int MOD = 1000000007;
   matrix = Matrix.powMod(matrix, n - 1, MOD);
   int[] ones = new int[c];
   Arrays.fill(ones, 1);
   Matrix x = new Matrix(new int[][] {ones});
   x = x.multiplyMod(matrix, MOD);
   long ans = 0;
   for (int i = 0; i < x.n; i++) {
     for (int j = 0; j < x.m; j++) {
       ans += x.get(i, j);
       ans %= MOD;
     }
   }
   out.println(ans);
 }
Example #10
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int length = in.nextInt();
   int[] a = new int[n];
   for (int i = 0; i < n; i++) {
     a[i] = in.nextInt();
   }
   Arrays.sort(a);
   double ans = 0;
   ans = Math.max(ans, a[0]);
   ans = Math.max(ans, length - a[n - 1]);
   for (int i = 1; i < n; i++) {
     ans = Math.max(ans, (a[i] - a[i - 1]) * .5);
   }
   out.println(ans);
 }
Example #11
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n1 = in.nextInt();
   int n2 = in.nextInt();
   int k1 = in.nextInt();
   int k2 = in.nextInt();
   boolean[][][] win = new boolean[2][n1 + 1][n2 + 1];
   for (int i = 0; i <= n1; i++) {
     for (int j = 0; j <= n2; j++) {
       for (int e = 1; e <= k1 && e <= i; e++) {
         win[0][i][j] |= !win[1][i - e][j];
       }
       for (int e = 1; e <= k2 && e <= j; e++) {
         win[1][i][j] |= !win[0][i][j - e];
       }
     }
   }
   out.println(win[0][n1][n2] ? "First" : "Second");
 }
Example #12
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int n = in.nextInt();
   int m = in.nextInt();
   int[] from = new int[m];
   int[] to = new int[m];
   for (int i = 0; i < m; i++) {
     from[i] = in.nextInt() - 1;
     to[i] = in.nextInt() - 1;
   }
   edges = GraphUtils.getEdgesDirectedUnweighted(n, from, to);
   g = new int[n];
   Arrays.fill(g, -1);
   for (int i = 0; i < n; i++) {
     if (g[i] < 0) {
       dfs(i);
     }
   }
   for (int i = 0; i < n; i++) {
     out.println(g[i]);
   }
 }
Example #13
0
 public void solve(int testNumber, FastScanner in, FastPrinter out) {
   int a1 = in.nextInt();
   int b1 = in.nextInt();
   int a2 = in.nextInt();
   int b2 = in.nextInt();
   if ((long) free(a1) * free(b1) != (long) free(a2) * free(b2)) {
     out.println(-1);
     return;
   }
   int two1 = two(a1) + two(b1);
   int three1 = three(a1) + three(b1);
   int two2 = two(a2) + two(b2);
   int three2 = three(a2) + three(b2);
   int best = Integer.MAX_VALUE;
   int ansTwo = -1;
   int ansThree = -1;
   for (int two = 0; two <= two1 || two <= two2; two++) {
     for (int three = 0; three <= three1 && three <= three2; three++) {
       int cost = three1 + three2 - three * 2;
       int c1 = two1 + three1 - three;
       int c2 = two2 + three2 - three;
       if (c1 < two || c2 < two) continue;
       cost += c1 - two;
       cost += c2 - two;
       if (cost < best) {
         best = cost;
         ansTwo = two;
         ansThree = three;
       }
     }
   }
   while (three1 > ansThree) {
     if (a1 % 3 == 0) {
       a1 /= 3;
       a1 *= 2;
       --three1;
       ++two1;
       continue;
     }
     if (b1 % 3 == 0) {
       b1 /= 3;
       b1 *= 2;
       --three1;
       ++two1;
       continue;
     }
     throw new AssertionError();
   }
   while (three2 > ansThree) {
     if (a2 % 3 == 0) {
       a2 /= 3;
       a2 *= 2;
       --three2;
       ++two2;
       continue;
     }
     if (b2 % 3 == 0) {
       b2 /= 3;
       b2 *= 2;
       --three2;
       ++two2;
       continue;
     }
     throw new AssertionError();
   }
   while (two1 > ansTwo) {
     if (a1 % 2 == 0) {
       a1 /= 2;
       --two1;
       continue;
     }
     if (b1 % 2 == 0) {
       b1 /= 2;
       --two1;
       continue;
     }
     throw new AssertionError();
   }
   while (two2 > ansTwo) {
     if (a2 % 2 == 0) {
       a2 /= 2;
       --two2;
       continue;
     }
     if (b2 % 2 == 0) {
       b2 /= 2;
       --two2;
       continue;
     }
     throw new AssertionError();
   }
   out.println(best);
   out.println(a1 + " " + b1);
   out.println(a2 + " " + b2);
 }
Example #14
0
  public void solve(int testNumber, FastScanner in, FastPrinter out) {
    Circle2DDouble c =
        new Circle2DDouble(new Point2DDouble(in.nextInt(), in.nextInt()), in.nextInt());
    Point2DDouble p1 = new Point2DDouble(in.nextInt(), in.nextInt());
    Point2DDouble p2 = new Point2DDouble(in.nextInt(), in.nextInt());
    if (p1.x > p2.x) {
      double t = p1.x;
      p1.x = p2.x;
      p2.x = t;
    }
    if (p1.y > p2.y) {
      double t = p1.y;
      p1.y = p2.y;
      p2.y = t;
    }
    List<Point2DDouble> allPoints = new ArrayList<>();
    allPoints.add(p1);
    allPoints.add(p2);
    allPoints.add(new Point2DDouble(p1.x, p2.y));
    allPoints.add(new Point2DDouble(p2.x, p1.y));
    allPoints.add(new Point2DDouble(c.p.x - c.radius, c.p.y));
    allPoints.add(new Point2DDouble(c.p.x + c.radius, c.p.y));
    allPoints.add(new Point2DDouble(c.p.x, c.p.y - c.radius));
    allPoints.add(new Point2DDouble(c.p.x, c.p.y + c.radius));
    for (double x : new double[] {p1.x, p2.x}) {
      if (comp.compare(x, c.p.x - c.radius) > 0 && comp.compare(x, c.p.x + c.radius) < 0) {
        double dx = c.p.x - x;
        double dy = Math.sqrt(c.radius * c.radius - dx * dx);
        allPoints.add(new Point2DDouble(x, c.p.y - dy));
        allPoints.add(new Point2DDouble(x, c.p.y + dy));
      }
    }
    for (double y : new double[] {p1.y, p2.y}) {
      if (comp.compare(y, c.p.y - c.radius) > 0 && comp.compare(y, c.p.y + c.radius) < 0) {
        double dy = c.p.y - y;
        double dx = Math.sqrt(c.radius * c.radius - dy * dy);
        allPoints.add(new Point2DDouble(c.p.x - dx, y));
        allPoints.add(new Point2DDouble(c.p.x + dx, y));
      }
    }
    List<Point2DDouble> onRect = new ArrayList<>();
    for (Point2DDouble e : allPoints) {
      if (inside(p1, p2, e)) {
        if (comp.compare(p1.x, e.x) < 0
            && comp.compare(e.x, p2.x) < 0
            && comp.compare(p1.y, e.y) < 0
            && comp.compare(e.y, p2.y) < 0) {
          continue;
        }
        onRect.add(e);
      }
    }
    List<Point2DDouble> onCircle = new ArrayList<>();
    for (Point2DDouble e : allPoints) {
      if (comp.compare(e.distance(c.p), c.radius) == 0) {
        onCircle.add(e);
      }
    }
    final Point2DDouble centerRect = p1.add(p2).multiply(0.5);
    Collections.sort(
        onRect,
        new Comparator<Point2DDouble>() {

          int get(Point2DDouble e) {
            int x = comp.compare(e.x, 0);
            int y = comp.compare(e.y, 0);
            if (x > 0) {
              return y > 0 ? 2 : y < 0 ? 8 : 1;
            } else if (x < 0) {
              return y > 0 ? 4 : y < 0 ? 6 : 5;
            } else {
              return y > 0 ? 3 : y < 0 ? 7 : 0;
            }
          }

          @Override
          public int compare(Point2DDouble o1, Point2DDouble o2) {
            o1 = o1.subtract(centerRect);
            o2 = o2.subtract(centerRect);
            int c = get(o1) - get(o2);
            if (c != 0) return c;
            return comp.compare(o2.vmul(o1), 0);
          }
        });
    final Point2DDouble circleCenter = c.p;
    Collections.sort(
        onCircle,
        new Comparator<Point2DDouble>() {

          int get(Point2DDouble e) {
            int x = comp.compare(e.x, 0);
            int y = comp.compare(e.y, 0);
            if (x > 0) {
              return y > 0 ? 2 : y < 0 ? 8 : 1;
            } else if (x < 0) {
              return y > 0 ? 4 : y < 0 ? 6 : 5;
            } else {
              return y > 0 ? 3 : y < 0 ? 7 : 0;
            }
          }

          @Override
          public int compare(Point2DDouble o1, Point2DDouble o2) {
            o1 = o1.subtract(circleCenter);
            o2 = o2.subtract(circleCenter);
            int c = get(o1) - get(o2);
            if (c != 0) return c;
            return comp.compare(o2.vmul(o1), 0);
          }
        });
    List<Segment> allSegments = new ArrayList<>();
    for (int i = 0; i < onRect.size(); i++) {
      Point2DDouble r1 = onRect.get(i);
      Point2DDouble r2 = onRect.get((i + 1) % onRect.size());
      if (pointsEqual(r1, r2)) continue;
      Point2DDouble r0 = r1.add(r2).multiply(0.5);
      if (comp.compare(r0.distance(c.p), c.radius) < 0) {
        allSegments.add(new Segment(r1, r2, 0));
      }
    }
    for (int i = 0; i < onCircle.size(); i++) {
      Point2DDouble r1 = onCircle.get(i);
      Point2DDouble r2 = onCircle.get((i + 1) % onCircle.size());
      if (pointsEqual(r1, r2)) continue;
      r1 = r1.subtract(c.p);
      r2 = r2.subtract(c.p);
      Point2DDouble r0 = r1.add(r2);
      r0 = r0.multiply(c.radius / r0.length());
      r0 = r0.add(c.p);
      if (inside(p1, p2, r0)) {
        allSegments.add(new Segment(r1.add(c.p), r2.add(c.p), c.radius));
      }
    }
    if (allSegments.isEmpty()) {
      out.println(0);
      return;
    }
    boolean[] was = new boolean[allSegments.size()];
    List<Segment> sortedSegments = new ArrayList<>();
    sortedSegments.add(allSegments.get(0));
    was[0] = true;
    Point2DDouble last = sortedSegments.get(sortedSegments.size() - 1).q;
    //        System.out.println(allSegments);
    while (!pointsEqual(sortedSegments.get(0).p, last)) {
      int id = -1;
      for (int i = 0; i < allSegments.size(); i++) {
        if (was[i]) continue;
        Segment e = allSegments.get(i);
        if (pointsEqual(e.p, last)) {
          id = i;
          break;
        }
      }
      if (id < 0) throw new AssertionError();
      was[id] = true;
      sortedSegments.add(allSegments.get(id));
      last = allSegments.get(id).q;
    }
    double area = 0;
    for (Segment e : sortedSegments) {
      area += e.p.vmul(e.q);
    }
    area = Math.abs(area) * .5;
    for (Segment e : sortedSegments) {
      if (comp.compare(e.r, 0) > 0) {
        double dist = e.p.distance(e.q);
        double sin = dist * .5 / c.radius;
        if (sin > 1) sin = 1;
        else if (sin < -1) sin = -1;
        double ang = Math.asin(sin) * 2;
        area += c.radius * c.radius * .5 * (ang - Math.sin(ang));
      }
    }
    out.println(area);
  }