Seven Segment Display


Time Limit: 1 Second      Memory Limit: 65536 KB

A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays. Seven segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.

Edward, a student in Marjar University, is studying the course "Logic and Computer Design Fundamentals" this semester. He bought an eight-digit seven segment display component to make a hexadecimal counter for his course project.

In order to display a hexadecimal number, the seven segment display component needs to consume some electrical energy. The total energy cost for display a hexadecimal number on the component is the sum of the energy cost for displaying each digit of the number. Edward found the following table on the Internet, which describes the energy cost for display each kind of digit.

Digit Energy Cost
(units/s)
0 6
1 2
2 5
3 5
4 4
5 5
6 6
7 3
Digit Energy Cost
(units/s)
8 7
9 6
A 6
B 5
C 4
D 5
E 5
F 4

For example, in order to display the hexadecimal number "5A8BEF67" on the component for one second, 5 + 6 + 7 + 5 + 5 + 4 + 6 + 3 = 41 units of energy will be consumed.

Edward's hexadecimal counter works as follows:

  • The counter will only work for n seconds. After n seconds the counter will stop displaying.
  • At the beginning of the 1st second, the counter will begin to display a previously configured eight-digit hexadecimal number m.
  • At the end of the i-th second (1 ≤ i < n), the number displayed will be increased by 1. If the number displayed will be larger than the hexadecimal number "FFFFFFFF" after increasing, the counter will set the number to 0 and continue displaying.

Given n and m, Edward is interested in the total units of energy consumed by the seven segment display component. Can you help him by working out this problem?

Input

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 105), indicating the number of test cases. For each test case:

The first and only line contains an integer n (1 ≤ n ≤ 109) and a capitalized eight-digit hexadecimal number m (00000000 ≤ m ≤ FFFFFFFF), their meanings are described above.

We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output

For each test case output one line, indicating the total units of energy consumed by the eight-digit seven segment display component.

Sample Input

3
5 89ABCDEF
3 FFFFFFFF
7 00000000

Sample Output

208
124
327

Hint

For the first test case, the counter will display 5 hexadecimal numbers (89ABCDEF, 89ABCDF0, 89ABCDF1, 89ABCDF2, 89ABCDF3) in 5 seconds. The total units of energy cost is (7 + 6 + 6 + 5 + 4 + 5 + 5 + 4) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 6) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 2) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 5) + (7 + 6 + 6 + 5 + 4 + 5 + 4 + 5) = 208.

For the second test case, the counter will display 3 hexadecimal numbers (FFFFFFFF, 00000000, 00000001) in 3 seconds. The total units of energy cost is (4 + 4 + 4 + 4 + 4 + 4 + 4 + 4) + (6 + 6 + 6 + 6 + 6 + 6 + 6 + 6) + (6 + 6 + 6 + 6 + 6 + 6 + 6 + 2) = 124.

数位DP硬刚

其实就是暴力

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e3+, mod = 1e9+, inf = 2e9; LL d[N],dp[][],vis[][],dp1[][];
LL a[] = {,,,,,,,,,,,,,,,};
int id(char ch) {
if(ch >= '' && ch <= '') return ch - '';
else return ch - 'A' + ;
}
LL quick_pow(LL x,LL p) {
if(p<=) return ;
LL ans = quick_pow(x,p>>);
ans = ans*ans;
if(p & ) ans = ans*x;
return ans;
}
LL dfs(int dep,int f,LL x) {
if(dep<) return ;
if(f && vis[dep][f]) return dp[dep][f];
if(f) {
LL& ret = dp[dep][f];
vis[dep][f] = ;
ret += *quick_pow(,dep) + 1LL**dfs(dep-,f,quick_pow(,dep));
return ret;
}
else
{
LL ret = ;
for(int i = ; i <= d[dep]; ++i) {
LL tmp;
if(i < d[dep]) tmp = quick_pow(,dep);
else tmp = x%quick_pow(,dep)+;
ret += (tmp)*a[i] + dfs(dep-,i<d[dep],tmp-);
}
return ret;
}
}
LL solve(LL x) {
if(x < ) return ;
int len = ;
LL tmp = x;
for(LL i = ; i <= ; ++i) {
d[len++] = x%;
x/=;
}
return dfs(len-,,tmp);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
char s[];
scanf("%d%s",&n,s);
int L = strlen(s);
LL l = ,tmp = ;
for(int i = L-; i >= ; --i) {
l += id(s[i])*tmp;
tmp*=;
}
LL r = l+n-,ans = ;
if(r > ) {
ans = solve() - solve(l-);
l = ;
r = r--;
}
printf("%lld\n",ans + solve(r) - solve(l-));
}
return ;
} /*
2
3 FFFFFFFF
7 00000000
*/

ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp的更多相关文章

  1. The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分

    Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence S = { ...

  2. The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - C 暴力 STL

    What Kind of Friends Are You? Time Limit: 1 Second      Memory Limit: 65536 KB Japari Park is a larg ...

  3. ZOJ 4033 CONTINUE...?(The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    #include <iostream> #include <algorithm> using namespace std; ; int a[maxn]; int main(){ ...

  4. ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )

    对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...

  6. ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA

    ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  8. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  9. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

随机推荐

  1. 关于linux安装kettle的总结

    一.部署准备 1.1 JDK安装配置 命令行键入“cd /etc”进入etc目录 命令行键入“vi profile”打开profile文件 敲击键盘ctrl+F到文件末尾 在末尾处,即第一个~的地方, ...

  2. Activiti数据表

    --1:资源库流程规则表SELECT * FROM JEESITE.act_re_deployment --部署信息表SELECT * FROM JEESITE.act_re_model --流程设计 ...

  3. ASP.NET MVC如何在页面加载完成后ajax异步刷新

    背景:之前已写过两篇有关Ajax的随笔,这一篇是单独针对在页面加载完成的Ajax操作.比如说打开学生列表页面,先加载页面,然后以Ajax的方式,从数据库中检索相应的学生信息,给浏览者更好的体验. 简单 ...

  4. jquery取当前节点的上级ID

  5. [Kubernetes]kubectl命令补全出错

    在kubernetes集群中,命令补全能够省很多事,但是这两天就很奇怪 kubectl get pod -n kube+tab键自动补全Namespace的时候出现错误 kubectl get pod ...

  6. POJ 2112: Optimal Milking【二分,网络流】

    题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机 ...

  7. 移动端:UI图px单位转换rem单位的计算方法

    简单说一下 em em 单位是相对于父元素字体大小来去定的.比方说: font-size:12px; 元素宽度是2em; 那么实际的宽度是 24px.(具体为什么,可以去查询资料,今天主讲rem) 简 ...

  8. 聪明的猴子(BZOJ 2429)

    题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水面上.猴子不会游泳,但跳跃能力比较强,它们仍然可以 ...

  9. 最小生成树求法 Prim + Kruskal

    prim算法的思路 和dijkstra是一样的 每次选取一个最近的点 然后去向新的节点扩张 注意这里的扩张 不再是 以前求最短路时候的到新的节点的最短距离 而是因为要生成一棵树 所以是要连一根最短的连 ...

  10. ibatis中的xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-/ ...