nyoj-0708-ones

题意:用1,+,*,(,). 这四个符号组成表达式表达数s(0 <= s <= 10000),且1最少时1的的个数

状态转移方程: dp[i] = min(dp[i-1] + 1, dp[j] + dp[i-j]);

代码:

#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;    
} 

 

posted on 2018-04-28 20:01  kindleheart  阅读(102)  评论(0编辑  收藏  举报