题目链接: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. 3532: [Sdoi2014]Lis 最小字典序最小割

    3532: [Sdoi2014]Lis Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 865  Solved: 311[Submit][Status] ...

  2. 【bzoj2795】【Poi2012】A Horrible Poem

    题解: 询问区间的整循环节 设区间长度为$n$ 如果有循环节长为$x$和$y$,那由斐蜀定理得$gcd(x,y)$也一定为一个循环节: 假设最小的循环节长为$mn$,那么对于任何循环节长$x$,一定$ ...

  3. html概括

    --引入 什么是html? HTML(Hyper Text Markup Language)超文本标记语言. -->那么第一个问题----什么是标记语言呢? 标记语言就是让文本展示更丰富,更美观 ...

  4. python3中SSLError错误处理

    在deepin中安装了python3.6,安装路径为/usr/local/python36,然后通过deepin自带的python2.7的pip安装了virtualenv: sudo pip inst ...

  5. ROI POOLING 介绍

    转自 https://blog.csdn.net/gbyy42299/article/details/80352418 Faster rcnn的整体构架: 训练的大致过程: 1.图片先缩放到MxN的尺 ...

  6. aspx <%= %> 绑定变量问题及解决方法

    aspx页面,前台页面需要用到后台变量字段的时候我们一般使用  <%= 变量  %>,多数情况下使用正常. 但是有的时候就会遇到变量解析被编码的情况,特别是是在head当中css引用lin ...

  7. RACCommand

    RACCommand是ReactiveCocoa中用于表示UI操作的一个类.它包含一个代表了UI操作的结果的信号以及标识操作当前是否被执行的一个状态. 1.创建新的RACCommand self.ex ...

  8. Linux下U盘、SD卡挂载与卸载

    1.手动挂载/卸载U盘.SD卡 对于ARM Linux来说,第一次使用U盘或SD时,U盘这个文件目录是不能直接进入的,我们需要对其进行挂载,然后再接下来的使用中就可以直接进行使用了.通过再网上查资料, ...

  9. Shell记录-Shell脚本基础(一)

    Shell 注释: 你可以把注释,在你的脚本如下: #!/bin/bash # Author : Zara Ali # Copyright (c) Tutorialsyiibai.com # Scri ...

  10. 用Python来进行词频统计

    # 把语料中的单词全部抽取出来, 转成小写, 并且去除单词中间的特殊符号 def words(text): return re.findall('[a-z]+', text.lower()) def ...