ZOJ 3962 Seven Segment Display

Seven Segment Display

思路:

经典数位dp

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

int cost[16]={6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};
int d[10];
LL dp[10][10000];
LL dfs(int pos,int sum,bool limit){
    if(pos<0)return sum;
    if(!limit&&~dp[pos][sum])return dp[pos][sum];
    int top=limit?d[pos]:15;
    LL t=0;
    for(int i=0;i<=top;i++){
        t+=dfs(pos-1,sum+cost[i],limit&&i==d[pos]);
    }
    if(!limit)dp[pos][sum]=t;
    return t;
}
LL solve(LL n){
    for(int i=0;i<8;i++){
        d[i]=n%16;
        n/=16;
    }
    return dfs(7,0,true);
}
int main(){
    LL n,m,l,r;
    int T;
    LL s=4294967295;
    mem(dp,-1);
    scanf("%d",&T);
    while(T--){
        scanf("%lld%llX",&n,&m);
        l=m;
        r=m+n-1;
        if(r>s){
            printf("%lld
",solve(r-s-1)+solve(s)-solve(l-1));
        }
        else{
            printf("%lld
",solve(r)-solve(l-1));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/8544025.html