Submission #200993

# Submission time Handle Problem Language Result Execution time Memory
200993 2020-02-09T04:19:03 Z model_code Slon (COCI15_slon) Python 2
120 / 120
253 ms 35628 KB
A = raw_input()
P, M = map(int, raw_input().strip().split(' '))
 
def mod (x):
  return (x[0]%M, x[1]%M)
 
def sum (a, b):
  return mod((a[0] + b[0], a[1] + b[1]))
 
def min (a, b):
  return mod((a[0] - b[0], a[1] - b[1]))
 
def mul (a, b):
  assert(not(a[0] > 0 and b[0] > 0))
  return mod((a[0]*b[1] + a[1]*b[0], a[1]*b[1]))
 
def apply (ret, c):
  if c == '+': tt = sum(ret[-2], ret[-1])
  if c == '-': tt = min(ret[-2], ret[-1])
  if c == '*': tt = mul(ret[-2], ret[-1])
  ret.pop()
  ret.pop()
  ret.append(tt)
 
opp = {'+': 0, '-': 0, '*': 1}
 
os = []
ret = []
T = []
 
val = 0
ok = 0
 
for i in range(len(A)):
  if A[i].isdigit():
    val = val*10 + int(A[i])
    ok = 1
  else:
    if ok: T.append(val)
    val = 0
    ok = 0
    T.append(A[i])
 
if ok: T.append(val)
 
for c in T:
  if c == 'x':
    ret.append((1, 0))
  elif c in opp:
    while len(os) and (os[-1] in opp) and opp[os[-1]] >= opp[c]:
      apply(ret, os[-1])
      os.pop()
    os.append(c)
  elif c == '(': os.append(c)
  elif c == ')':
    while len(os) and os[-1] != '(':
      apply(ret, os[-1])
      os.pop()
    assert(len(os) > 0)
    os.pop()
  else:
    ret.append((0, c))
 
for c in os[::-1]:
  apply(ret, c)
 
assert(len(ret) == 1)
ret = ret[0]
for i in range(0, 10**6):
  if ((ret[0]*i + ret[1])%M + M)%M == P:
    print i
    break
# Verdict Execution time Memory Grader output
1 Correct 253 ms 34864 KB Output is correct
2 Correct 210 ms 35628 KB Output is correct
3 Correct 66 ms 34720 KB Output is correct
4 Correct 124 ms 34936 KB Output is correct
5 Correct 69 ms 34936 KB Output is correct
6 Correct 155 ms 34812 KB Output is correct
7 Correct 85 ms 34808 KB Output is correct
8 Correct 103 ms 34980 KB Output is correct
9 Correct 124 ms 35152 KB Output is correct
10 Correct 148 ms 35356 KB Output is correct