ZOJ Problem Set

来源:互联网 发布:我打软件技巧 编辑:程序博客网 时间:2024/06/08 04:47

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962

Seven Segment Display

Time Limit: 2 Seconds      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.

DigitEnergy Cost
(units/s)0612253544556673DigitEnergy Cost
(units/s)8796A6B5C4D5E5F4

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 numberm.
  • 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 numberm (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

35 89ABCDEF3 FFFFFFFF7 00000000

Sample Output

208124327

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.


Author: ZHOU, Jiayu
Source: The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple


题意:给定一个n,代表区间长度,给一个16进制数代表区间左端。给定0~F的权值,求区间的值


传统方法:数位dp,可是我太菜,不会

一开始做过类似题目

【L,R】区间内有几个0

题目链接

https://vjudge.net/contest/145556#problem/E

https://vjudge.net/contest/145556#problem/F

我的思路都是[ 0 , x ]统计各个位有多少个数字


统计个位有多少个0,十位(16进制,暂且称为十位)有多少个0,。。。。

统计个位有多少个1,十位有多少个1,。。。。

统计个位有多少个2,十位有多少个2,。。。。

。。。

统计个位有多少个F,十位有多少个F,。。。。


再用[0,R]的结果减去 [0,L-1]的结果就是答案


我们知道每隔16,在个位就会有一个0,1,2 ~ ,F

1~10 , 11~20 , 21~30.......(16)

令 x= n / 16  , y= n % 16

有      x   个 0,

有      x   个 1 ,2 ,3 ,。。。。。F

还有 一个 1,2,3   ,。。。。y




我们知道每隔16*16,在十位位就会有16个0,1,2 ~ ,F

1~100  , 101 ~200  ,  201~300 ........(16)

令 x= n / 16  , y= n % 16

////////////////////////////////////////////////////////////////////////////////////////////////////数字0

首先有      16*(x-1)   个 0

如果 n=106(16),x=1,y=6

除了(0*16)个 0 ,还有 7个0      ps :100,101,102,103,104,105,106

如果 n=116(16),x=1,y=16

除了(0*16)个 0 ,还有 16个0    ps :100,101,102,103,104,105,106,107,108,109,10A,10B,10C , 10D ,10E , 10F
如果y >=F 十位就会多16个0
不然 十位就会多(y+1)个0

num(0)=(x-1)*16+(y>=9?16:y+1);

////////////////////////////////////////////////////////////////////////////////////////////////////数字1~F

首先有      16*x  个  1,2,3,4,5,6 ~ F

如果 n=116(16),x=1,y=16

除了(1*16)个 1,还有 7个1      ps :110,111,112,113,114,115,116

如果 n=126(16),x=1,y=26

除了(1*16)个 1,还有 16个1

 如果y >=iF 十位就会多16个i
不然 如果y>=i0 十位就会多(y-i0+1)个i

再不然就没有多余的i

                x=r/a[i+1];y=r%a[i+1];num[0]+=((x-1)*a[i]);if(y>=(a[i]-1)) num[0]+=a[i];else num[0]+=(y+1);for(int j=1;j<=15;j++){num[j]+=(x*a[i]);if(y>=(j+1)*a[i]-1) num[j]+=a[i];else  if(y>j*a[i]-1) num[j]+=(y-j*a[i]+1);    }


缕一缕头绪

1.首先为什么0,其他的不一样? 因为两位数的时候没有0i,三位数的时候有没有0ij,其他的数就不一样了

2.为什么不,0~F,10~1F? 【0,0】要特判

规律很明确,明了,但是总结的过程很繁琐,


时间复杂度  :  数字位数   *  数字个数  O(1)???

最后记得前导零

感觉代码风格还是。。。。。。你们将就看看吧

#include<bits/stdc++.h>const long long M=0xFFFFFFFF;const long long N=M+1;using namespace std;long long num[16];   long long a[9];long long b[16]={6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};void gg(long long r){     //    [0,r]int i=0;long long x,y;memset(num,0,sizeof(num));num[0]=8;while(r>=a[i]){//前导零     if(r>=a[i+1]-1) num[0]+=(a[i+1]-a[i])*(8-i-1);    else num[0]+=(8-i-1)*(r-a[i]+1);    //数字     //核心代码 x=r/a[i+1];y=r%a[i+1];num[0]+=((x-1)*a[i]);if(y>=(a[i]-1)) num[0]+=a[i];else num[0]+=(y+1);for(int j=1;j<=15;j++){num[j]+=(x*a[i]);if(y>=(j+1)*a[i]-1) num[j]+=a[i];else  if(y>j*a[i]-1) num[j]+=(y-j*a[i]+1);    }i++;}return ;}long long pp(long long r){gg(r); long long ans=0;for(int i=0;i<=15;i++){ans+=(num[i]*b[i]);}return ans;}int main(){long long n=0,l=0,r=0;char  s[20];int t;a[0]=1;//a[]储存16的幂 for(int i=1;i<=9;i++){a[i]=a[i-1]<<4;}scanf("%d",&t);while(t--){scanf("%lld %s",&n,s);l=0; for(int i=0;i<=7;i++){char j=s[i];l<<=4;if(j<='9') l+=(j-'0');else l+=(10+j-'A');}//左右边界 r=(l+n-1)%N;l=(l-1+N)%N;if(r>=l) printf("%lld\n",pp(r)-pp(l));else printf("%lld\n",pp(r)+pp(M)-pp(l));}return 0;}




    












0 0
原创粉丝点击