再次求调
查看原帖
再次求调
731925
happy_zero楼主2023/9/6 21:40

悬三关!

#include <bits/stdc++.h>
#define int __int128
using namespace std;
inline int read() {
    int w = 1, q = 0; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar();
    return w * q;
}
void write(int x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
int Mod, P;
string s;
stack <int> num;
stack <char> t;
int cal() {
	int y = num.top(); num.pop();
	int x = num.top(); num.pop();
	char op = t.top(); t.pop();
	if (op == '+') return (x + y) % Mod;
	if (op == '-') return (x - y + Mod) % Mod;
	if (op == '*') return x * y % Mod;
}
int level(char x) {
	if (x == '+' || x == '-') return 1;
	if (x == '*') return 2;
	return -1;
}
bool chknum(char x) {
    if (x < '0' || x > '9') return 0;
    return 1;
}
int work(int x) {
    while (!num.empty()) num.pop();
	int n = s.size(), now = 0;
	for (int i = 0; i < n; i++) {
		if (chknum(s[i])) {
		    now = (now * 10 + (s[i] - '0')) % Mod;
		    if (i == n - 1 || !chknum(s[i + 1])) num.push(now);
		    now = 0;
		}
		else if (s[i] == 'x') num.push(x);
		else if (s[i] == '(') t.push(s[i]);
		else if (s[i] == ')') {
			while (!t.empty() && t.top() != '(') num.push(cal());
			t.pop();
		}
		else {
			while (!t.empty() && level(t.top()) >= level(s[i])) num.push(cal());
			t.push(s[i]);
		}
	}
	while (!t.empty()) {
	    if (t.top() == '(') {
	        t.pop();
	        continue;
	    }
	    num.push(cal());
	}
	return num.top() % Mod;
}
signed main() {
	cin >> s;
	P = read(), Mod = read();
    int t1 = work(0), t2 = work(1);
	int k = (t2 - t1 + Mod) % Mod, b = t1;
	for (int i = 0; i < Mod; i++)
		if ((k * i % Mod + b + Mod) % Mod == P)
			return write(i), 0;
	return 0;
} 
2023/9/6 21:40
加载中...