ACM/ICPC Asia Regional Shenyang Online 1007/hdu 5898 Digit DP

Source: Internet
Author: User

Odd-even number

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 388 Accepted Submission (s): 212


Problem Descriptionfor A number,if the length of continuous odd digits is even and the length of continuous even digits are Odd,we call it odd-even number. Now we want to know the amount of odd-even number between l,r (1<=l<=r<= 9*10^18).

Inputfirst Line a t,then t cases.every line contains, integers L and R.

Outputprint the output for each case is on one line in the format as shown below.

Sample INPUT2 1 100 110 220

Sample outputcase #1:29Case #2:36

Source ACM/ICPC Asia Regional Shenyang Online test instructions: A number that has an even-length segment on every odd number of digits, and even-numbered bits that form odd-length segments. He's good. Ask [L, R] for a number of good numbers. DP[I][J] indicates that the state of the first digit is J. There are 4 states, 1-odd length is odd, 2-odd length is even, 3-even length is odd, 4-even length is even plus a state flag=0 means that the current bit is preceded by 0 (note that transitions between states have been memorized)
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^    ^ ^5 o o6 ******************************/7 //#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cmath> One#include <cstdio> A #definell Long Long - #defineMoD 1000000007 - #definePI ACOs (-1.0) the using namespacestd; - intT; - ll L,r; - intnum[ -]; +ll dp[ -][5]; - /* + 1 Kiki A 2 Odd-even at 3 even odd - 4 Even - */ -ll Dfs (intPosintStatusintflag) - { -     if(pos<1) in     { -         if(status==2|| status==3) to              return 1; +         Else -              return 0; the     } *     if(!flag&&Dp[pos][status]) $         returnDp[pos][status];Panax Notoginseng     intEnd=flag? Num[pos]:9The current one can take 0~9 if it's preceded by a leading zero. -ll ans=0; the      for(intI=0; i<=end;i++) +     { A         if(!status) the         { +             if(!i) -             { $Ans=dfs (pos-1,0,0); $             } -             Else if(i&1) -             { theAns+=dfs (pos-1,1, flag&&i==end); -             }Wuyi             Else the             { -Ans+=dfs (pos-1,3, flag&&i==end); Wu             } -  About         } $         Else -         { -             if(status==1){ -                 if(i&1){ AAns+=dfs (pos-1,2, flag&&i==end); +                 } the             } -             Else if(status==2){ $                 if(i&1){ theAns+=dfs (pos-1,1, flag&&i==end); the                 } the                 Else{ theAns+=dfs (pos-1,3, flag&&i==end); -                 } in             } the             Else if(status==3){ the                 if(i&1){ AboutAns+=dfs (pos-1,1, flag&&i==end); the                 } the                 Else theAns+=dfs (pos-1,4, flag&&i==end); +             } -             Else{ the                 if(! (i&1))Bayi                 { theAns+=dfs (pos-1,3, flag&&i==end); the                 } -             } -         } the     } thedp[pos][status]=ans; the     returnans; the } - ll Slove (ll x) the { theMemset (DP,0,sizeof(DP)); the     intlen=0;94      while(x) the     { thenum[++len]=x%Ten; theX/=Ten;98     } About     returnDFS (Len,0,1); - }101 intMain ()102 {103scanf"%d",&t);104          for(intI=1; i<=t;i++) the         {106scanf"%i64d%i64d",&l,&R);107printf"Case #%d:%i64d\n", I,slove (R)-slove (l1));108         }109     return 0; the}

ACM/ICPC Asia Regional Shenyang Online 1007/hdu 5898 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.