結果

問題 No.1092 modular arithmetic
ユーザー neko_the_shadowneko_the_shadow
提出日時 2020-07-09 15:26:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 475 ms / 2,000 ms
コード長 4,259 bytes
コンパイル時間 3,175 ms
コンパイル使用メモリ 84,704 KB
実行使用メモリ 54,756 KB
最終ジャッジ日時 2024-10-07 05:28:56
合計ジャッジ時間 16,432 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
38,336 KB
testcase_01 AC 389 ms
54,756 KB
testcase_02 AC 89 ms
38,132 KB
testcase_03 AC 446 ms
51,224 KB
testcase_04 AC 372 ms
50,556 KB
testcase_05 AC 395 ms
50,544 KB
testcase_06 AC 341 ms
46,460 KB
testcase_07 AC 402 ms
50,728 KB
testcase_08 AC 431 ms
51,096 KB
testcase_09 AC 393 ms
51,268 KB
testcase_10 AC 366 ms
50,528 KB
testcase_11 AC 369 ms
50,432 KB
testcase_12 AC 347 ms
46,296 KB
testcase_13 AC 264 ms
48,232 KB
testcase_14 AC 276 ms
47,632 KB
testcase_15 AC 165 ms
42,248 KB
testcase_16 AC 254 ms
48,192 KB
testcase_17 AC 284 ms
47,664 KB
testcase_18 AC 434 ms
50,820 KB
testcase_19 AC 347 ms
46,424 KB
testcase_20 AC 436 ms
51,308 KB
testcase_21 AC 375 ms
50,696 KB
testcase_22 AC 418 ms
51,240 KB
testcase_23 AC 447 ms
51,184 KB
testcase_24 AC 271 ms
43,504 KB
testcase_25 AC 402 ms
48,496 KB
testcase_26 AC 440 ms
51,060 KB
testcase_27 AC 209 ms
41,212 KB
testcase_28 AC 402 ms
47,632 KB
testcase_29 AC 475 ms
51,308 KB
testcase_30 AC 405 ms
47,280 KB
testcase_31 AC 183 ms
40,976 KB
testcase_32 AC 366 ms
48,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public void exec() {
        long p = stdin.nextLong();
        int n = stdin.nextInt();
        long[] a = stdin.nextLongArray(n);
        String s = stdin.nextString();

        long ans = a[0];
        for (int i = 0; i < n - 1; i++) {
            if (s.charAt(i) == '+') ans = (ans + a[i+1]) % p;
            if (s.charAt(i) == '-') ans = (ans - a[i+1] + p) % p;
            if (s.charAt(i) == '*') ans = (ans * a[i+1]) % p;
            if (s.charAt(i) == '/') ans = (ans * modInv(a[i+1], p)) % p;
        }
        stdout.println(ans);
    }

    public long modPow(long x, long y, long mod) {
        long z = 1;
        while (y > 0) {
            if (y % 2 == 0) {
                x = (x * x) % mod;
                y /= 2;
            } else {
                z = (z * x) % mod;
                y--;
            }
        }
        return z;
    }

    public long modInv(long x, long mod) {
        return modPow(x, mod - 2, mod);
    }

    private static final Stdin stdin = new Stdin();
    private static final Stdout stdout = new Stdout();

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

    public static class Stdin {
        private BufferedReader stdin;
        private Deque<String> tokens;
        private Pattern delim;

        public Stdin() {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            tokens = new ArrayDeque<>();
            delim = Pattern.compile(" ");
        }

        public String nextString() {
            try {
                if (tokens.isEmpty()) {
                    String line = stdin.readLine();
                    delim.splitAsStream(line).forEach(tokens::addLast);
                }
                return tokens.pollFirst();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public long nextLong() {
            return Long.parseLong(nextString());
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout() {
            stdout =  new PrintWriter(System.out, false);
        }

        public void printf(String format, Object ... args) {
            stdout.printf(format, args);
        }

        public void println(Object ... objs) {
            String line = Arrays.stream(objs).map(this::deepToString).collect(Collectors.joining(" "));
            stdout.println(line);
        }

        private String deepToString(Object o) {
            if (o == null || !o.getClass().isArray()) {
                return Objects.toString(o);
            }

            int len = Array.getLength(o);
            String[] tokens = new String[len];
            for (int i = 0; i < len; i++) {
                tokens[i] = deepToString(Array.get(o, i));
            }
            return "{" + String.join(",", tokens) + "}";
        }

        public void flush() {
            stdout.flush();
        }
    }
}
0