题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962

题目大意:

有t组数据。 
给你一个n,和8位的十六进制数st,还有一张表格,里面有每一个数字的消耗。

比如"5A8BEF67"的消耗为为5 + 6 + 7 + 5 + 5 + 4 + 6 + 3 = 41。

然后让你求[n,n+st-1]区间的所有的数字的消耗之和。

解题思路:

数位DP,用solve(x)求0~x的总消耗。

lim=0xFFFFFFFF

则可以分两种情况:

①n+st-1>=lim,输出solve((st+n-1)%lim)+solve(lim-1)-solve(st-1)

②n+st-1<lim,输出solve(st+n-1)-solve(st-1)

其实这题可以算是比较明显的数位DP了,写起来也很简单,只是题目有点吓人,所以没敢写。。。

代码

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=1e2+;
const int INF=0x3f3f3f3f;
const LL lim=(1LL<<); int mp[]={,,,,,,,,,,,,,,,};
LL top[N],dp[N][N]; //dp[pos][val]其中pos表示数位,val表示当前需要消耗的电路
char str[N]; LL dfs(bool limit,int pos,int val){
if(pos==-) return val;
if(!limit&&dp[pos][val]!=-) return dp[pos][val];
int up=limit?top[pos]:;
LL ans=;
for(int i=;i<=up;i++){
ans+=dfs(limit&&i==up,pos-,val+mp[i]);
}
if(!limit) dp[pos][val]=ans;
return ans;
} LL solve(LL x){
if(x==-) return ;
int cnt=-;
while(x){
top[++cnt]=x%;
x/=;
}
while(cnt<){
top[++cnt]=;
}
return dfs(,cnt,);
} int main(){
int t;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--){
LL n,st;
scanf("%lld %llx",&n,&st);
LL ans;
if(st+n->=lim){
ans=solve((st+n-)%lim)+solve(lim-)-solve(st-);
}
else{
ans=solve(st+n-)-solve(st-);
}
printf("%lld\n",ans);
}
return ;
}

ZOJ 3962 Seven Segment Display(数位DP)的更多相关文章

  1. zoj 3962 Seven Segment Display 数位dp

    非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...

  2. ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。

    Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...

  3. ZOJ 3962 Seven Segment Display(数位DP)题解

    题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...

  4. ZOJ 3962 Seven Segment Display

    Seven Segment Display 思路: 经典数位dp 代码: #include<bits/stdc++.h> using namespace std; #define LL l ...

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

    传送门:Seven Segment Display 题意:求一个给定区间每个数字的消耗值的和: 思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的: 还有就是到最大 ...

  6. ZOJ 3494 (AC自动机+高精度数位DP)

    题目链接:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...

  7. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  8. ZOJ 2599 Graduated Lexicographical Ordering (数位DP)

    首先要吐两行槽:看到集训队论文上有这道题,由于数位DP一律写成记忆化搜索形式的强迫症,就没去看论文上的几个函数是什么……:结果被这道题虐的脑细胞死光……,最后是用随机数据对拍AC程序然后发现BUG改掉 ...

  9. ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)

    题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...

随机推荐

  1. fzyjojP2931 乱搞

    其实很简单(第二个不知是啥) 贡献独立 其实第一种就是考虑一个点在哈夫曼树上的期望深度是多少 因为精度要求较高 所以要高精小数加,高精小数除以低精整数

  2. python之旅:并发编程之多线程

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.python ...

  3. ES6学习(一)搭建环境

    作为一名后端小开发,业务工作需要将后台系统重构一番,许多同事都已经使用前后分离搭建项目,为了不拖后腿自己在家摸索ES6的新特性,真心不知道什么ES3,ES5,一上来就开始搞ES6,在此留下学习笔记,方 ...

  4. Lnmp上安装Yaf学习(二)

    上一节主要实践了在Lnmp上安装Yaf扩展,那么这一节将测试 Yaf 的一个简单demo的运行. 一.通过Lnmp 创建 vhost 文件 [root@localhost yaf-3.0.6]# ln ...

  5. 静态常量整数成员在class内部直接初始化

    #include <vector> #include <deque> #include <algorithm> #include <iostream> ...

  6. spoj 375 树链剖分 模板

    QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...

  7. Linux运维五:定时任务crond服务

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  8. python【文件操作:修改文件】

  9. js原型解释图

  10. codevs 3235 战争

    3235 战争 http://codevs.cn/problem/3235/  时间限制: 2 s  空间限制: 128000 KB   题目描述 Description 2050年,人类与外星人之间 ...