nyoj-0708-ones

nyoj-0708-ones

The meaning of the question: use 1, +, *, (,). These four symbols form an expression to express the number s (0 <= s <= 10000), and the number of 1s when 1 is the least

State transition equation: dp[i] = min(dp[i-1] + 1, dp[j] + dp[ij]);

Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 100001;
int dp[N];
int main() {
    fill(dp, dp + N, 10000);
    dp[0] = 0; dp[1] = 1;
    for(int i = 2; i <= 10000; i++) {
        int t = (int)sqrt(i);
        dp[i] = dp[i-1] + 1;
        for(int j = i - 1; j >= t; j--) {
            if(i % j == 0) dp[i] = min(dp[i], dp[j] + dp[i/j]);    
        }
    }
    int n;
    while(scanf("%d", &n) != EOF) {
        printf("%d\n", dp[n]);
    }
    return 0;    
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325013672&siteId=291194637