結果
| 問題 |
No.1092 modular arithmetic
|
| ユーザー |
onsen_manjuuu
|
| 提出日時 | 2020-06-24 02:29:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 2,018 bytes |
| コンパイル時間 | 1,655 ms |
| コンパイル使用メモリ | 170,176 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-03 19:45:03 |
| 合計ジャッジ時間 | 3,688 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 32 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int mod;
struct ModInt{
int x;
ModInt():x(0){}
ModInt(long long y):x(y>=0?y%mod:(mod-(-y)%mod)%mod){}
ModInt &operator+=(const ModInt &p){
if((x+=p.x)>=mod)x-=mod;
return *this;
}
ModInt &operator-=(const ModInt &p){
if((x+=mod-p.x)>=mod)x-=mod;
return *this;
}
ModInt &operator*=(const ModInt &p){
x=(int)(1LL*x*p.x%mod);
return *this;
}
ModInt &operator/=(const ModInt &p){
*this*=p.inverse();
return *this;
}
ModInt &operator^=(long long p){
ModInt res = 1;
for (; p; p >>= 1) {
if (p & 1) res *= *this;
*this *= *this;
}
return *this = res;
}
ModInt operator-()const{return ModInt(-x);}
ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;}
ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;}
ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;}
ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;}
ModInt operator^(long long p)const{return ModInt(*this)^=p;}
bool operator==(const ModInt &p)const{return x==p.x;}
bool operator!=(const ModInt &p)const{return x!=p.x;}
explicit operator int() const { return x; }
ModInt operator=(const int p) {x = p; return ModInt(*this);}
ModInt inverse()const{
int a=x,b=mod,u=1,v=0,t;
while(b>0){
t=a/b;
a-=t*b;
std::swap(a,b);
u-=t*v;
std::swap(u,v);
}
return ModInt(u);
}
};
int main()
{
int n;
cin >> mod >> n;
vector<int> a(n);
string s;
for(int i = 0; i < n; i++)cin >> a[i];
cin >> s;
ModInt cur = a.front();
for(int i = 0; i < n - 1; i++) {
char op = s[i];
if(op == '+')cur += a[i + 1];
if(op == '-')cur -= a[i + 1];
if(op == '*')cur *= a[i + 1];
if(op == '/')cur /= a[i + 1];
}
cout << (int)cur << endl;
}
onsen_manjuuu