2020牛客多校九A Groundhog and 2-Power Representation

/*
find函数返回括号里的值,经过观察可以发现,2(1)也就是2,和最内层括号里的数都能通过
条件s[idx+1]!='('判断出来
*/

#include<bits/stdc++.h>
using namespace std;
const int N = 100005;


vector<int> a, b;
//加法,低位在左边
vector<int> add(vector<int>& a, vector<int>& b)
{
	if (a.size() < b.size())
		return add(b, a);
	vector<int> c;
	int t = 0;
	for (int i = 0; i < a.size(); i++)
	{
		t += a[i];
		if (i < b.size())
			t += b[i];
		c.push_back(t % 10);
		t /= 10;
	}
	if (t)
		c.push_back(t);
	return c;
}


//减法,返回值低位在左边,使用减法时保证a>b
bool cmp(vector<int>& a, vector<int>& b)
{
	if (a.size() != b.size())
		return a.size() > b.size();
	for (int i = a.size() - 1; i >= 0; i--)
		if (a[i] != b[i])
			return a[i] > b[i];
	return true;
}
vector<int> sub(vector<int>& a, vector<int>& b)
{
	vector<int> c;
	for (int i = 0, t = 0; i < a.size(); i++)
	{
		t = a[i] - t;
		if (i < b.size())
			t -= b[i];
		c.push_back((t + 10) % 10);
		t = (t >= 0 ? 0 : 1);
	}
	while (c.size() > 1 && c.back() == 0)
		c.pop_back();
	return c;
}

//乘法,大数乘以整型数,低位在左边
vector<int> mul(vector<int>& A, int b)
{
	vector<int> c;
	for (int i = 0, t = 0; i < A.size() || t; i++)
	{
		if (i < A.size())
			t += A[i] * b;
		c.push_back(t % 10);
		t /= 10;
	}

	while (c.size() > 1 && c.back() == 0)
		c.pop_back();
	return c;
}

//除法,低位在左边
vector<int> div(vector<int>& a, int b, int& r)
{
	vector<int> c;
	for (int i = a.size() - 1; i >= 0; i--)
	{
		r = r * 10 + a[i];
		c.push_back(r / b);
		r %= b;
	}
	reverse(c.begin(), c.end());
	while (c.size() > 1 && c.back() == 0)
		c.pop_back();
	return c;
}


int idx = 0;
string s;
int find()
{
	int res = 0;
	while (1)
	{
		if (s[idx] == '(')
		{
			idx++;
			int m = find();
			int t = 1;
			res += (t<<=m);
		}
		
		if (isdigit(s[idx]) && s[idx + 1] != '(')
			res += s[idx] - '0';
	
		if (s[idx] == ')')
		{
			idx++;
			break;
		}
		idx++;
	}

	return res;
}

vector<int> bpow(int m)
{
	vector<int> res;
	res.push_back(1);

	while (m--)
		res = mul(res, 2);

	return res;
}

int main()
{
	
	cin >> s;
	int len = s.length();
	s += 'a';
	vector<int> ans;
	vector<int> tmp;
	tmp.push_back(2);

	while (idx < len)
	{
		if (s[idx] == '(')
		{
			idx++;
			int m = find();
			auto b = bpow(m);
			ans = add(ans, b);
		}
		if (s[idx]=='2' && s[idx + 1] != '(')
			ans = add(ans, tmp);
		idx++;
	}

	for (int i = ans.size() - 1; i >= 0; i--)
		cout << ans[i];
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值