Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define sz(x) int(x.size())
- #define pb push_back
- #define FER(i,a,b) for(ll i = ll(a); i < ll(b); ++ i)
- #define IFR(i,a,b) for(ll i = ll(a); i >= ll(b); -- i)
- #define all(v) (v).begin(),(v).end()
- #define mp make_pair
- #define pb push_back
- #define ff first
- #define ss second
- #define fill(x,v) memset(x,v,sizeof(x))
- #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
- #define sqr(x) (x)*(x)
- #define bas 987625403
- typedef long double ld;
- typedef long long ll;
- typedef pair<ll, ll> ii;
- #define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
- template<typename t> void fff(const char *x, t&& val1){
- cout << x << " : " << val1 << endl;
- }
- template<typename t1, typename... t2> void fff(const char *x, t1&& val1, t2&&... val2){
- const char *xd = strchr(x+1,',');
- cout.write(x, xd-x) << " : " << val1 << " | ";
- fff(xd+1, val2...);
- }
- #define N 64
- string cad;
- int overf[N][N][N][4];
- ll dp[N][N][N][4];
- ll go(int pos, int open, int rem, int last){
- ll &ans = dp[pos][open][rem][last];
- if(ans != -1) return ans;
- if(pos == sz(cad) and open == 0 and rem == 0) return ans = 1;
- if(last == 1 and rem == 0 and open == 0 and cad[pos] == '-') return ans = 0;
- ans = 0;
- // colocar {
- if(rem){
- ans += go(pos,open+1,rem-1,2);
- if(overf[pos][open+1][rem-1][2] or ans > 1e18+1)
- overf[pos][open][rem][last] = 1;
- }
- // colocar }
- if(open){
- ans += go(pos,open-1,rem,3);
- if(overf[pos][open-1][rem][3] or ans > 1e18+1){
- overf[pos][open][rem][last] = 1;
- }
- }
- if(pos < sz(cad) and !(cad[pos] == '-' and last == 1)){
- // seguir con el caracter
- if(cad[pos] == '+'){
- ans += go(pos+1,open,rem,0);
- if(overf[pos+1][open][rem][0] or ans > 1e18+1){
- overf[pos][open][rem][last] = 1;
- }
- }
- if(cad[pos] == '-'){
- ans += go(pos+1,open,rem,1);
- if(overf[pos+1][open][rem][1] or ans > 1e18+1){
- overf[pos][open][rem][last] = 1;
- }
- }
- }
- //printf("dp[%d][%d][%d][%d] = %lld\n",pos,open,rem,last,ans);
- return ans;
- }
- void solve(int pos, int open, int rem, int last, ll k){
- if(pos == sz(cad) and open == 0 and rem == 0) return;
- // colocar el caracter
- if(pos < sz(cad) and !(cad[pos] == '-' and last == 1)){
- if(cad[pos] == '+'){
- if(k <= dp[pos+1][open][rem][0]){
- cout << "+";
- solve(pos+1,open,rem,0,k);
- return ;
- }else k -= dp[pos+1][open][rem][0];
- }
- if(cad[pos] == '-' and last != 1){
- if(k <= dp[pos+1][open][rem][1]){
- cout << "-";
- solve(pos+1,open,rem,1,k);
- return;
- }else k -= dp[pos+1][open][rem][1];
- }
- }
- // abrir
- if(rem){
- if(k <= dp[pos][open+1][rem-1][2]){
- cout << "{";
- solve(pos,open+1,rem-1,2,k);
- return;
- }else k -= dp[pos][open+1][rem-1][2];
- }
- // cerrar
- if(open){
- if(k <= dp[pos][open-1][rem][3]){
- cout << "}";
- solve(pos,open-1,rem,3,k);
- return;
- }
- }
- }
- int main(){
- fastio;
- ll k;
- cin >> cad >> k;
- int rem = 0;
- for(int i = 1; i < sz(cad); ++i){
- if(cad[i-1] == cad[i] and cad[i] == '-') rem++;
- }
- rem = (rem+1)/2;
- fill(dp,-1);
- go(0,0,rem,0);
- if(overf[0][0][rem][0]){
- cout << "Overflow" << endl;
- exit(0);
- }
- if(dp[0][0][rem][0] < k){
- cout << "Overflow" << endl;
- exit(0);
- }
- solve(0,0,rem,0,k);
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement