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. LeetCode(73)Set Matrix Zeroes

    题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...

  2. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  3. 洛谷 P1156 垃圾陷阱 谈论剪枝,非满分

    这是一个91分的非dp代码(是我太弱) 剪枝八五个(实际上根本没那么多,主要是上课装逼,没想到他们dp水过去了),不过我的思路与dp不同: 1.层数到达i+1,return 这个必须有 2.当前剩余生 ...

  4. jsp页面遍历输出

    <c:foreach>类似于for和foreach循环 以下是我目前见过的用法: 1.循环遍历,输出所有的元素.<c:foreach items="${list}" ...

  5. BC#76.2DZY Loves Balls

    DZY Loves Balls  Accepts: 659  Submissions: 1393  Time Limit: 4000/2000 MS (Java/Others)  Memory Lim ...

  6. BGP路由属性详解

    Weight属性:cisco私有的BGP属性参数,它只适用于一台路由器中的路由,也就是不会传递给任何其他的路由器.他的取值范围为<0-65535>,这个数越大优先级越高,默认从邻居学到的路 ...

  7. firefox自动化测试的常用插件

    1.firebug 2.firepath 3.firefinder 5.WebDriver Element Locator 提供多种语言的xpath路径

  8. IntelliJ IDEA平台下JNI编程—HelloWorld篇

    转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53906237] JNI(Java Native I ...

  9. Python基础教程笔记——第6章:抽象(函数)

    (1)计算裴波那契数列: fbis=[0,1] num=int(input("please input the number")) for i in range(num-2): f ...

  10. vs2010 静态使用 opencv 2.46 库

    下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...