HDU 5179-digit DP

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5179

Beautiful numberTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 198 Accepted Submission (s): 116


Problem Descriptionlet A=∑ n i = a i ? tenn? I (1≤ a i ≤9) ( n is the number of A ' s digits). We Call A As "beautiful number" if and only if a[i]≥a[i+ 1 ] When 1≤i<n and a[i] MoD a[J]=0 When 1≤i≤n,i<J ≤n (Such as 931 is a "beautiful number" while the.
Could you tell me the number of "beautiful number" in the interval [L,R] (including L and R)?

Inputthe Fist line contains a single integer T (about indicating), the number of cases.
Each test case begins with a integers L,R(1≤L≤R≤ ten 9 ) .
Outputfor each case, output an integer means the number of "beautiful number".
Sample Input
21 11999999993 999999999

Sample Output
102

Sourcebestcoder Round #31
Recommendhujie | We have carefully selected several similar problems for you:5181 5180 5179 5178 5177

IDEA: Digital DP template Problem ~ ~

#include <iostream> #include <stdio.h> #include <string> #include <string.h>using namespace std ; int dp[25][25][3];int bit[25];int dfs (int pos,int pre,int flag,int z) {  if (pos==0) return 1;  if (!flag && dp[pos][pre][z]!=-1) return dp[pos][pre][z];  int end=flag?bit[pos]:9;  int i=0,ans=0;  if (z==0) I=1;  for (; i<=end;i++)  {       if (pre==0)         Ans+=dfs (pos-1,i,flag&& (i==end),z&& (i==0));       else       {           if (pre>=i && pre%i==0) Ans+=dfs (pos-1,i,flag&& (i==end),z&& (i==0));       }  }  if (!flag) Dp[pos][pre][z]=ans;  return ans;} int cal (int x) {int len=0; memset (bit,0,sizeof (bit)); Memset (Dp,-1,sizeof (DP)); while (x) {   bit[++len]=x%10;   x/=10; } return Dfs (len,0,1,1);} int main () {  int l,r;  int T;  cin>>t;  while (t--)  {      scanf ("%d%d", &l,&r);      int s1=cal (r);      int s2=cal (L-1);      printf ("%d\n", s1-s2);  }  return 0;}


HDU 5179-digit DP

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.