POJ 3286 How between 0 & amp; #39; s? (Digital dp)

Source: Internet
Author: User

POJ 3286 How between 0 & #39; s? (Digital dp)
Question

Input n, m, evaluate n ~ The total number of digits 0 in the m range.

Ideas

Use 2034 as an example.
The number of times that 0 appears in bits
When a single position is 0, you do not need to consider it later. You only need to consider the front. Because 0 is smaller than 4, even if the front is obtained to the maximum, the front can be 1 ~ 203 (because the current BIT is 0, the front cannot be 0 ). A total of 203 types.
10: when the value of 10 is 0, the first value is 1 ~ 20, followed by 0 ~ 9. A total of 123*10 types.
: When the value of a hundred bits is 0, because 0 is equal to the upper limit of the Current BITs, 0 ~ can be taken after the first 1 ~ 99. When the preceding value is 2, the following value can only be 0 ~ 34. A total of 1*100 + 35 types.
The number of thousands cannot be 0, so the total number is 0.
Convert the above idea into code.

Code
#include 
  
   #include #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include
        using namespace std;const int N = 10009;#define LL long longLL p[20];void init(){ p[0] = 1; for(int i=1; i<18; i++) p[i] = p[i-1]*10;}LL solve(LL x){ if(x == -1) return -1; LL ans = 0; for(int i=1; ; i++) { LL l = x/p[i]; LL r = x%p[i-1]; LL now = x%p[i]/p[i-1]; if(now > 0) ans += l*p[i-1]; else ans += (l-1)*p[i-1] + r+1; if(p[i] > x) break; } return ans;}int main(){ LL n, m; init(); while(cin>>n>>m && (n!=-1 || m!=-1)) printf("%lld\n", solve(m) - solve(n-1)); return 0;}
       
      
     
    
   
  

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.