結果

問題 No.1092 modular arithmetic
ユーザー pengin_2000
提出日時 2022-01-18 17:11:02
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 686 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 30,080 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-23 13:23:55
合計ジャッジ時間 3,297 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
long long int modpow(long long int a, long long int n, long long int p)
{
	long long int res = 1;
	while (n > 0)
	{
		if (n % 2 > 0)
			res = res * a % p;
		a = a * a % p;
		n /= 2;
	}
	return res;
}
int main()
{
	long long int p, n;
	scanf("%lld %lld", &p, &n);
	long long int i;
	long long int a[100005];
	for (i = 0; i < n; i++)
		scanf("%lld", &a[i]);
	char s[100005];
	scanf("%s", s);
	long long int ans = a[0];
	for (i = 1; i < n; i++)
	{
		if (s[i - 1] == '+')
			ans += a[i];
		else if (s[i - 1] == '-')
			ans += p - a[i];
		else if (s[i - 1] == '*')
			ans *= a[i];
		else
			ans *= modpow(a[i], p - 2, p);
		ans %= p;
	}
	printf("%lld\n", ans);
	return 0;
}
0