結果

問題 No.2069 み世界数式
ユーザー okkuukenkenokkuukenken
提出日時 2022-08-17 09:31:50
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,579 bytes
コンパイル時間 3,971 ms
コンパイル使用メモリ 250,708 KB
実行使用メモリ 9,148 KB
最終ジャッジ日時 2023-08-16 05:55:16
合計ジャッジ時間 6,216 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,388 KB
testcase_01 AC 2 ms
5,488 KB
testcase_02 AC 2 ms
7,400 KB
testcase_03 AC 2 ms
5,404 KB
testcase_04 AC 3 ms
5,712 KB
testcase_05 AC 2 ms
5,484 KB
testcase_06 AC 3 ms
7,592 KB
testcase_07 AC 2 ms
7,400 KB
testcase_08 AC 2 ms
5,480 KB
testcase_09 AC 2 ms
7,488 KB
testcase_10 AC 2 ms
5,568 KB
testcase_11 AC 4 ms
7,632 KB
testcase_12 AC 26 ms
8,708 KB
testcase_13 AC 16 ms
8,688 KB
testcase_14 AC 21 ms
8,764 KB
testcase_15 AC 13 ms
8,920 KB
testcase_16 AC 24 ms
8,768 KB
testcase_17 AC 20 ms
8,732 KB
testcase_18 AC 22 ms
8,908 KB
testcase_19 AC 26 ms
8,724 KB
testcase_20 AC 22 ms
8,756 KB
testcase_21 AC 8 ms
8,996 KB
testcase_22 AC 26 ms
8,884 KB
testcase_23 AC 18 ms
8,784 KB
testcase_24 AC 22 ms
8,788 KB
testcase_25 AC 21 ms
8,992 KB
testcase_26 AC 25 ms
8,996 KB
testcase_27 AC 3 ms
7,656 KB
testcase_28 AC 3 ms
7,816 KB
testcase_29 AC 4 ms
7,632 KB
testcase_30 AC 2 ms
7,472 KB
testcase_31 AC 3 ms
7,524 KB
testcase_32 AC 4 ms
7,564 KB
testcase_33 AC 3 ms
7,612 KB
testcase_34 AC 3 ms
7,508 KB
testcase_35 AC 4 ms
7,648 KB
testcase_36 AC 3 ms
7,552 KB
testcase_37 AC 61 ms
9,148 KB
testcase_38 AC 37 ms
9,120 KB
testcase_39 AC 3 ms
5,752 KB
testcase_40 AC 9 ms
8,020 KB
testcase_41 AC 4 ms
7,864 KB
testcase_42 AC 9 ms
7,972 KB
testcase_43 AC 8 ms
7,952 KB
testcase_44 AC 14 ms
8,332 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<<"\r\n";
		return 0;
	}
	expr2(0,ans);
	cout<<expr<<"\r\n";
}
0