HDU 3652 digital DP

Source: Internet
Author: User
/* Digit DP meaning: Find the number solution that contains the substring 13 between 1-N and can be divisible by 13: at the beginning, DP [N] [N] [2] here 2 is used to record whether the current BIT is 13, I have not recorded the case where the last bit is 1 and the current BIT is 13 and the last bit is 1 in the array. */# Include <stdio. h> # include <string. h> # define n 14int DP [N] [N] [3]; int digit [N]; int DFS (INT Len, int mod, int CNT, int OK) {If (! Len) {If (mod = 0 & CNT = 2) return 1; return 0;} If (! OK & DP [Len] [mod] [CNT]! =-1) return DP [Len] [mod] [CNT]; int ans = 0, I, Maxx = OK? Digit [Len]: 9; for (I = 0; I <= Maxx; I ++) {If (CNT = 2 | (CNT = 1 & I = 3) ans + = DFS (len-1, (mod * 10 + I) % 13,2, OK & I = Maxx); else if (I = 1) // At the beginning, the judgment condition is written as if (CNT = 0 & I = 1) this is not true because of missing a situation (I = 1 & CNT = 1) ans + = DFS (len-1, (mod * 10 + I) % 13, 1, OK & I = Maxx); else ans + = DFS (len-1, (mod * 10 + I) % 13,0, OK & I = Maxx);} If (! OK) DP [Len] [mod] [CNT] = ans; return ans;} int F (INT N) {int Len = 0; while (N) {digit [++ Len] = n % 10; N/= 10;} return DFS (Len, 0, 0, 1) ;}int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N )! = EOF) {printf ("% d \ n", F (N) ;}return 0 ;}
<PRE name = "code" class = "CPP">/* in my original thought, I did not enable one-dimensional record pre at the beginning */# include <stdio. h> # include <string. h> # define n 14int DP [N] [N] [2] [2]; int digit [N]; int DFS (INT Len, int mod, int pre, int CNT, int OK) {If (! Len) {If (mod = 0 & CNT = 1) return 1; return 0;} If (! OK & DP [Len] [mod] [pre] [CNT]! =-1) return DP [Len] [mod] [pre] [CNT]; int ans = 0, I, Maxx = OK? Digit [Len]: 9; for (I = 0; I <= Maxx; I ++) {If (CNT | (pre & I = 3 )) ans + = DFS (len-1, (mod * 10 + I) % 13, I =, OK & I = Maxx); else ans + = DFS (len-1, (mod * 10 + I) % 13, I =, OK & I = Maxx);} If (! OK) DP [Len] [mod] [pre] [CNT] = ans; return ans;} int F (INT N) {int Len = 0; while (N) {digit [++ Len] = n % 10; N/= 10;} return DFS (Len,);} int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N )! = EOF) {printf ("% d \ n", F (N) ;}return 0 ;}


 

HDU 3652 digital 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.