hdu 3555

本文深入探讨了数位DP(动态规划)的模板实现方法,通过不断的实践与编写,作者对于数位DP有了更深层次的理解。文章提供了一个完整的数位DP代码示例,包括关键的数据结构定义与递归函数实现,旨在帮助读者更好地掌握数位DP的基本原理及其应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

坚持写数位dp 套模板 好像有了一些更深的理解

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <iterator>
#include <cmath>
#include <deque>
#include <stack>
#include <cctype>
#include <iomanip>
using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 10;
const int INF = 0xfffffff;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const ld PI = acos (-1.0);

#define INFL 0x7fffffffffffffffLL
#define met(a, b) memset(a, b, sizeof(a))
#define put(a) cout << setiosflags(ios::fixed) << setprecision(a)

ll dp[20][10][2], n;
int t, bit[20];
ll calc (ll n);
ll dfs (int len, int pre, bool yet, bool ismax);

int main ()
{
    met (dp, -1);
    for (cin >> t; t--; cin >> n, cout << calc(n) << endl);
    return 0;
}

ll calc (ll n)
{
    int len = 0;
    for (; n; bit[len++]=n%10, n/=10);
    return dfs (len-1, 0, false, true);
}

ll dfs (int len, int pre, bool yet, bool ismax)
{
    if (len == -1) return yet;
    if (!ismax && dp[len][pre][yet] != -1) return dp[len][pre][yet];
    ll cnt = 0;
    int maxnum = ismax ? bit[len] : 9;
    for (int i=0; i<=maxnum; i++)
        cnt += dfs (len - 1, i, yet || (pre == 4 && i == 9), ismax && i == maxnum);
    return ismax ? cnt : dp[len][pre][yet] = cnt;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值