ZOJ - 3962 - Seven Segment Display-17省赛-数位DP

时间:2023-12-20 14:04:44

传送门:Seven Segment Display

题意:求一个给定区间每个数字的消耗值的和;

思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的;

    还有就是到最大的 0xffffffff 后,会从新跳到0,这里要加上两段solve(ri)+solve(most)-solve(m-1);

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
typedef long long ll;
using namespace std;
const ll most = (ll)0xffffffff;
int cost[]={,,,,,,,,,,,,,,,};
ll dp[][];
int w[];
ll dfs(ll d, ll sum, bool limit)
{
if(d < )return sum;
if(!limit && dp[d][sum] != -)return dp[d][sum];
ll res = ;
int top = limit ? w[d] : ;
for(int i=; i<=top; i++)
{
res+=dfs(d-, sum+cost[i], limit&&i==w[d]);
}
if(!limit) dp[d][sum]=res;
return res;
}
ll solve (ll t)
{
for(int i=;i<=;i++)
{
w[i]=t%;
t/=;
}
return dfs(, , true);
}
int main(){
//freopen("in","r",stdin);
int t;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
ll n, m, ri;
scanf("%lld%llx",&n,&m);
ri = m + n -;
if(ri<most)
{
printf("%lld\n",solve(ri)-solve(m-));
}
else
{
printf("%lld\n",solve(ri)+solve(most)-solve(m-));
}
}
return ;
}