[HDU 3652] B-number [digital DP]

Source: Internet
Author: User

Question:

Evaluate the number of integers between 1 and N that contain 13 and can be divisible by 13.

Ideas:

Digit DP. Note that the remainder must be recorded in the condition of division, that is, the modulus.

# Include <cstdio> # include <cstring> using namespace STD; int DP [12] [15] [2] [12]; int bit [12]; // pos: number of digits processed num: modulo 13 remainder T: existing 13 E: Current tail flag: whether to enter the Upper Limit Range int DFS (INT POs, int num, bool T, int e, bool flag) {If (Pos =-1) // The return T &&(! Num); // determine whether there are still 13 Conditions for Division. If yes, 1 If (! Flag & DP [POS] [num] [T] [e]! =-1) // you have not reached the upper limit and have already calculated it. You can directly enter the return DP [POS] [num] [T] [E]; int end = flag? Bit [POS]: 9; // The maximum value of this bit. If the previous number does not reach the upper limit, this bit can be obtained to the maximum value of 9. otherwise, it is the int ans = 0 of N; For (INT I = 0; I <= end; I ++) {// The next DP, the remainder can be so accumulated (?), Whether there are 13 or just 13, ending number, whether this bit is still upper limit ans + = DFS (pos-1, (Num * 10 + I) % 13, T | (E = 1 & I = 3), I, flag & (I = END);} If (! Flag) DP [POS] [num] [T] [e] = ans; // if it is not the upper limit, this bit can be used for general and memory-based search. return ans;} int solve (int n) {int Pos = 0; while (n) {bit [POS ++] = n % 10; N/= 10 ;} return DFS (pos-1, 0, 0, 0, 1); // The final flag is only initialized to 1 to ensure that subsequent results are not affected. explanation .. still do not need to explain} int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N) = 1) {printf ("% d \ n", solve (N ));}}

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.