Submission #263636

# Submission time Handle Problem Language Result Execution time Memory
263636 2020-08-13T22:59:43 Z ijxjdjd Boat (APIO16_boat) Java 11
100 / 100
1453 ms 39344 KB

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.StringTokenizer;

public class boat {
    static long[][] choose = new long[501][501];
    static long[] inverse = new long[505];
    public static long inverse(long a, long mod) {
        long[] inv = extended_gcd(a, mod);
        if (inv[0] != 1) {
            return 0;
        }
        else {
            return (inv[1] + 2*mod)%mod;
        }
    }
    public static long[] extended_gcd(long a, long b) {
        //three numbers, first is gcd, second is x, third is y
        if (a == 0) {
            return new long[]{b, 0, 1};
        }
        long[] next = extended_gcd(b%a, a);
        long tempX = next[1];
        next[1] = next[2] - (b/a) * next[1];
        next[2] = tempX;
        return next;
    }
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N = in.nextInt();
        int[][] intervals = new int[N][2];
        HashSet<Integer> points = new HashSet<>();
        for (int i = 0; i <= 500; i++) {
            choose[i][0] = 1;
            for (int j = 1; j <= i; j++) {
                choose[i][j] = (choose[i-1][j] + choose[i-1][j-1])%MOD;
            }
        }
        for (int i = 1; i < inverse.length; i++) {
            inverse[i] = inverse(i, MOD);
        }
        for (int i = 0; i < N; i++) {
            intervals[i][0] = in.nextInt();
            intervals[i][1] = in.nextInt();
            points.add(intervals[i][0]);
            points.add(intervals[i][1]+1);
        }
        ArrayList<Integer> pSort = new ArrayList<>(points);
        ArrayList<Integer>[] items = new ArrayList[pSort.size()];
        for (int i = 0; i < pSort.size(); i++) {
            items[i] = new ArrayList<>();
        }
        Collections.sort(pSort);
        for (int i = 0; i < N; i++) {
            boolean flag = false;
            for (int j = 0; j < pSort.size(); j++) {
                if (pSort.get(j) == intervals[i][1]+1) {
                    break;
                }
                if (flag || pSort.get(j) == intervals[i][0]) {
                    flag = true;
                    items[j].add(i+1);
                }

            }
        }
        long[] dp = new long[N+1];
        dp[0] = 1;
        for (int i = 0; i < pSort.size()-1; i++) {
            if (items[i].size() > 0) {
                long[] pref = new long[N + 1];
                pref[0] = dp[0];
                for (int j = 1; j <= N; j++) {
                    pref[j] = dp[j] + pref[j - 1];
                    while (pref[j] >= MOD) {
                        pref[j] -= MOD;
                    }
                }
                long[] pathCount = new long[items[i].size()];
                long sz = pSort.get(i + 1) - pSort.get(i);
                long[] szCount = new long[items[i].size()];
                szCount[0] = (sz * (sz - 1)) / 2;
                szCount[0] %= MOD;
                for (int j = 1; j < items[i].size(); j++) {
                    szCount[j] = ((szCount[j - 1] * (sz - j - 1))%MOD) * inverse[j + 2];
                    szCount[j] %= MOD;
                }
                for (int j = 1; j < items[i].size(); j++) {
                    for (int k = 0; k < j; k++) {
                        pathCount[j] += szCount[k] * choose[j - 1][k];
                        pathCount[j] %= MOD;
                    }
                }
                pathCount[0] = sz;
                for (int j = 0; j < items[i].size(); j++) {
                    for (int d = 0; d <= j; d++) {
                        dp[items[i].get(j)] += pathCount[d] * pref[items[i].get(j - d) - 1];
                        dp[items[i].get(j)] %= MOD;
                    }
                }
            }
        }
        long tot = 0;
        for (int i = 1; i < dp.length; i++) {
            tot += dp[i];
            while (tot >= MOD) {
                tot -= MOD;
            }
        }
        out.println(tot);
        out.close();
    }
    static long MOD = (int)(1e9) + 7;

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public String nextLine() {
            try {
                return reader.readLine();
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 179 ms 17560 KB Output is correct
2 Correct 191 ms 17420 KB Output is correct
3 Correct 180 ms 17548 KB Output is correct
4 Correct 181 ms 17528 KB Output is correct
5 Correct 187 ms 17636 KB Output is correct
6 Correct 174 ms 17556 KB Output is correct
7 Correct 191 ms 17444 KB Output is correct
8 Correct 192 ms 17656 KB Output is correct
9 Correct 197 ms 17712 KB Output is correct
10 Correct 178 ms 17424 KB Output is correct
11 Correct 185 ms 17384 KB Output is correct
12 Correct 193 ms 18076 KB Output is correct
13 Correct 197 ms 17836 KB Output is correct
14 Correct 180 ms 17528 KB Output is correct
15 Correct 194 ms 17528 KB Output is correct
16 Correct 166 ms 15604 KB Output is correct
17 Correct 172 ms 15460 KB Output is correct
18 Correct 156 ms 15588 KB Output is correct
19 Correct 172 ms 16212 KB Output is correct
20 Correct 155 ms 15480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 17560 KB Output is correct
2 Correct 191 ms 17420 KB Output is correct
3 Correct 180 ms 17548 KB Output is correct
4 Correct 181 ms 17528 KB Output is correct
5 Correct 187 ms 17636 KB Output is correct
6 Correct 174 ms 17556 KB Output is correct
7 Correct 191 ms 17444 KB Output is correct
8 Correct 192 ms 17656 KB Output is correct
9 Correct 197 ms 17712 KB Output is correct
10 Correct 178 ms 17424 KB Output is correct
11 Correct 185 ms 17384 KB Output is correct
12 Correct 193 ms 18076 KB Output is correct
13 Correct 197 ms 17836 KB Output is correct
14 Correct 180 ms 17528 KB Output is correct
15 Correct 194 ms 17528 KB Output is correct
16 Correct 166 ms 15604 KB Output is correct
17 Correct 172 ms 15460 KB Output is correct
18 Correct 156 ms 15588 KB Output is correct
19 Correct 172 ms 16212 KB Output is correct
20 Correct 155 ms 15480 KB Output is correct
21 Correct 515 ms 26944 KB Output is correct
22 Correct 520 ms 26528 KB Output is correct
23 Correct 477 ms 26356 KB Output is correct
24 Correct 512 ms 26500 KB Output is correct
25 Correct 550 ms 27068 KB Output is correct
26 Correct 783 ms 29076 KB Output is correct
27 Correct 860 ms 30924 KB Output is correct
28 Correct 874 ms 30436 KB Output is correct
29 Correct 825 ms 29176 KB Output is correct
30 Correct 214 ms 19636 KB Output is correct
31 Correct 191 ms 19504 KB Output is correct
32 Correct 198 ms 19832 KB Output is correct
33 Correct 188 ms 19468 KB Output is correct
34 Correct 191 ms 19532 KB Output is correct
35 Correct 194 ms 19780 KB Output is correct
36 Correct 207 ms 19608 KB Output is correct
37 Correct 190 ms 19268 KB Output is correct
38 Correct 197 ms 19704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 15348 KB Output is correct
2 Correct 164 ms 15420 KB Output is correct
3 Correct 156 ms 15596 KB Output is correct
4 Correct 143 ms 15816 KB Output is correct
5 Correct 134 ms 15720 KB Output is correct
6 Correct 165 ms 16340 KB Output is correct
7 Correct 164 ms 16020 KB Output is correct
8 Correct 174 ms 16316 KB Output is correct
9 Correct 202 ms 16680 KB Output is correct
10 Correct 180 ms 16460 KB Output is correct
11 Correct 150 ms 15936 KB Output is correct
12 Correct 137 ms 15224 KB Output is correct
13 Correct 154 ms 16096 KB Output is correct
14 Correct 151 ms 15220 KB Output is correct
15 Correct 156 ms 15480 KB Output is correct
16 Correct 129 ms 14964 KB Output is correct
17 Correct 151 ms 15352 KB Output is correct
18 Correct 137 ms 15424 KB Output is correct
19 Correct 135 ms 15068 KB Output is correct
20 Correct 128 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 17560 KB Output is correct
2 Correct 191 ms 17420 KB Output is correct
3 Correct 180 ms 17548 KB Output is correct
4 Correct 181 ms 17528 KB Output is correct
5 Correct 187 ms 17636 KB Output is correct
6 Correct 174 ms 17556 KB Output is correct
7 Correct 191 ms 17444 KB Output is correct
8 Correct 192 ms 17656 KB Output is correct
9 Correct 197 ms 17712 KB Output is correct
10 Correct 178 ms 17424 KB Output is correct
11 Correct 185 ms 17384 KB Output is correct
12 Correct 193 ms 18076 KB Output is correct
13 Correct 197 ms 17836 KB Output is correct
14 Correct 180 ms 17528 KB Output is correct
15 Correct 194 ms 17528 KB Output is correct
16 Correct 166 ms 15604 KB Output is correct
17 Correct 172 ms 15460 KB Output is correct
18 Correct 156 ms 15588 KB Output is correct
19 Correct 172 ms 16212 KB Output is correct
20 Correct 155 ms 15480 KB Output is correct
21 Correct 515 ms 26944 KB Output is correct
22 Correct 520 ms 26528 KB Output is correct
23 Correct 477 ms 26356 KB Output is correct
24 Correct 512 ms 26500 KB Output is correct
25 Correct 550 ms 27068 KB Output is correct
26 Correct 783 ms 29076 KB Output is correct
27 Correct 860 ms 30924 KB Output is correct
28 Correct 874 ms 30436 KB Output is correct
29 Correct 825 ms 29176 KB Output is correct
30 Correct 214 ms 19636 KB Output is correct
31 Correct 191 ms 19504 KB Output is correct
32 Correct 198 ms 19832 KB Output is correct
33 Correct 188 ms 19468 KB Output is correct
34 Correct 191 ms 19532 KB Output is correct
35 Correct 194 ms 19780 KB Output is correct
36 Correct 207 ms 19608 KB Output is correct
37 Correct 190 ms 19268 KB Output is correct
38 Correct 197 ms 19704 KB Output is correct
39 Correct 146 ms 15348 KB Output is correct
40 Correct 164 ms 15420 KB Output is correct
41 Correct 156 ms 15596 KB Output is correct
42 Correct 143 ms 15816 KB Output is correct
43 Correct 134 ms 15720 KB Output is correct
44 Correct 165 ms 16340 KB Output is correct
45 Correct 164 ms 16020 KB Output is correct
46 Correct 174 ms 16316 KB Output is correct
47 Correct 202 ms 16680 KB Output is correct
48 Correct 180 ms 16460 KB Output is correct
49 Correct 150 ms 15936 KB Output is correct
50 Correct 137 ms 15224 KB Output is correct
51 Correct 154 ms 16096 KB Output is correct
52 Correct 151 ms 15220 KB Output is correct
53 Correct 156 ms 15480 KB Output is correct
54 Correct 129 ms 14964 KB Output is correct
55 Correct 151 ms 15352 KB Output is correct
56 Correct 137 ms 15424 KB Output is correct
57 Correct 135 ms 15068 KB Output is correct
58 Correct 128 ms 14968 KB Output is correct
59 Correct 687 ms 27612 KB Output is correct
60 Correct 707 ms 27736 KB Output is correct
61 Correct 694 ms 27708 KB Output is correct
62 Correct 788 ms 30304 KB Output is correct
63 Correct 754 ms 28732 KB Output is correct
64 Correct 1310 ms 31688 KB Output is correct
65 Correct 1425 ms 33196 KB Output is correct
66 Correct 1192 ms 31164 KB Output is correct
67 Correct 1226 ms 30724 KB Output is correct
68 Correct 1453 ms 39344 KB Output is correct
69 Correct 679 ms 28044 KB Output is correct
70 Correct 692 ms 26512 KB Output is correct
71 Correct 691 ms 27992 KB Output is correct
72 Correct 657 ms 27184 KB Output is correct
73 Correct 725 ms 28776 KB Output is correct
74 Correct 396 ms 20208 KB Output is correct
75 Correct 411 ms 20000 KB Output is correct
76 Correct 386 ms 20904 KB Output is correct
77 Correct 368 ms 20308 KB Output is correct
78 Correct 369 ms 20192 KB Output is correct