#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100050
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef short int si;
typedef array <ll, 3> a3;
typedef array <ll, 5> a5;
//typedef tree <ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
string a;
ll p, m;
ll sm(ll x, ll y) {return (x + y + m) % m;}
ll mult(ll x, ll y) {return (x * y) % m;}
bool gd(ll x) {return a[x] != '+' && a[x] != '-' && a[x] != '*' && a[x] != '(' && a[x] != ')';}
ll get_num(ll &i)
{
ll num = 0, j = 0;
while (i + j + 1 < sz(a) && gd(i + j + 1)) j++;
for (ll u = i; u <= i + j; u++) {num *= 10; num += a[u] - '0'; num = sm(num, 0);}
if (j == 0 && a[i + j] == 'x') num = -1;
i += j + 1;
return num;
}
pair <pair <ll, ll>, ll> calc(ll v);
pair <ll, ll> get_mult(ll cur, ll kft, ll dob, ll &i)
{
for(;; i++)
{
if (a[i] == '(')
{
pair <pair <ll, ll>, ll> pt = calc(i);
if (pt.F.S != 0) kft = mult(pt.F.S, cur);
if (pt.F.F != 0) {if (pt.F.S != 0) dob = 1; else kft = mult(kft, pt.F.F); cur = mult(cur, pt.F.F);}
i = pt.S;
}
else
{
ll num = get_num(i);
if (num == -1) kft = cur; else {cur = mult(cur, num); kft = mult(kft, num);}
}
if (a[i] == '*') continue;
ll sumr = 0;
if (kft == 0) sumr = sm(sumr, cur); else sumr = sm(sumr, mult(dob, cur));
return {sumr, kft};
}
}
pair <pair <ll, ll>, ll> calc(ll v)
{
ll kf = 0, val = 0, otr = 1;
for (ll i = v + 1; i < sz(a); i++)
{
ll cur = 1, kft = 0, dob = 0;
if (a[i] == ')') return {{val, kf}, i + 1};
if (a[i] == '+') {otr = 1; continue;}
if (a[i] == '-') {otr = -1; continue;}
if (a[i] == '(')
{
pair <pair <ll, ll>, ll> pt = calc(i);
i = pt.S;
if (a[i] != '*')
{
val = sm(val, mult(otr, pt.F.F));
kf = sm(kf, mult(otr, pt.F.S));
i--;
continue;
}
else {cur = max(cur, pt.F.F); if (pt.F.S != 0) {if (pt.F.F != 0) dob = 1; kft = pt.F.S;}}
}
else
{
ll num = get_num(i);
if (a[i] != '*')
{
if (num != -1) val = sm(val, mult(otr, num)); else kf = sm(kf, otr);
i--;
continue;
}
if (num == -1) kft = 1; else cur = num;
}
i++;
pair <ll, ll> tr = get_mult(cur, kft, dob, i);
kf = sm(kf, mult(otr, tr.S));
val = sm(val, mult(otr, tr.F));
i--;
}
}
void gcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1;
y = 0;
return;
}
ll X, Y;
gcd(b, a % b, X, Y);
x = Y;
y = (X - (a / b) * Y + m) % m;
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> a;
cin >> p >> m;
a = '(' + a;
a = a + ')';
pair <pair <ll, ll>, ll> pt = calc(0);
ll val = pt.F.F, kf = pt.F.S;
//cout << val << " " << kf << endl;
p -= val;
p = sm(p, m);
ll gc = __gcd(m, kf);
m /= gc;
p /= gc;
kf /= gc;
ll x, y;
gcd(kf, m, x, y);
cout << sm(0, mult(p, x)) << endl;
}
Compilation message
slon.cpp: In function 'std::pair<std::pair<long long int, long long int>, long long int> calc(ll)':
slon.cpp:147:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
636 KB |
Output isn't correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |