zoj 3962 Seven Segment Display 数位dp
非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能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的更多相关文章
- 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 ...
- ZOJ 3962 Seven Segment Display(数位DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目大意: 有t组数据. 给你一个n,和8位的十六进制数s ...
- ZOJ 3962 Seven Segment Display(数位DP)题解
题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...
- ZOJ 3962 Seven Segment Display
Seven Segment Display 思路: 经典数位dp 代码: #include<bits/stdc++.h> using namespace std; #define LL l ...
- ZOJ - 3962 - Seven Segment Display-17省赛-数位DP
传送门:Seven Segment Display 题意:求一个给定区间每个数字的消耗值的和: 思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的: 还有就是到最大 ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- ZOJ 2599 Graduated Lexicographical Ordering (数位DP)
首先要吐两行槽:看到集训队论文上有这道题,由于数位DP一律写成记忆化搜索形式的强迫症,就没去看论文上的几个函数是什么……:结果被这道题虐的脑细胞死光……,最后是用随机数据对拍AC程序然后发现BUG改掉 ...
- ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)
题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...
随机推荐
- js_6_dom选择
什么是dom编程? 找 找到html中的标签,赋值给一个变量 改 通过更改这个变量动态地更改html中的内容 返回的内容为列表 如何找到那些标签? id:var find = document.get ...
- 函数式编程--lambda表达式对比匿名内部类
从前面的整理中我们看出了,Lambda表达式其实是匿名内部类的一种简化,因此它可以部分取代匿名内部类. 1,Lambda表达式与匿名内部类存在如下相同点: 1),Lambda表达式与匿名内部类一样,都 ...
- pulltorefresh 设置刷新文字提示颜色
xmlns:ptr="http://schemas.android.com/apk/res-auto" 赵泽民 2016/7/12 15:48:58 ptr:ptrHeaderS ...
- The mkdir Command
The mkdir command is is used to create new directories. A directory, referred to as a folder in some ...
- Linux 下Telnet 服务安装
Linux 下Telnet 服务安装 注:以下所有命令均在root用户下执行. 命令测试在Linxu版本6.x下完成,部分命令不适用Linux 7.0以上 1.简介 默认情况下Linux只安装了Tel ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- 《Thinking in Java》学习笔记(六)
1.Class相关知识 Class类可以理解为类的图纸,通过Class类可以分析类的结构.构建出类的实例. Class.forName("test.TestClass").newI ...
- BZOJ 1951: [Sdoi2010]古代猪文 [Lucas定理 中国剩余定理]
1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2194 Solved: 919[Submit][Status] ...
- POJ1556 The Doors [线段相交 DP]
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8334 Accepted: 3218 Descrip ...
- Go基础之--接口
定义 在Go语言中,一个类只要实现了接口要求的所有函数,我们就说这个类实现了该接口 interface类型可以定义一组方法,用来表示一个对象的行为特征,interface不能包含任何变量,接口是引用类 ...