QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 64 MB مجموع النقاط: 120

#13765. SLON

الإحصائيات

一个名叫 Slon 的学生在学校里非常调皮。他上课总是觉得无聊,并且总是捣乱。老师想让他安静下来并“驯服”他,于是给他出了一道很难的数学题。

老师给 Slon 一个算术表达式 $A$,以及整数 $P$ 和 $M$。Slon 需要回答以下问题:“在表达式 $A$ 中,变量 $x$ 的最小非负值是多少,使得 $A$ 除以 $M$ 的余数等于 $P$?”。保证解总是存在。

此外,如果我们将分配律应用于表达式 $A$,变量 $x$ 不会与变量 $x$ 相乘(形式上,该表达式是关于变量 $x$ 的一次多项式)。

合法表达式 $A$ 的示例:$5 + x * (3 + 2)$,$x + 3 * x + 4 * (5 + 3 * (2 + x - 2 * x))$。

不合法表达式 $A$ 的示例:$5 * (3 + x * (3 + x))$,$x * (x + x * (1 + x))$。

输入格式

第一行输入包含表达式 $A$($1 \le |A| \le 100\,000$)。

第二行输入包含两个整数 $P$($0 \le P \le M - 1$)和 $M$($1 \le M \le 1\,000\,000$)。

算术表达式 $A$ 仅由字符 +-*()x 以及数字 09 组成。

括号总是成对出现,运算符 +-* 总是应用于恰好两个值(不会出现类似 (-5)(4+-5) 的表达式),并且所有乘法都是显式的(不会出现类似 4(5)2(x) 的表达式)。

输出格式

输出的第一行也是唯一一行必须包含变量 $x$ 的最小非负值。

样例

输入样例 1

5+3+x
9 10

输出样例 1

1

输入样例 2

20+3+x
0 5

输出样例 2

2

输入样例 3

3*(x+(x+4)*5)
1 7

输出样例 3

1

说明

第一个样例的解释:当 $x = 0$ 时,$5 + 3 + x$ 除以 $10$ 的余数是 $8$;当 $x = 1$ 时,除以 $10$ 的余数是 $9$,这就是我们要找的解。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.