Xbz grouping question B Geely digital digit DP entry

Source: Internet
Author: User

B Geely digital
Time Limit: 1 s

[Description]
Bigboyouyun recently came to a magical conclusion. If a number can be divided by 10, it is called the auspicious number. Calculate the number of Geely numbers in a certain interval.

[Input]
The number of first behavior samples is N. In the next n rows, each row represents an input sample. Each input sample has two numbers, representing the start point A and end point B of a specific interval.
Note that the range is [a, B], 1 <= A <= B <= 10 ^ 9

[Output]
N rows. For the X input sample, enter the result of the sample in Line X.

[Input example]
2
1 10
1 20

[Output example]
0
1

[Hint] There is no lucky number in 1-10, and only 19 lucky numbers in 1-20
Bytes ------------------------------------------------------------------------------------

Digital DP beginners.

I started by referring to the non-recursive method of the digital DP getting started PPT. I typed a table and then calculated it. After half a day of YY, I still got the error t ^ t.

 

Here is a template for writing digital DP using DFS:

Http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html

Code:

 1 #include <stdio.h> 2 #include <string.h> 3 int dp[100][100]; 4 int dig[100]; 5  6 int dfs(int i,int s,bool e) 7 { 8     if (!i) return (s==0?1:0); 9     if ((!e)&&(dp[i][s]!=-1))    return dp[i][s];10     int res=0;11     int u=e?dig[i]:9;12     for (int q=0;q<=u;q++)13         res+=dfs(i-1,(s+q)%10,e&&q==u);14     if (!e)    dp[i][s]=res;15     return res;16 }17 18 int f(int n)19 {20     int len = 0;21     while(n)22     {23         dig[++len] = n % 10;24         n /= 10;25     }26     return dfs(len,0,true);27 }28 29 int main()30 {31     int a,b,T;32     memset(dp,-1,sizeof(dp));33     scanf("%d",&T);34     while (T--)35     {36         scanf("%d %d",&a,&b);37         printf("%d\n",f(b)-f(a-1));38     }39     return 0;40 }
View code

 

Core: (from neopenx, orz)

 1 int dfs(int len,int remain,bool fp) 2 { 3     if(!len) return remain==0?1:0; 4     if(!fp&&dp[len][remain]!=-1) 5         return dp[len][remain]; 6     int ret=0,fpmax=fp?digit[len]:9; 7     for(int i=0;i<=fpmax;i++) 8         ret+=dfs(len-1,(remain+i)%10,fp&&i==fpmax); 9     if(!fp) dp[len][remain]=ret;10     return ret;11 }12 13 int f(int n)14 {15     int len=0;16     while(n)17     {18         digit[++len]=n%10;19         n/=10;20     }21     return dfs(len,0,true);22 }

 

Len: the number of digits currently scanned, from high to status

Remain: indicates the current status. As shown in this question, from the highest digit to the current digit, numbers and % 10

FP: This is also called "First Place". neopenx's understanding is the first time to create a table.

The basic idea of this DFS function is to write down a table and use

Fpmax: current highest bit

 

 

Extension: hdu2089

Http://blog.csdn.net/dgq8211/article/details/9296953

 

Xbz grouping question B Geely digital digit DP entry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.