Submission #1306092


Source Code Expand

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

public class Main {
    private static boolean debug = false;
    private static boolean elapsed = false;

    private static PrintWriter _out = new PrintWriter(System.out);
    private static PrintWriter _err = new PrintWriter(System.err);

    private void solve(BufferedReader in) throws IOException {
        int N = Integer.parseInt(in.readLine());
        long[] A = new long[N];
        long[] B = new long[N];
        long[] r = new long[N];
        for (int i = 0; i < N; ++i) {
            String[] s = in.readLine().split(" ");
            A[i] = Long.parseLong(s[0]);
            B[i] = Long.parseLong(s[1]);
            r[i] = B[i] - (A[i] % B[i]);
        }

        long ans = 0;
        for (int i = N - 1; i >= 0; --i) {
            long ct = r[i] - ans;
            while (ct < 0) {
                ct += B[i];
            }
            ct = ct % B[i];
            ans += ct;
        }
        _out.println(ans);
    }
    private static BigInteger C(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        for (long i = r; i > 1; --i) {
            res = res.divide(BigInteger.valueOf(i));
        }
        return res;
    }
    private static BigInteger P(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
    /*
     * 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
     * 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
     */
    public static void main(String[] args) throws IOException {
        long S = System.currentTimeMillis();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        new Main().solve(in);
        _out.flush();

        long G = System.currentTimeMillis();
        if (elapsed) {
            _err.println((G - S) + "ms");
        }
        _err.flush();
    }
}

Submission Info

Submission Time
Task A - Multiple Array
User hhelibex
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 2139 Byte
Status TLE
Exec Time 2109 ms
Memory 48016 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 300
Status
AC × 2
AC × 10
TLE × 8
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt TLE 2105 ms 46068 KB
02.txt TLE 2109 ms 47412 KB
03.txt TLE 2109 ms 45136 KB
04.txt TLE 2109 ms 48016 KB
05.txt TLE 2105 ms 44660 KB
06.txt TLE 2105 ms 47404 KB
07.txt AC 262 ms 47964 KB
08.txt AC 1146 ms 46380 KB
09.txt AC 273 ms 46440 KB
10.txt TLE 2105 ms 46928 KB
11.txt AC 251 ms 46928 KB
12.txt AC 248 ms 45464 KB
13.txt TLE 2109 ms 44016 KB
14.txt AC 290 ms 44224 KB
15.txt AC 70 ms 21076 KB
16.txt AC 69 ms 19284 KB
s1.txt AC 75 ms 20436 KB
s2.txt AC 69 ms 21076 KB