Advertisement
Guest User

Too Many Hyphens

a guest
Jan 23rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define sz(x) int(x.size())
  4. #define pb push_back
  5. #define FER(i,a,b) for(ll i = ll(a); i < ll(b); ++ i)
  6. #define IFR(i,a,b) for(ll i = ll(a); i >= ll(b); -- i)
  7. #define all(v) (v).begin(),(v).end()
  8. #define mp make_pair
  9. #define pb push_back
  10. #define ff first
  11. #define ss second
  12. #define fill(x,v) memset(x,v,sizeof(x))
  13. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  14. #define sqr(x) (x)*(x)
  15. #define bas 987625403
  16. typedef long double ld;
  17. typedef long long ll;
  18. typedef pair<ll, ll> ii;
  19. #define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
  20.  
  21. template<typename t> void fff(const char *x, t&& val1){
  22.     cout << x << " : " << val1 << endl;
  23. }
  24.  
  25. template<typename t1, typename... t2> void fff(const char *x, t1&& val1, t2&&... val2){
  26.     const char *xd = strchr(x+1,',');
  27.     cout.write(x, xd-x) << " : " << val1 << " | ";
  28.     fff(xd+1, val2...);
  29. }
  30.  
  31. #define N 64
  32.  
  33. string cad;
  34. int overf[N][N][N][4];
  35. ll dp[N][N][N][4];
  36.  
  37. ll go(int pos, int open, int rem, int last){
  38.     ll &ans = dp[pos][open][rem][last];
  39.     if(ans != -1) return ans;
  40.     if(pos == sz(cad) and open == 0 and rem == 0) return ans = 1;
  41.     if(last == 1 and rem == 0 and open == 0 and cad[pos] == '-') return ans = 0;
  42.     ans = 0;
  43.     // colocar {
  44.     if(rem){
  45.         ans += go(pos,open+1,rem-1,2);
  46.         if(overf[pos][open+1][rem-1][2] or ans > 1e18+1)
  47.             overf[pos][open][rem][last] = 1;
  48.     }
  49.     // colocar }
  50.     if(open){
  51.         ans += go(pos,open-1,rem,3);
  52.         if(overf[pos][open-1][rem][3] or ans > 1e18+1){
  53.             overf[pos][open][rem][last] = 1;
  54.         }
  55.     }
  56.     if(pos < sz(cad) and !(cad[pos] == '-' and last == 1)){
  57.     // seguir con el caracter
  58.         if(cad[pos] == '+'){
  59.             ans += go(pos+1,open,rem,0);
  60.             if(overf[pos+1][open][rem][0] or ans > 1e18+1){
  61.                 overf[pos][open][rem][last] = 1;
  62.             }
  63.         }
  64.         if(cad[pos] == '-'){
  65.             ans += go(pos+1,open,rem,1);
  66.             if(overf[pos+1][open][rem][1] or ans > 1e18+1){
  67.                 overf[pos][open][rem][last] = 1;
  68.             }
  69.         }
  70.     }
  71.     //printf("dp[%d][%d][%d][%d] = %lld\n",pos,open,rem,last,ans);
  72.     return ans;
  73. }
  74.  
  75. void solve(int pos, int open, int rem, int last, ll k){
  76.     if(pos == sz(cad) and open == 0 and rem == 0) return;
  77.     // colocar el caracter
  78.     if(pos < sz(cad) and !(cad[pos] == '-' and last == 1)){
  79.         if(cad[pos] == '+'){
  80.             if(k <= dp[pos+1][open][rem][0]){
  81.                 cout << "+";
  82.                 solve(pos+1,open,rem,0,k);
  83.                 return ;
  84.             }else k -= dp[pos+1][open][rem][0];
  85.         }
  86.         if(cad[pos] == '-' and last != 1){
  87.             if(k <= dp[pos+1][open][rem][1]){
  88.                 cout << "-";
  89.                 solve(pos+1,open,rem,1,k);
  90.                 return;
  91.             }else k -= dp[pos+1][open][rem][1];
  92.         }
  93.     }
  94.     // abrir
  95.     if(rem){
  96.         if(k <= dp[pos][open+1][rem-1][2]){
  97.             cout << "{";
  98.             solve(pos,open+1,rem-1,2,k);
  99.             return;
  100.         }else k -= dp[pos][open+1][rem-1][2];
  101.     }
  102.     // cerrar
  103.     if(open){
  104.         if(k <= dp[pos][open-1][rem][3]){
  105.             cout << "}";
  106.             solve(pos,open-1,rem,3,k);
  107.             return;
  108.         }
  109.     }
  110. }
  111.  
  112. int main(){
  113.    
  114.     fastio;
  115.     ll k;
  116.     cin >> cad >> k;
  117.  
  118.     int rem = 0;
  119.     for(int i = 1; i < sz(cad); ++i){
  120.         if(cad[i-1] == cad[i] and cad[i] == '-') rem++;
  121.     }
  122.  
  123.     rem = (rem+1)/2;
  124.  
  125.     fill(dp,-1);
  126.     go(0,0,rem,0);
  127.     if(overf[0][0][rem][0]){
  128.         cout << "Overflow" << endl;
  129.         exit(0);
  130.     }
  131.  
  132.     if(dp[0][0][rem][0] < k){
  133.         cout << "Overflow" << endl;
  134.         exit(0);
  135.     }
  136.  
  137.     solve(0,0,rem,0,k);
  138.     cout << endl;
  139.    
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement