結果

問題 No.1092 modular arithmetic
ユーザー forest3forest3
提出日時 2020-07-01 19:17:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 713 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 166,600 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 02:40:30
合計ジャッジ時間 6,683 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 43 ms
4,368 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 51 ms
4,348 KB
testcase_04 AC 44 ms
4,352 KB
testcase_05 AC 44 ms
4,368 KB
testcase_06 AC 32 ms
4,352 KB
testcase_07 AC 42 ms
4,352 KB
testcase_08 AC 47 ms
4,352 KB
testcase_09 AC 41 ms
4,352 KB
testcase_10 AC 36 ms
4,352 KB
testcase_11 AC 40 ms
4,352 KB
testcase_12 AC 32 ms
4,352 KB
testcase_13 AC 15 ms
4,348 KB
testcase_14 AC 17 ms
4,352 KB
testcase_15 AC 4 ms
4,352 KB
testcase_16 AC 12 ms
4,348 KB
testcase_17 AC 17 ms
4,352 KB
testcase_18 AC 52 ms
4,348 KB
testcase_19 AC 32 ms
4,348 KB
testcase_20 AC 54 ms
4,352 KB
testcase_21 AC 42 ms
4,356 KB
testcase_22 AC 51 ms
4,352 KB
testcase_23 AC 70 ms
4,352 KB
testcase_24 AC 22 ms
4,348 KB
testcase_25 AC 54 ms
4,352 KB
testcase_26 AC 66 ms
4,352 KB
testcase_27 AC 12 ms
4,352 KB
testcase_28 AC 54 ms
4,352 KB
testcase_29 AC 82 ms
4,352 KB
testcase_30 AC 54 ms
4,372 KB
testcase_31 AC 8 ms
4,356 KB
testcase_32 AC 48 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long power( long long x, long long n, long long m )
{
	long long res = 1;
	while( n > 0 ) {
		if( n % 2 ) res = res * x % m;
		x = x * x % m;
		n /= 2;
	}
	return res;
}

int main()
{
	long long P;
	int N;
	cin >> P >> N;
	vector<long long> A( N );
	for( int i = 0; i < N; i++ ) {
		cin >> A[i];
	}
	string S;
	cin >> S;

	long long ans = A[0];
	for( int i = 1; i < N; i++ ) {
		char c = S[i - 1];
		if( c == '+' ) {
			ans = (ans + A[i]) % P;
		}
		else if( c ==  '-' ) {
			ans = (ans - A[i] + P) % P;
		}
		else if( c == '*' ) {
			ans = ans * A[i] % P;
		}
		else if( c == '/' ) {
			ans = ans * power( A[i], P - 2, P ) % P;
		}
	}

	cout << ans << endl;
}
0