您的位置:首页 > 其它

HDU3555 要49 数位DP

2016-02-26 16:14 274 查看
题目大意:数字里面含有连续的49的

思路:

可以先预处理,也可以直接dfs搜索一下,但是要注意有记忆化数组,不然爆栈了

具体的看一下这个人的详细的解释吧,感觉这个模板很好,适合新手看(强调一下,本人也是新手^0^,看了以后感觉很不错,尤其是结合了一下题目)

给出我的代码吧:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long ll;
const ll inf = pow(2, 63) - 1;
ll dp[25][15][2];
ll bit[25];
ll n;
int cnt = 0;

ll dfs(ll pos, ll pre, ll status, ll limit){
if (pos < 1) return status;
if (!limit && dp[pos][pre][status] != -1)
return dp[pos][pre][status];
int end = limit ? bit[pos] : 9;
ll res = 0;
for (int i = 0; i <= end; i++){
res += dfs(pos - 1, i, status || (pre == 4 && i == 9), limit && (i == end));
}
if (!limit) dp[pos][pre][status] = res;
return res;
}

int main(){
int t;
scanf("%d", &t);
while (t--){
memset(dp, -1, sizeof(dp));
memset(bit, 0, sizeof(bit));
scanf("%I64d", &n);
cnt = 0;
while (n){
bit[++cnt] = n % 10;
n /= 10;
}
printf("%I64d\n", dfs(cnt, 0, 0, 1));
}
return 0;
}
</cmath></cstring></algorithm></cstdio>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: