ZOJ 3962 Seven Segment Display【数位DP*好题】

ZOJ 3962 Seven Segment Display
在这里插入图片描述
Sample Input
3
5 89ABCDEF
3 FFFFFFFF
7 00000000
Sample Output
208
124
327
Hint
For the first test case, the counter will display 5 hexadecimal numbers (89ABCDEF, 89ABCDF0, 89ABCDF1, 89ABCDF2, 89ABCDF3) in 5 seconds. The total units of energy cost is (7 + 6 + 6 + 5 + 4 + 5 + 5 + 4) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 6) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 2) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 5) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 5) = 208.
For the second test case, the counter will display 3 hexadecimal numbers (FFFFFFFF, 00000000, 00000001) in 3 seconds. The total units of energy cost is (4 + 4 + 4 + 4 + 4 + 4 + 4 + 4) + (6 + 6 + 6 + 6 + 6 + 6 + 6 + 6) + (6 + 6 + 6 + 6 + 6 + 6 + 6 + 2) = 124.

题意:给你一个n和一个16进制下的八位数码x(无论何种情况都为八位),求 [ x , x + n − 1 ] [x,x + n - 1] [x,x+n1]下每位的权值(详见样例);
分析:【经典数位DP】
这个16进制快把我绕晕了,复杂的不是数位DP那块,而是每一位公式的推导,同时也要求对数位DP理解的程度不能仅仅停留在模板阶段。
d p [ p o s ] [ x ] [ s t a ] : 前 p o s 位 且 当 前 位 为 x 时 , s t a 下 的 权 值 dp[pos][x][sta]:前pos位且当前位为x时,sta下的权值 dp[pos][x][sta]:posxsta;
不同于之前的dp,这个你要考虑 i − 1 i-1 i1位对 i i i位的贡献:
1:if(limit && i == up) ,当前位应该乘以真实值;(调了好久的bug,没有考虑 i = = u p i==up i==up)
2:else ,当前位应该乘以上限值;

#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define met(s) memset(s, 0, sizeof(s))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
template <class T> inline void scan_d(T &ret) {
char c; ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9') {
ret = ret * 10 + (c - '0'), c = getchar();}}
typedef long long LL;
typedef unsigned long long ull;
typedef pair<LL, LL> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 1e5 + 10;
LL mp[] = {6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};
LL a[15], b[15], f[15], c[20];
LL dp[10][30][2];
char str[10];

inline LL dfs(LL pos, LL x, LL limit, LL sta) {
    if(pos < 0) return 0;
    if(dp[pos][x][sta] >= 0 && !limit) return dp[pos][x][sta];
    LL up = limit ? a[pos] : 15;
    LL ans = 0, cnt;
    for(LL i = 0; i <= up; ++i) {
        if(limit && i == up) cnt = b[pos]; //这个稍微有点绕
        else cnt = f[pos];
        ans += dfs(pos - 1, i, limit && i == up, sta && i == 0) + mp[i] * cnt;
        //printf("## %lld %lld %lld %lld\n", i, x, ans, dfs(pos - 1, i, limit && i == up, sta && i == 0));
    }
    //printf("$$ %lld %lld %lld %lld\n", pos, x, ans, up);
    if(!limit) dp[pos][x][sta] = ans;
    return ans;
}

inline LL so(LL x) {
    if(x < 0) return 0;
    LL p = 0;
    while(x) {
        a[p++] = x % 16;
        x /= 16;
    }
    b[1] = a[0] + 1; b[0] = f[0] = 1ll;
    for(LL i = 1; i <= 10; ++i) f[i] = f[i - 1] * 16;
    for(LL i = 2; i <= 7; ++i)  b[i] = a[i - 1] * f[i - 1] + b[i - 1];
    return dfs(7, 0, 1ll, 1ll);
}

void gt(LL n) {
    LL cnt1, cnt2;
    LL len = strlen(str); LL x = 0;
    for(LL i = 0; i < len; ++i) {
        if(str[i] <= '9') x = x * 16 + str[i] - '0';
        else x = x * 16 + str[i] - 'A' + 10;
    }
    memset(a, 0, sizeof(a));
    cnt1 = so(x - 1);
    memset(a, 0, sizeof(a));
    LL y = x + n - 1;
    if(y > 4294967295) { //4294967295这个数字为FFFFFFFF情况下的值
        LL cnt = so(y - 4294967295 - 1);
        memset(a, 0, sizeof(a));
        LL res = so(4294967295);
        cnt2 = cnt + res - cnt1;
    } else cnt2 = so(x + n - 1) - cnt1;
    printf("%lld\n", cnt2);
}

int main() {
    memset(dp, -1, sizeof(dp));
    int T; LL n; scanf("%d", &T);
    while(T--) {
        scanf("%lld %s", &n, str);
        gt(n);
    }
    return 0;
}
/*
2 00000011
7 00000002
5 89ABCDEF
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值