Seven Segment Display(数位dp)

来源:互联网 发布:淘宝仿网红家衣服质量 编辑:程序博客网 时间:2024/05/24 07:24

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.

题意:一个长度为8,有0-F(十六进制)表示的数,每个数字有一个权值,一个串的权值为8个数字的权值之和,然后给你一个数n,让你求之前的那个字符串表示的数加上n - 1之后得到一个数也用8位16进制数表示,问你这两个数中间所以的数的权值之和,如果大于ffffffff则回到00000000.

解题思路:直接数位dp,dp[i][j] 表示当前为第i位(从高位到低位),前面已累计和为j,后面的任意)的所有权值和。具体思路如代码。

#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;ll dp[20][10000];//前i个数字,已累计和为jll c[20] = {6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};ll Pow[10];//16的n次方ll inf = 0xffffffff;int num[20];//保存一个数的各个位置上的数字ll n;void init(){    memset(dp,-1,sizeof(dp));    Pow[0] = 1;    for(int i = 1; i <= 7; i++)    {        Pow[i] = 16*Pow[i - 1];    }}ll getValue(char *s){    ll sum = 0;    for(int i = 7,j = 0; i >= 0; i--,j++)    {        if(s[i] >= 'A'&&s[i] <= 'Z')        {            ll value = s[i] - 'A' + 10;            sum += value*Pow[j];        }        else        {            ll value = s[i] - '0';            sum += value*Pow[j];        }    }    return sum;}ll dfs(int loc,ll sum,int judge){    if(loc < 0) return sum;    if(!judge&&dp[loc][sum] != -1) return dp[loc][sum];    int flag;    if(judge) flag = num[loc];    else flag = 15;    ll term = 0;    for(int i = 0; i <= flag; i++)    {        term += dfs(loc - 1,sum + c[i],judge&&(i == num[loc]));    }    if(!judge)    dp[loc][sum] = term;    return term;}ll solve(ll x){    memset(num,0,sizeof(num));    int loc = 0;    while(x)    {        ll value = x%16;        num[loc++] = value;        x /= 16;    }    return dfs(7,0,1);}int main(){    int T;    scanf("%d",&T);    char s[20];    init();    while(T--)    {        scanf("%lld",&n);        scanf("%s",s);        ll Value = getValue(s);        //cout<<"value == "<<Value<<endl;        if(Value + n - 1 > inf)        {            ll res = (Value + n - 1)%(inf + 1);            printf("%lld\n",solve(inf) - (solve(Value - 1) - solve(res)));        }        else        {            ll res = Value + n - 1;            printf("%lld\n",solve(res) - solve(Value - 1));        }    }    return 0;}
0 0
原创粉丝点击