tle,应该是 compile(char*) 出了问题
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
int bases[105];
char str[1005];
int divide;
int compile(char *str) {
int len = strlen(str), polylen = 1;
while (str[polylen] != ')') polylen++; polylen--;
divide = 0;
for (int i = polylen + 3; i < len; i++) {
divide = divide * 10 + str[i] - '0';
}
memset(bases, 0, sizeof bases);
int k = 0;
int c = 0, e = 0;
bool positive = 1;
for (int i = 1; i <= polylen;) {
c = e = 0; positive = 1;
while (str[i] == '+' || str[i] == '-') {
if (str[i] == '-') positive ^= 1; i++;
}
if (str[i] == 'n') c = 1;
else while (str[i] >= '0' && str[i] <= '9') {
c = c * 10 + str[i] - '0'; i++;
}
c = positive ? c : -c;
if (str[i] == 'n') {
i++;
if (str[i] != '^') e = 1;
else {
i++; while (str[i] >= '0' && str[i] <= '9') {
e = e * 10 + str[i] - '0'; i++;
}
}
}
bases[e] += c; k = max(k, e);
}
return k;
}
long long qkpow(long long a, int b, int m) {
long long ans = 1; a %= m;
while (b) {
if (b & 1) ans = ans * a % m;
a = a * a % m; b >>= 1;
}
return ans;
}
bool execute(int *bases, int k) {
for (int i = 1; i <= k + 1; i++) {
long long ans = 0;
for (int j = 0; j <= k; j++) {
ans = ((ans + bases[j] * qkpow(i, j, divide) % divide) % divide + divide) % divide;
}
if (ans != 0) return 0;
}
return 1;
}
signed main() {
// freopen("out.out", "w", stdout);
for (int i = 1; i; i++) {
for (int i = 0; str[i]; i++) str[i] = 0;
scanf("%s", str);
if (str[0] == '.') break;
int k = compile(str);
bool ans = execute(bases, k);
printf("Case %lld: %slways an integer\n", i, ans ? "A" : "Not a");
}
}
/*
(n^2-n)/2
(2n^3+3n^2+n)/6
(-n^14-11n+1)/3
(n^5+123n^3-123)/134
(n^4-6n^3+23n^2-18n+24)/24
(2n^3+3n^2+n)/6
.
(2n^6)/3
(n)/1
(n)/2
(2)/2
(3)/2
.
*/