[Digital DP] spoj 10738 ra-one numbers

Source: Internet
Author: User

Given X and Y, the number of even digits between [x, y] is equal to or equal to one minus odd digits.

One digit is the first.

Example: 10 = 1-0 = 1 so 10 is such a number

Idea: digit DP [I] [Sum] [OK] I bit and sum whether there is a leading 0.

Then, because there is a negative number, 0 is set to 100 Based on the range, and then the last and equal to 101 are the numbers.

Code:

[CPP]View plaincopyprint?
  1. # Include "cstdlib"
  2. # Include "cstdio"
  3. # Include "cstring"
  4. # Include "cmath"
  5. # Include "stack"
  6. # Include "algorithm"
  7. # Include "iostream"
  8. Using namespace STD;
  9. Int DP [12] [200] [2], num [12];
  10. Int fuck [2] = {1,-1 };
  11. Int DFS (INT site, int sum, int OK, int F)
  12. {
  13. If (Site = 0)
  14. {
  15. If (OK = 0) return 0;
  16. Return sum = 101? 1:0; // The sum of small processing and 101
  17. }
  18. If (! F & DP [site] [Sum] [OK]! =-1) return DP [site] [Sum] [OK];
  19. Int Len = f? Num [site]: 9;
  20. Int ans = 0;
  21. For (INT I = 0; I <= Len; I ++)
  22. {
  23. If (OK = 0)
  24. {
  25. If (I = 0) ans + = DFS (site-1, sum, OK | I! = 0, F & I = Len );
  26. Else ans + = DFS (site-1, sum + I * fuck [Site % 2], OK | I! = 0, F & I = Len );
  27. }
  28. Else
  29. {
  30. Ans + = DFS (site-1, sum + I * fuck [Site % 2], OK | I! = 0, F & I = Len );
  31. }
  32. }
  33. If (! F) DP [site] [Sum] [OK] = ans;
  34. Return ans;
  35. }
  36. Int solve (int x)
  37. {
  38. If (x <0) return 0;
  39. Int CNT = 0;
  40. While (X)
  41. {
  42. Num [++ CNT] = x % 10;
  43. X/= 10;
  44. }
  45. Return DFS (CNT, 100, 0, 1); // sum =
  46. }
  47. Int main ()
  48. {
  49. Int T;
  50. Scanf ("% d", & T );
  51. Memset (DP,-1, sizeof (DP ));
  52. While (t --)
  53. {
  54. Int X, Y;
  55. Scanf ("% d", & X, & Y );
  56. Printf ("% d \ n", solve (y)-solve (x-1 ));
  57. }
  58. Return 0;
  59. }

[Digital DP] spoj 10738 ra-one numbers

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.