比赛 暑假综合模拟2 评测结果 AAAAAAAAAA
题目名称 等价表达式 最终得分 100
用户昵称 jekyll 运行时间 0.074 s
代码语言 C++ 内存使用 0.22 MiB
提交时间 2018-08-05 19:33:24
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<cstring>
#define work(st) calc(st, 0, strlen(st) - 1, a)
using namespace std;

typedef long long ll;

const ll mod = 1e4 + 7;
const ll maxn = 55;
const ll inf = 0x3f3f3f3f;

ll pow(ll a, ll b, ll p) {
    ll ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ans;
}

ll calc(char *st, int l, int r, ll a) {
    int pri = 0, cnt = 0, pos, minpri = inf;
    for (int i = l; i <= r; i++) {
        int prio = inf + 1;
        switch (st[i]) {
            case '(': pri += 100; break;
            case ')': pri -= 100; break;
            case '^': prio = pri + 3, ++cnt; break;
            case '*': prio = pri + 2, ++cnt; break;
            case '+': prio = pri + 1, ++cnt; break;
            case '-': prio = pri + 1, ++cnt; break;
        }
        if (prio <= minpri) minpri = prio, pos = i;
    }
    if (!cnt) {
        for (int i = l; i <= r; i++)
            if (st[i] == 'a') return a;
        ll x = 0;
        for (int i = l; i <= r; i++)
            if ('0' <= st[i] && st[i] <= '9') x = (x<<1) + (x<<3) + (st[i]^48);
        return x;
    }
    switch (st[pos]) {
        case '^':
            return pow(calc(st, l, pos - 1, a), calc(st, pos + 1, r, a), mod);
        case '*':
            return (calc(st, l, pos - 1, a) * calc(st, pos + 1, r, a)) % mod;
        case '+':
            return (calc(st, l, pos - 1, a) + calc(st, pos + 1, r, a)) % mod;
        case '-':
            return (calc(st, l, pos - 1, a) - calc(st, pos + 1, r, a)) % mod;
    }
    return 0;
}

int main() {
//	freopen("testdata1.in", "r", stdin);
	freopen("equal.in", "r", stdin);
	freopen("equal.out", "w", stdout);
    int n;
    char st[55];
    gets(st);
    ll a = 12, ans = work(st);// cout << ans << endl;
    scanf("%d\n", &n);
    for (int i = 0; i < n; i++) {
        gets(st);// cout << work(st) << endl;
        if (work(st) == ans) printf("%c", 'A' + i);
    }
}