結果

問題 No.1092 modular arithmetic
ユーザー tentententen
提出日時 2022-08-04 14:21:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 1,794 bytes
コンパイル時間 1,895 ms
コンパイル使用メモリ 74,348 KB
実行使用メモリ 57,568 KB
最終ジャッジ日時 2023-10-12 11:52:21
合計ジャッジ時間 12,594 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,412 KB
testcase_01 AC 227 ms
56,048 KB
testcase_02 AC 42 ms
49,408 KB
testcase_03 AC 255 ms
56,652 KB
testcase_04 AC 216 ms
55,956 KB
testcase_05 AC 226 ms
56,740 KB
testcase_06 AC 225 ms
56,340 KB
testcase_07 AC 217 ms
56,204 KB
testcase_08 AC 233 ms
56,052 KB
testcase_09 AC 223 ms
56,024 KB
testcase_10 AC 210 ms
55,892 KB
testcase_11 AC 216 ms
56,124 KB
testcase_12 AC 232 ms
56,032 KB
testcase_13 AC 145 ms
56,076 KB
testcase_14 AC 163 ms
55,840 KB
testcase_15 AC 99 ms
52,504 KB
testcase_16 AC 134 ms
55,356 KB
testcase_17 AC 152 ms
55,960 KB
testcase_18 AC 251 ms
56,848 KB
testcase_19 AC 224 ms
54,176 KB
testcase_20 AC 269 ms
56,428 KB
testcase_21 AC 225 ms
56,308 KB
testcase_22 AC 265 ms
56,824 KB
testcase_23 AC 303 ms
57,528 KB
testcase_24 AC 182 ms
55,820 KB
testcase_25 AC 284 ms
57,016 KB
testcase_26 AC 320 ms
57,216 KB
testcase_27 AC 121 ms
52,548 KB
testcase_28 AC 294 ms
57,236 KB
testcase_29 AC 327 ms
56,120 KB
testcase_30 AC 289 ms
57,396 KB
testcase_31 AC 96 ms
52,124 KB
testcase_32 AC 281 ms
57,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        long ans = values[0];
        char[] opps = sc.next().toCharArray();
        for (int i = 1; i < n; i++) {
            if (opps[i - 1] == '+') {
                ans += values[i];
            } else if (opps[i - 1] == '-') {
                ans += mod - values[i];
            } else if (opps[i - 1] == '*') {
                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 Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0