結果

問題 No.1092 modular arithmetic
ユーザー tentententen
提出日時 2023-08-01 17:30:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 2,354 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 91,100 KB
実行使用メモリ 46,408 KB
最終ジャッジ日時 2024-04-19 16:42:38
合計ジャッジ時間 11,373 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,156 KB
testcase_01 AC 239 ms
45,784 KB
testcase_02 AC 46 ms
36,852 KB
testcase_03 AC 242 ms
45,704 KB
testcase_04 AC 223 ms
45,296 KB
testcase_05 AC 237 ms
45,500 KB
testcase_06 AC 223 ms
44,412 KB
testcase_07 AC 226 ms
45,124 KB
testcase_08 AC 238 ms
45,424 KB
testcase_09 AC 224 ms
45,296 KB
testcase_10 AC 208 ms
44,472 KB
testcase_11 AC 222 ms
44,996 KB
testcase_12 AC 227 ms
44,584 KB
testcase_13 AC 152 ms
44,836 KB
testcase_14 AC 163 ms
44,356 KB
testcase_15 AC 107 ms
40,308 KB
testcase_16 AC 145 ms
43,668 KB
testcase_17 AC 164 ms
44,364 KB
testcase_18 AC 271 ms
45,560 KB
testcase_19 AC 216 ms
44,424 KB
testcase_20 AC 269 ms
45,860 KB
testcase_21 AC 228 ms
44,832 KB
testcase_22 AC 251 ms
45,376 KB
testcase_23 AC 309 ms
46,408 KB
testcase_24 AC 168 ms
41,808 KB
testcase_25 AC 293 ms
46,160 KB
testcase_26 AC 285 ms
46,380 KB
testcase_27 AC 113 ms
40,176 KB
testcase_28 AC 268 ms
45,352 KB
testcase_29 AC 321 ms
45,740 KB
testcase_30 AC 278 ms
45,784 KB
testcase_31 AC 108 ms
39,536 KB
testcase_32 AC 288 ms
45,608 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[] ops = sc.next().toCharArray();
        for (int i = 0; i < n - 1; i++) {
            if (ops[i] == '+') {
                ans += values[i];
            } else if (ops[i] == '-') {
                ans += MOD - values[i];
            } else if (ops[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