Submission #3411160


Source Code Expand

Copy
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    final int mod = (int) 1e9 + 7;

    int add(int x, int y) {
        x += y;
        return x >= mod ? (x - mod) : x;
    }

    int sub(int x, int y) {
        x -= y;
        return x <0 ? (x + mod) : x;
    }

    int mul(int x, int y) {
        return (int) (x * 1L * y % mod);
    }

    void solve() {
        int n = in.nextInt() + 1;

        int[] fact = new int[n];
        fact[0] = 1;
        for (int i = 1; i < n; i++) {
            fact[i] = mul(fact[i - 1], i);
        }
        int[] inv = new int[n];
        for (int i = n - 1; i > 0; i--) {
            inv[i] = BigInteger.valueOf(i).modInverse(BigInteger.valueOf(mod)).intValue();
        }
        inv[0] = 1;

        n--;
        int[] f = new int[n];
        for (int k = 0; k < n; k++) {
            f[k] = mul(fact[n], inv[k + 1]);
        }
        int[] pref = new int[n];
        pref[0] = f[0];
        for (int i = 1; i < n; i++) {
            pref[i] = add(pref[i - 1], f[i]);
        }

        int[] a = new int[n];
        int res = 0;
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            res = add(res, mul(a[i], pref[i]));
            res= add(res, mul(a[i], pref[n - 1 -i ]));
            res = sub(res, mul(a[i], f[0]));
        }
        out.println(res);
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}

Submission Info

Submission Time
Task B - Removing Blocks
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 600
Code Size 3645 Byte
Status
Exec Time 765 ms
Memory 55708 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
All 600 / 600 sample-01.txt, sample-02.txt, sample-03.txt, sample-01.txt, sample-02.txt, sample-03.txt, subtask01-01.txt, subtask01-02.txt, subtask01-03.txt, subtask01-04.txt, subtask01-05.txt, subtask01-06.txt, subtask01-07.txt, subtask01-08.txt, subtask01-09.txt, subtask01-10.txt, subtask01-11.txt, subtask01-12.txt, subtask01-13.txt
Case Name Status Exec Time Memory
sample-01.txt 75 ms 20564 KB
sample-02.txt 73 ms 18260 KB
sample-03.txt 73 ms 19924 KB
subtask01-01.txt 70 ms 17616 KB
subtask01-02.txt 637 ms 55412 KB
subtask01-03.txt 509 ms 49760 KB
subtask01-04.txt 595 ms 54440 KB
subtask01-05.txt 321 ms 47180 KB
subtask01-06.txt 414 ms 53180 KB
subtask01-07.txt 297 ms 49904 KB
subtask01-08.txt 765 ms 55140 KB
subtask01-09.txt 740 ms 55708 KB
subtask01-10.txt 750 ms 53076 KB
subtask01-11.txt 733 ms 54668 KB
subtask01-12.txt 747 ms 53984 KB
subtask01-13.txt 710 ms 54352 KB