ZOJ 3962 Seven Segment Display (数位 DP)

来源:互联网 发布:购买空间域名要多少钱 编辑:程序博客网 时间:2024/05/22 16:06

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.

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?

将区间 [m, m+n-1] 中的所有数字用 16 进制表示输出依次输出到电子显示屏上的耗电量(其中每个十六进制数字的耗电量已知)。

解题思路

数位 dp

代码

#include<bits/stdc++.h>using namespace std;const unsigned int MAX = (1ll<<32) - 1;unsigned int m;int ec[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6, 6, 5, 4, 5, 5, 4};int n, num[10];long long dp[10][17];long long cnt(int pos){    long long ret = 0;    for(int i=pos-1;i>=0;i--)        ret *= 16,  ret += num[i];    return ret+1;}long long dfs(int pos, int limit){    if(pos < 0) return 0;    if(limit == 16 && dp[pos][limit]) return dp[pos][limit];    long long ret = 0;    for(int i=0;i<limit;i++)        ret += dfs(pos-1, 16),  ret += ec[i] * (1ll<<(pos*4));    if(limit < 16)        ret += dfs(pos-1, num[pos-1]),  ret += ec[limit] * cnt(pos);    return dp[pos][limit] = ret;}long long solve(long long limit){    memset(num, 0, sizeof(num));    for(int i=0;limit;i++)        num[i] = limit % 16,    limit /= 16;    long long ans = dfs(7, num[7]);    return ans;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d %X",&n,&m);        if(m+n-1 < m)            printf("%lld\n", solve(MAX) - solve(m-1) + solve(m+n-1));        else            printf("%lld\n", solve(m+n-1) - solve(m-1));    }}
0 0
原创粉丝点击