結果

問題 No.1092 modular arithmetic
ユーザー tentententen
提出日時 2023-02-16 15:09:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 2,357 bytes
コンパイル時間 3,422 ms
コンパイル使用メモリ 100,856 KB
実行使用メモリ 57,728 KB
最終ジャッジ日時 2023-09-25 19:48:41
合計ジャッジ時間 13,461 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,420 KB
testcase_01 AC 231 ms
56,132 KB
testcase_02 AC 44 ms
49,384 KB
testcase_03 AC 282 ms
56,420 KB
testcase_04 AC 259 ms
56,352 KB
testcase_05 AC 225 ms
56,180 KB
testcase_06 AC 239 ms
56,468 KB
testcase_07 AC 236 ms
56,516 KB
testcase_08 AC 243 ms
56,764 KB
testcase_09 AC 231 ms
56,356 KB
testcase_10 AC 220 ms
56,004 KB
testcase_11 AC 226 ms
56,188 KB
testcase_12 AC 238 ms
56,680 KB
testcase_13 AC 151 ms
56,004 KB
testcase_14 AC 161 ms
55,712 KB
testcase_15 AC 93 ms
52,140 KB
testcase_16 AC 135 ms
55,908 KB
testcase_17 AC 159 ms
56,008 KB
testcase_18 AC 282 ms
56,628 KB
testcase_19 AC 236 ms
56,572 KB
testcase_20 AC 277 ms
57,104 KB
testcase_21 AC 224 ms
56,228 KB
testcase_22 AC 270 ms
56,556 KB
testcase_23 AC 312 ms
57,308 KB
testcase_24 AC 188 ms
55,816 KB
testcase_25 AC 301 ms
57,296 KB
testcase_26 AC 290 ms
57,368 KB
testcase_27 AC 122 ms
52,152 KB
testcase_28 AC 297 ms
57,208 KB
testcase_29 AC 328 ms
56,904 KB
testcase_30 AC 308 ms
57,728 KB
testcase_31 AC 99 ms
52,172 KB
testcase_32 AC 291 ms
57,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int MOD;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        MOD = sc.nextInt();
        int n = sc.nextInt();
        long ans = sc.nextInt();
        int[] values = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            values[i] = sc.nextInt();
        }
        char[] opes = sc.next().toCharArray();
        for (int i = 0; i < n - 1; i++) {
            if (opes[i] == '+') {
                ans += values[i];
            } else if (opes[i] == '-') {
                ans += MOD - values[i];
            } else if (opes[i] == '*') {
                ans *= values[i];
            } else {
                ans *= pow(values[i], MOD - 2);
            }
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0