예제 #1
0
 int racecar(int t) {
     if (dp[t] > 0) return dp[t];
     int n = floor(log2(t)) + 1, res;
     if (1 << n == t + 1) dp[t] = n;
     else {
         dp[t] = racecar((1 << n) - 1 - t) + n + 1;
         for (int m = 0; m < n - 1; ++m)
             dp[t] = min(dp[t], racecar(t - (1 << (n - 1)) + (1 << m)) + n + m + 1);
     }
     return dp[t];
 }
 int racecar(int target) {
     if(dp[target] > 0){
         return dp[target];
     }
     
     int n = floor(log2(target)) + 1;
     if((1<<n) - 1 == target){
         dp[target] = n;
     } else {
         dp[target] = racecar( (1<<n) - 1 - target ) + n + 1;
         for(int m = 0; m < n - 1; ++ m){
             dp[target] = min(dp[target], racecar( target - ( 1<<(n - 1) ) + (1<<m) ) + n + m + 1);
         }
     }
     
     return dp[target];
 }