非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC。

思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献。如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大值之间选择,所以必须增加一个标记表示当前是否被限制。否则就可以从0~15中任选一个填充该位,这种情况就是可能被重复访问的,因为要填充剩下的位,每一位都能填0~15,所以记忆一下,当再次访问时就返回。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 8 + 5;
const LL mod = (LL)0xffffffff+1;
const int w[] = {6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};
LL dp[maxn][maxn*7];
LL a[maxn];
LL dfs(int pos, int sum, int flag) {
	if(pos < 0) return sum;
	if(!flag && dp[pos][sum] != -1) return dp[pos][sum];
	int n = flag ? a[pos] : 15;
	LL ans = 0;
	for(int i = 0; i <= n; ++i)
		ans += dfs(pos-1, sum+w[i], flag&&(i==n));
	if(!flag) dp[pos][sum] = ans;
	return ans;
}

LL solve(LL x) {
	if(x < 0) return 0;
	memset(a, 0, sizeof(a));
	int cur = 0;
	while(x > 0) {
		a[cur++] = x % 16;
		x /= 16;
	}
	return dfs(7, 0, 1);
}

LL get(char *s) {
	LL res = 0;
	LL v = 1;
	for(int i = 7; i >= 0; --i) {
		if(s[i] >= 'A' && s[i] <= 'F') {
			res += (s[i] - 'A' + 10) * v;
		}
		else res += (s[i] - '0') * v;
		v *= 16;
	}
	return res;
}

int main() {
	memset(dp, -1, sizeof(dp));
	int T;
	scanf("%d", &T);
	LL n;
	char s[10];
	while(T--) {
		scanf("%lld%s", &n, s);
		LL res = get(s);
		--n;
		if(res + n >= mod) {
			printf("%lld\n", solve((res+n)%mod) + solve(mod-1) - solve(res-1));
		}
		else printf("%lld\n", solve(res+n) - solve(res-1));
	}
	return 0;
}

如有不当之处欢迎指出!

zoj 3962 Seven Segment Display 数位dp的更多相关文章

  1. 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 ...

  2. ZOJ 3962 Seven Segment Display(数位DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目大意: 有t组数据. 给你一个n,和8位的十六进制数s ...

  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. SQL 优化经验总结34条(转)

    (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...

  2. Linux tar 解压的时候报错

    报错如下: # tar zxvf php-7.1.6.tar.gz gzip: stdin: not in gzip format tar: Child returned status 1 tar: ...

  3. yaml 格式

    来源:http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt 1.YAML是一种通用的数据串行格式 2.基本语法规则: 大小写敏感 使用缩进表示层级 ...

  4. Mysql的主从配置

    前言:这次学习分布式的思想要配置mysql的主从复制和读写分离,我在主从配置上踩到很多坑,在此演示一遍配置过程,并附上问题的说明和自己的一些见解 Mysql主从复制的原理 附上原理图: mysql的主 ...

  5. 号外号外!解决github+hexo+yilia评论插件的问题!!!

    先走一波效果图!    本人网站--http://www.wenzheng.club/ ps:效果还是不错的,支持QQ微信登录,支持表情,甚至gif动图评论! 插件采用韩国服务器的来必力评论插件--h ...

  6. 重置CentOS 7的Root密码

    centos7与centos6有很多修改,不一样了,打算写几篇关于日常用到的改动 修改root密码 centos7的用户模式跟6有所不同 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e ...

  7. 探索ArrayList自动改变size真相

    探索ArrayList自动改变size真相 ArrayList的列表对象实质上是存储在一个引用型数组里的,有人认为该数组有“自动增长机制”可以自动改变size大小.正式地说,该数组是无法改变 大小的, ...

  8. dnion的remap.conf文件

    # # URL Remapping Config File # # Using remap.config allows you to accomplish two things: # # 1) Rew ...

  9. C之多线程(例子很不错)

    1.线程 线程池是一个树状结构. 多线程解决并发问题. 一个线程内部的执行顺序是线性的.而线程之间是乱序的. 若要创建一个多线程程序,它的参数必须是空指针类型. 变色龙程序: #define _CRT ...

  10. @RequestMapping注解

    Spring MVC中用于参数绑定的注解有很多,都在org.springframework.web.bind.annotation包中,根据它们处理的request的不同内容部分可以分为四类(主要讲解 ...