結果

問題 No.2069 み世界数式
ユーザー okkuukenkenokkuukenken
提出日時 2022-08-16 15:44:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,575 bytes
コンパイル時間 3,013 ms
コンパイル使用メモリ 249,916 KB
実行使用メモリ 9,016 KB
最終ジャッジ日時 2023-08-16 05:54:36
合計ジャッジ時間 7,785 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,420 KB
testcase_01 AC 2 ms
5,452 KB
testcase_02 AC 2 ms
7,460 KB
testcase_03 AC 2 ms
5,464 KB
testcase_04 AC 2 ms
5,404 KB
testcase_05 AC 2 ms
5,428 KB
testcase_06 AC 3 ms
7,532 KB
testcase_07 AC 2 ms
7,504 KB
testcase_08 AC 2 ms
5,460 KB
testcase_09 AC 2 ms
7,752 KB
testcase_10 AC 2 ms
5,344 KB
testcase_11 AC 4 ms
7,800 KB
testcase_12 AC 28 ms
8,700 KB
testcase_13 AC 17 ms
8,700 KB
testcase_14 AC 23 ms
8,796 KB
testcase_15 AC 14 ms
8,680 KB
testcase_16 AC 25 ms
8,752 KB
testcase_17 AC 22 ms
8,672 KB
testcase_18 AC 23 ms
8,956 KB
testcase_19 AC 27 ms
8,684 KB
testcase_20 AC 24 ms
8,856 KB
testcase_21 AC 8 ms
8,764 KB
testcase_22 AC 26 ms
8,808 KB
testcase_23 AC 18 ms
8,692 KB
testcase_24 AC 23 ms
8,716 KB
testcase_25 AC 22 ms
8,864 KB
testcase_26 AC 26 ms
8,812 KB
testcase_27 AC 2 ms
7,544 KB
testcase_28 AC 3 ms
7,584 KB
testcase_29 AC 4 ms
7,632 KB
testcase_30 AC 2 ms
7,636 KB
testcase_31 AC 3 ms
7,572 KB
testcase_32 AC 4 ms
7,492 KB
testcase_33 AC 3 ms
7,580 KB
testcase_34 AC 3 ms
7,620 KB
testcase_35 AC 3 ms
7,672 KB
testcase_36 AC 3 ms
7,708 KB
testcase_37 AC 65 ms
9,016 KB
testcase_38 AC 43 ms
8,908 KB
testcase_39 AC 2 ms
5,736 KB
testcase_40 AC 10 ms
8,144 KB
testcase_41 AC 4 ms
7,952 KB
testcase_42 AC 10 ms
8,016 KB
testcase_43 AC 8 ms
7,996 KB
testcase_44 AC 16 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
int m,ans;
string expr;
bool dp[3][1001][301];
int op1[2][1001][301],op2[2][1001][301];
char op[2][1001][301];
vector<pair<int,int>>chr[3][1001];
int expr1(int n);
int term1(int n);
int factor1(int n);
void expr2(int n,int ans);
void term2(int n,int ans);
void factor2(int n,int ans);
int expr1(int n){
	auto&ch=chr[0][n];
	ch.push_back({n,-1});
	n=term1(n);
	ch.back().second=n;
	copy(dp[1][n],dp[1][n]+m+1,dp[0][n]);
	while(n<expr.size()&&expr[n]=='$'){
		int bef=n;
		n++;
		ch.push_back({n,-1});
		n=term1(n);
		ch.back().second=n;
		for(int i=0;i<=m;i++){
			for(int j=0;j<=m;j++){
				if(dp[0][bef][i]&&dp[1][n][j]&&i+j<=m){
					dp[0][n][i+j]=1;
					op1[0][n][i+j]=i;
					op2[0][n][i+j]=j;
					op[0][n][i+j]='+';
				}
				if(dp[0][bef][i]&&dp[1][n][j]&&i-j>=0){
					dp[0][n][i-j]=1;
					op1[0][n][i-j]=i;
					op2[0][n][i-j]=j;
					op[0][n][i-j]='-';
				}
			}
		}
	}
	return n;
}
int term1(int n){
	auto&ch=chr[1][n];
	ch.push_back({n,-1});
	n=factor1(n);
	ch.back().second=n;
	copy(dp[2][n],dp[2][n]+m+1,dp[1][n]);
	while(n<expr.size()&&expr[n]=='&'){
		int bef=n;
		n++;
		ch.push_back({n,-1});
		n=factor1(n);
		ch.back().second=n;
		for(int i=0;i<=m;i++){
			for(int j=0;j<=m;j++){
				if(dp[1][bef][i]&&dp[2][n][j]&&i*j<=m){
					dp[1][n][i*j]=1;
					op1[1][n][i*j]=i;
					op2[1][n][i*j]=j;
					op[1][n][i*j]='*';
				}
				if(dp[1][bef][i]&&dp[2][n][j]&&j!=0){
					dp[1][n][i/j]=1;
					op1[1][n][i/j]=i;
					op2[1][n][i/j]=j;
					op[1][n][i/j]='/';
				}
			}
		}
	}
	return n;
}
int factor1(int n){
	if(isdigit(expr[n])){
		int num=0;
		while(n<expr.size()&&isdigit(expr[n])){
			num=num*10+expr[n]-'0';
			n++;
		}
		dp[2][n][num]=1;
	}else{
		auto&ch=chr[2][n];
		n++;
		ch.push_back({n,-1});
		n=expr1(n);
		ch.back().second=n;
		n++;
		copy(dp[0][n-1],dp[0][n-1]+m+1,dp[2][n]);
	}
	return n;
}
void expr2(int n,int ans){
	auto&ch=chr[0][n];
	for(int i=ch.size()-1;i>=1;i--){
		term2(ch[i].first,op2[0][ch[i].second][ans]);
		expr[ch[i-1].second]=op[0][ch[i].second][ans];
		ans=op1[0][ch[i].second][ans];
	}
	term2(n,ans);
}
void term2(int n,int ans){
	auto&ch=chr[1][n];
	for(int i=ch.size()-1;i>=1;i--){
		factor2(ch[i].first,op2[1][ch[i].second][ans]);
		expr[ch[i-1].second]=op[1][ch[i].second][ans];
		ans=op1[1][ch[i].second][ans];
	}
	factor2(n,ans);
}
void factor2(int n,int ans){
	auto&ch=chr[2][n];
	if(!ch.empty())
		expr2(ch[0].first,ans);
}
int main(){
	cin>>m>>ans>>expr;
	expr1(0);
	if(!dp[0][expr.size()][ans]){
		cout<<-1<<endl;
		return 0;
	}
	expr2(0,ans);
	cout<<expr<<endl;
}
0