题目链接: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. spoj 375 树链剖分 模板

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

  2. Docker图形界面管理之Shipyard

    一.介绍 Shipyard基于Docker API实现的容器图形管理系统,支持container.images.engine.cluster等功能,可满足我们基本的容器部署需求. 可堆栈的Docker ...

  3. Java质数求解

    质数概念 质数,又称素数,指在一个大于1的自然数中,除了1和此整数自身外,无法被其他自然数整除的数(也可定义为只有1和本身两个因数的数).最小的素数是2,也是素数中唯一的偶数:其他素数都是奇数.质数有 ...

  4. 使用Java API的5个技巧

    原文出处:CSDN邓帅 本文介绍了一些关于Java API安全和性能方面的简单易用的技巧,其中包括保证API Key安全和开发Web Service方面中在框架方面选择的一些建议. 程序员都喜欢使用A ...

  5. MVVM实战

    1.层次依赖 - (UIViewController *)createInitialViewController { self.viewModelServices = [RWTViewModelSer ...

  6. zkw费用流模板

    理论:http://www.cnblogs.com/acha/p/6735037.html #include<cstdio> #include<cstring> #includ ...

  7. MongoDB - MongoDB CRUD Operations, Bulk Write Operations

    Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...

  8. Linux下编译Phantomjs

    1.安装依赖的库 <pre> sudo apt-get install g++ flex bison gperf ruby perl \ libsqlite3-dev libfontcon ...

  9. Docker 初相见

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ...

  10. SearchSploit

    在我们的GitHub上的Exploit Database存储库中包含一个名为"searchsploit"的Exploit-DB的命令行搜索工具,该工具还允许您在任何地方随身携带一个 ...