結果

問題 No.1092 modular arithmetic
ユーザー forest3
提出日時 2020-07-01 19:17:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 713 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 168,840 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-14 02:27:05
合計ジャッジ時間 5,011 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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