結果

問題 No.2069 み世界数式
ユーザー ripityripity
提出日時 2023-06-22 16:44:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 918 ms / 2,000 ms
コード長 2,086 bytes
コンパイル時間 2,966 ms
コンパイル使用メモリ 209,472 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-12 11:09:46
合計ジャッジ時間 11,995 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 12 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 21 ms
4,376 KB
testcase_12 AC 382 ms
4,376 KB
testcase_13 AC 214 ms
4,376 KB
testcase_14 AC 312 ms
4,376 KB
testcase_15 AC 170 ms
4,376 KB
testcase_16 AC 329 ms
4,380 KB
testcase_17 AC 300 ms
4,380 KB
testcase_18 AC 303 ms
4,380 KB
testcase_19 AC 373 ms
4,376 KB
testcase_20 AC 310 ms
4,376 KB
testcase_21 AC 80 ms
4,380 KB
testcase_22 AC 338 ms
4,376 KB
testcase_23 AC 220 ms
4,380 KB
testcase_24 AC 305 ms
4,380 KB
testcase_25 AC 289 ms
4,376 KB
testcase_26 AC 361 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 15 ms
4,380 KB
testcase_29 AC 26 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 27 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 15 ms
4,380 KB
testcase_36 AC 12 ms
4,380 KB
testcase_37 AC 918 ms
4,376 KB
testcase_38 AC 606 ms
4,376 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 120 ms
4,384 KB
testcase_41 AC 19 ms
4,376 KB
testcase_42 AC 126 ms
4,376 KB
testcase_43 AC 93 ms
4,376 KB
testcase_44 AC 215 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int M;

vector<string> expression(string &s, int &i);
vector<string> term(string &s, int &i);
vector<string> factor(string &s, int &i);
vector<string> number(string &s, int &i);

vector<string> expression(string &s, int &i) {
	vector<string> dp = term(s, i), ep(M+1, "X"), fp;
	while( s[i] == '$' ) {
		i++;
		fill(ep.begin(), ep.end(), "X");
		fp = term(s, i);
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; y <= M-x; y++ ) {
				if( ep[x+y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x+y] = dp[x]+"+"+fp[y];
				}
			}
		}
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; y <= x; y++ ) {
				if( ep[x-y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x-y] = dp[x]+"-"+fp[y];
				}
			}
		}
		swap(dp, ep);
	}
	return dp;
}
vector<string> term(string &s, int &i) {
	vector<string> dp = factor(s, i), ep(M+1, "X"), fp;
	while( s[i] == '&' ) {
		i++;
		fill(ep.begin(), ep.end(), "X");
		fp = factor(s, i);
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; x*y <= M; y++ ) {
				if( ep[x*y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x*y] = dp[x]+"*"+fp[y];
				}
				if( x == 0 ) break;
			}
		}
		for( int x = 0; x <= M; x++ ) {
			for( int y = 1; y <= M; y++ ) {
				if( ep[x/y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x/y] = dp[x]+"/"+fp[y];
				}
			}
		}
		swap(dp, ep);
	}
	return dp;
}
vector<string> factor(string &s, int &i) {
	vector<string> dp;
	if( s[i] == '(' ) {
		i++;
		dp = expression(s, i);
		i++;
	}else {
		dp = number(s, i);
	}
	return dp;
}
vector<string> number(string& s, int &i) {
	int ret = 0;
	vector<string> dp(M+1, "X");
	while( isdigit(s[i]) ) {
		ret = 10*ret+(s[i]-'0');
		i++;
	}
	dp[ret] = "";
	return dp;
}

int main() {
	int ans;
	string expr;
	cin >> M >> ans >> expr;
	int _i = 0;
	vector<string> decode = expression(expr, _i);
	if( decode[ans] == "X" ) {
		cout << -1 << endl;
	}else {
		for( int i = 0, j = 0; i < expr.size(); i++ ) {
			if( expr[i] == '$' || expr[i] == '&' ) {
				expr[i] = decode[ans][j];
				j++;
			}
		}
		cout << expr << endl;
	}
}
0