結果
| 問題 |
No.2595 Parsing Challenge
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-23 16:40:09 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 11,141 bytes |
| コンパイル時間 | 3,577 ms |
| コンパイル使用メモリ | 169,148 KB |
| 実行使用メモリ | 21,516 KB |
| 最終ジャッジ日時 | 2024-09-27 12:27:14 |
| 合計ジャッジ時間 | 32,274 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 52 TLE * 1 -- * 2 |
ソースコード
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <atcoder/convolution>
const int DIGIT = 6;
const int BASE = 1000000;
struct positive_bigint{
std::vector<int> d;
positive_bigint(){
}
positive_bigint(long long X){
while (X > 0){
d.push_back(X % BASE);
X /= BASE;
}
}
positive_bigint(std::string S){
if (S == "0"){
S = "";
}
int L = S.size();
d.resize((L + DIGIT - 1) / DIGIT, 0);
for (int i = L - 1; i >= 0; i -= 6){
for (int j = std::max(i - 5, 0); j <= i; j++){
d[i / DIGIT] *= 10;
d[i / DIGIT] += S[j] - '0';
}
}
std::reverse(d.begin(), d.end());
}
bool empty() const {
return d.empty();
}
int size() const {
return d.size();
}
int& operator [](int i){
return d[i];
}
int operator [](int i) const {
return d[i];
}
};
std::string to_string(const positive_bigint &A){
int N = A.size();
std::string ans;
for (int i = N - 1; i >= 0; i--){
std::string tmp = std::to_string(A[i]);
if (i < N - 1){
ans += std::string(DIGIT - tmp.size(), '0');
}
ans += tmp;
}
if (ans.empty()){
ans = "0";
}
return ans;
}
std::istream& operator >>(std::istream &is, positive_bigint &A){
std::string S;
is >> S;
A = positive_bigint(S);
return is;
}
std::ostream& operator <<(std::ostream &os, positive_bigint &A){
os << to_string(A);
return os;
}
int cmp(const positive_bigint &A, const positive_bigint &B){
int N = A.size();
int M = B.size();
if (N < M){
return -1;
} else if (N > M){
return 1;
} else {
for (int i = N - 1; i >= 0; i--){
if (A[i] < B[i]){
return -1;
}
if (A[i] > B[i]){
return 1;
}
}
return 0;
}
}
bool operator ==(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) == 0;
}
bool operator !=(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) != 0;
}
bool operator <(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) < 0;
}
bool operator >(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) > 0;
}
bool operator <=(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) <= 0;
}
bool operator >=(const positive_bigint &A, const positive_bigint &B){
return cmp(A, B) >= 0;
}
positive_bigint& operator +=(positive_bigint &A, const positive_bigint &B){
int N = A.size();
int M = B.size();
while (N < M){
A.d.push_back(0);
N++;
}
for (int i = 0; i < M; i++){
A[i] += B[i];
}
for (int i = 0; i < N - 1; i++){
if (A[i] >= BASE){
A[i] -= BASE;
A[i + 1]++;
}
}
if (N > 0){
if (A[N - 1] >= BASE){
A.d.push_back(1);
A[N - 1] -= BASE;
}
}
return A;
}
positive_bigint operator +(const positive_bigint &A, const positive_bigint &B){
positive_bigint A2 = A;
A2 += B;
return A2;
}
positive_bigint& operator -=(positive_bigint &A, const positive_bigint &B){
int N = A.size();
int M = B.size();
for (int i = 0; i < M; i++){
A[i] -= B[i];
}
for (int i = 0; i < N - 1; i++){
if (A[i] < 0){
A[i] += BASE;
A[i + 1]--;
}
}
while (!A.empty()){
if (A.d.back() == 0){
A.d.pop_back();
} else {
break;
}
}
return A;
}
positive_bigint operator -(const positive_bigint &A, const positive_bigint &B){
positive_bigint A2 = A;
A2 -= B;
return A2;
}
positive_bigint operator *(const positive_bigint &A, const positive_bigint &B){
if (A.empty() || B.empty()){
return 0;
}
int N = A.size();
int M = B.size();
std::vector<long long> a(N);
for (int i= 0; i < N; i++){
a[i] = A[i];
}
std::vector<long long> b(M);
for (int i = 0; i < M; i++){
b[i] = B[i];
}
std::vector<long long> C = atcoder::convolution_ll(a, b);
for (int i = 0; i < N + M - 2; i++){
C[i + 1] += C[i] / BASE;
C[i] %= BASE;
}
if (C[N + M - 2] >= BASE){
C.resize(N + M);
C[N + M - 1] += C[N + M - 2] / BASE;
C[N + M - 2] %= BASE;
}
positive_bigint ans;
ans.d.resize(C.size());
for (int i = 0; i < C.size(); i++){
ans[i] = C[i];
}
return ans;
}
positive_bigint operator *=(positive_bigint &A, const positive_bigint &B){
A = A * B;
return A;
}
struct bigint{
bool neg = false;
positive_bigint a;
bigint(){
}
bigint(long long X): neg(X < 0), a(abs(X)){
}
bigint(const positive_bigint &X, bool neg = false): neg(neg), a(X){
}
bigint(const std::string &s){
if (!s.empty()){
if (s[0] == '-'){
neg = true;
a = positive_bigint(s.substr(1, s.size() - 1));
} else {
a = positive_bigint(s);
}
}
}
bool empty() const {
return a.empty();
}
int size() const {
return a.size();
}
int& operator [](int i){
return a[i];
}
};
std::string to_string(const bigint &A){
std::string ans;
if (A.neg){
ans += '-';
}
ans += to_string(A.a);
return ans;
}
std::istream& operator >>(std::istream &is, bigint &A){
std::string S;
is >> S;
if (S != "0"){
A = bigint(S);
}
return is;
}
std::ostream& operator <<(std::ostream &os, bigint A){
os << to_string(A);
return os;
}
positive_bigint abs(const bigint &A){
return A.a;
}
int cmp(const bigint &A, const bigint &B){
if (!A.neg){
if (!B.neg){
return cmp(A.a, B.a);
} else {
return 1;
}
} else {
if (!B.neg){
return -1;
} else {
return cmp(B.a, A.a);
}
}
}
bool operator ==(const bigint &A, const bigint &B){
return cmp(A, B) == 0;
}
bool operator !=(const bigint &A, const bigint &B){
return cmp(A, B) != 0;
}
bool operator <(const bigint &A, const bigint &B){
return cmp(A, B) < 0;
}
bool operator >(const bigint &A, const bigint &B){
return cmp(A, B) > 0;
}
bool operator <=(const bigint &A, const bigint &B){
return cmp(A, B) <= 0;
}
bool operator >=(const bigint &A, const bigint &B){
return cmp(A, B) >= 0;
}
bigint operator +(const bigint &A){
return A;
}
bigint operator -(const bigint &A){
bigint A2 = A;
if (!A2.empty()){
A2.neg = !A2.neg;
}
return A2;
}
bigint& operator +=(bigint &A, const bigint &B){
if (A.neg == B.neg){
A.a += B.a;
} else {
int c = cmp(A.a, B.a);
if (c > 0){
A.a -= B.a;
} else if (c < 0){
A.a = B.a - A.a;
A.neg = !A.neg;
} else {
A = 0;
}
}
return A;
}
bigint operator +(const bigint &A, const bigint &B){
bigint A2 = A;
A2 += B;
return A2;
}
bigint& operator -=(bigint &A, const bigint &B){
if (A.neg != B.neg){
A.a += B.a;
} else {
int c = cmp(A.a, B.a);
if (c > 0){
A.a -= B.a;
} else if (c < 0){
A.a = B.a - A.a;
A.neg = !A.neg;
} else {
A = 0;
}
}
return A;
}
bigint operator -(const bigint &A, const bigint &B){
bigint A2 = A;
A2 -= B;
return A2;
}
bigint operator *=(bigint &A, const bigint &B){
if (A.empty() || B.empty()){
A = 0;
} else {
if (B.neg){
A.neg = !A.neg;
}
A.a *= B.a;
}
return A;
}
bigint operator *(const bigint &A, const bigint &B){
bigint A2 = A;
A2 *= B;
return A2;
}
using namespace std;
using stc = stack<char>;
using stb = stack<bigint *>;
using vb = vector<bigint *>;
void neg(bigint &a) {
if (a != bigint(0)) {
a.neg = !a.neg;
}
}
bool cb(bigint *a, bigint *b) {
return a->size() > b->size();
}
void proc(stc &so, stb &sn) {
char op = so.top();
so.pop();
if (op == 'n') {
neg(*sn.top());
return;
}
bigint *b = sn.top();
sn.pop();
bigint *a = sn.top();
sn.pop();
if (op == '-') {
op = '+';
neg(*b);
}
if (a->size() < b->size()) {
swap(a, b);
}
if (op == '*') {
*a *= *b;
} else {
*a += *b;
}
delete b;
sn.push(a);
}
void proc_plus(stc &so, stb &sn) {
vb tmp;
while (!so.empty() && so.top() != '(') {
if (so.top() == '*' || so.top() == 'n') {
proc(so, sn);
} else {
tmp.push_back(sn.top());
sn.pop();
if (so.top() == '-') {
neg(*tmp.back());
}
so.pop();
}
}
tmp.push_back(sn.top());
sn.pop();
sort(tmp.begin(), tmp.end(), cb);
sn.push(tmp[0]);
for (int i = 1; i < (int)tmp.size(); i++) {
so.push('+');
sn.push(tmp[i]);
}
while (!so.empty() && so.top() != '(') {
proc(so, sn);
}
}
bigint *calc_mul(const vb &a, int l, int r) {
if (r - l == 1) {
return a[l];
}
int mid = (l + r) / 2;
bigint *u = calc_mul(a, l, mid);
bigint *v = calc_mul(a, mid, r);
*u *= *v;
delete v;
return u;
}
void proc_mul(stc &so, stb &sn) {
vb tmp;
while (!so.empty() && (so.top() == '*' || so.top() == 'n')) {
if (so.top() == 'n') {
proc(so, sn);
} else {
tmp.push_back(sn.top());
sn.pop();
so.pop();
}
}
tmp.push_back(sn.top());
sn.pop();
sn.push(calc_mul(tmp, 0, tmp.size()));
}
int main() {
int n;
cin >> n;
string s;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == '-' && (s.length() == 0 || (!isdigit(s.back()) && s.back() != ')'))) {
c = 'n';
}
s += c;
}
stc so;
stb sn;
string cur;
for (char c : s) {
if (isdigit(c)) {
cur += c;
} else {
if (cur != "") {
sn.push(new bigint(cur));
cur = "";
}
if (c == '(') {
so.push(c);
} else if (c == ')') {
proc_mul(so, sn);
proc_plus(so, sn);
so.pop();
} else if (c == '*') {
so.push(c);
} else if (c == 'n') {
so.push(c);
} else {
proc_mul(so, sn);
so.push(c);
}
}
}
if (cur != "") {
sn.push(new bigint(cur));
cur = "";
}
proc_mul(so, sn);
proc_plus(so, sn);
cout << *sn.top() << endl;
return 0;
}