結果

問題 No.265 数学のテスト
ユーザー Kmcode1Kmcode1
提出日時 2015-08-07 23:12:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 4,046 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 111,248 KB
実行使用メモリ 22,172 KB
最終ジャッジ日時 2023-08-16 02:00:15
合計ジャッジ時間 2,641 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
22,172 KB
testcase_01 AC 26 ms
13,344 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
#include<unordered_map>
using namespace std;

int n;
int d;

string s;
#define MAX 50002
int be[MAX];
string pluss(string a, string b, bool flag = false){
	if (!flag){
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
	}
	if (a.size() > b.size()){
		swap(a, b);
	}
	string ans;
	ans.clear();
	long long int want = 0;
	for (int i = 0; i < a.size(); i++){
		long long int val = a[i] - '0';
		val += b[i] - '0';
		val += want;
		want = val / 10LL;
		val %= 10LL;
		ans.push_back(val + '0');
	}
	for (int j = a.size(); j < b.size(); j++){
		long long int val = 0;
		val += b[j] - '0';
		val += want;
		want = val / 10LL;
		val %= 10LL;
		ans.push_back(val + '0');
	}
	while (want){
		ans.push_back(want % 10 + '0');
		want /= 10;
	}
	if (!flag){
		reverse(ans.begin(), ans.end());
	}
	return ans;
}
string mult(string a, string b, bool flag = false){
	if (!flag){
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
	}
	string ans = "0";
	string pas;
	string kari;
	pas.clear();
	for (int i = 0; i < b.size(); i++){
		long long int bb = b[i] - '0';
		long long int tmp = 0;
		kari = pas;
		for (int j = 0; j < a.size(); j++){
			long long int val = (long long int)(a[j] - '0')*bb;
			val += tmp;
			tmp = val / 10LL;
			val %= 10LL;
			kari.push_back(val + '0');
		}
		while (tmp){
			kari.push_back((tmp % 10LL) + '0');
			tmp /= 10LL;
		}
		ans = pluss(ans, kari, true);
		//end
		pas.push_back('0');
	}
	if (!flag){
		reverse(ans.begin(), ans.end());
	}
	return ans;
}
string zero = "0";
vector<string> v;
string kari;
struct st{
	string ind[12];
	st(){
		for (int i = 0; i < 12; i++){
			ind[i] = zero;
		}
	}
	void mult_x(int num=1){
		for (int i = 11; i >= 0; i--){
			if (i + num < 12){
				ind[i + num] = ind[i];
			}
		}
	}
	void mult_val(string num){
		for (int i = 0; i <= 11; i++){
			if (ind[i] != zero){
				ind[i] = mult(ind[i], num);
			}
		}
	}
	st ex(){
		for (int i = 1; i <= 11; i++){
			ind[i - 1] = ind[i];
			ind[i - 1] = mult(ind[i - 1], v[i]);
		}
		return *this;
	}
	void output(){
		for (int i = 0; i <= d; i++){
			if (i){
				printf(" ");
			}
			while (ind[i].size() > 1 && ind[i][0] == '0'){
				ind[i].erase(0, 1);
			}
			printf("%s", ind[i].c_str());
		}
	}
	void merge(st a){
		for (int i = 0; i <= 11; i++){
			ind[i] = pluss(ind[i], a.ind[i]);
		}
	}
	void add(int id, string val){
		ind[id] = pluss(ind[id], val);
	}
};
inline st dfs(int l,int r){
	st R;
	int countt = 0;
	string val = "1";
	bool flag = false;
	bool fll = false;
	while (l <= r){
		if (s[l] == '}' || s[l] == '+'){
			if (flag){
				R.add(countt, val);
				val = "1";
				countt = 0;
				flag = false;
				fll = false;
			}
			l++;
			continue;
		}
		if (s[l] == '{'){
			l++;
			continue;
		}
		if (s[l] == 'd'){
			if (flag){
				exit(1);
			}
			R.merge(dfs(l + 2, be[l]).ex());
			l = be[l] + 1;
			continue;
		}
		if (s[l] == '*'){
			l++;
			continue;
		}
		if (isdigit(s[l])){
			fll = true;
			flag = true;
			val = mult(val, v[s[l] - '0']);
			l++;
			continue;
		}
		if (s[l] == 'x'){
			flag = true;
			countt++;
			l++;
			continue;
		}
	}
	if (flag){
		if (r != s.size() - 1){
			exit(1);
		}
		R.add(countt, val);
	}
	return R;
}
int main(){
	for (int i = 0; i <= 11; i++){
		kari.clear();
		kari.push_back(i + '0');
		if (i == 10){
			kari = "10";
		}
		if (i == 11){
			kari = "11";
		}
		v.push_back(kari);
	}
	cin >> n >> d;
	cin >> s;
	stack<int> st;
	for (int i = 0; i < s.size(); i++){
		if (s[i] == 'd'){
			st.push(i);
		}
		if (s[i]=='}'){
			be[st.top()] = i;
			st.pop();
		}
	}
	if (st.size()){
		return 1;
	}
	dfs(0, s.size() - 1).output();
	puts("");
	return 0;
}
0