HDU 4389 x Mod f (x) (Digital DP)

Source: Internet
Author: User

Finally, I got this question... I watched it for half a week. The number of questions accumulated in the competition is decreasing... Come on!

Digit DP. f [I] [Sum] [mod] [res] indicates the first I bit, sum, Mod, sum % mod, and Res. F [I + 1] [Sum + k] [mod] [(RES * 10 + k) % mod] + = f [I] [Sum] [mod] [res];

All F [I] [Sum] [mod] [res] are processed, and the statistics are calculated from high to low. For example, if the current BIT is a_ I, then the enumerated Current BIT is 0... (a_i-1) these numbers, all the States of the number after the I bit has been preprocessed... So % mod = 0 can be accumulated directly... Finally, the bitwise is processed separately...

1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 # include <vector> 5 # include <cstring> 6 # include <algorithm> 7 # include <string> 8 # include <set> 9 # include <ctime> 10 # include <queue> 11 # include <map> 12 # include <functional> 13 # include <numeric> 14 # include <sstream> 15 16 # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) 17 # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) 18 # Define for (I, L, H) for (I) = (l); (I) <= (h); ++ (I )) 19 # define Ford (I, H, L) for (I) = (h); (I)> = (l); -- (I )) 20 # define L (x) <1 21 # define R (x) <1 | 1 22 # define mid (L, R) (L + r)> 1 23 # define min (x, y) x <Y? X: Y 24 # define max (x, y) x <Y? Y: x 25 # define e (x) (1 <(x) 26 27 const int EPS = 1e-4; 28 typedef long ll; 29 const int INF = ~ 0u> 2; 30 using namespace STD; 31 32 const int n = 83; 33 34 int f [12] [N] [N] [N]; 35 int ten [15]; 36 37 38 void Init () {39 int I, sum, Mod, res, K, J; 40 ten [0] = 1; 41 for (I = 1; I <= 10; + I) Ten [I] = ten [I-1] * 10; 42 Cl (F, 0 ); 43 for (I = 0; I <10; ++ I) {44 for (j = 1; j <n; ++ J) f [1] [I] [J] [I % J] = 1; 45} 46 47 for (I = 1; I <9; ++ I) {48 for (sum = 0; sum <n; ++ sum) {49 for (mod = 1; mod <n; + + Mod) {50 for (RES = 0; Res <n; ++ res) {51 if (! F [I] [Sum] [mod] [res]) continue; 52 for (k = 0; k <10 & sum + k <n; ++ K) 53 f [I + 1] [Sum + k] [mod] [(RES * 10 + k) % mod] + = f [I] [Sum] [mod] [res]; 54} 55} 56} 57} 58} 59 60 int A [15]; 61 62 int do (int x) {63 If (x <= 10) return X; 64 int tsum = 0, I, j, n = 0, U; 65 int mod, sum, num, res, ANS = 0; 66 67 for (u = x; u> 0; U/= 10) A [++ N] = u % 10, tsum + = u % 10; 68 69 for (mod = 1; mod <= 9 * n; ++ mod ){/ /Enumeration modulo, that is, all digits and 70 If (mod> X) break; 71 sum = MOD; 72 num = 0; 73 for (I = N; i> 1; -- I) {74 for (j = 0; j <A [I] & J <= sum; ++ J) {// from 0... (a_ I-1) enumeration. 75 for (RES = 0; Res <MOD; ++ res) {// res indicates the number of digits after I bit and the value obtained by modulo 76 if (Num + J * Ten [I-1] + Res) % mod = 0) {// according to the same remainder formula 77 ans + = f [I-1] [Sum-J] [mod] [res]; 78} 79} 80} 81 sum-= A [I]; 82 num + = A [I] * Ten [I-1]; 83} 84} 85 while (1) {// handle the random bit 86 If (X % tsum = 0) ans ++; 87 If (X % 10 = 0) break; 88 X --; tsum --; 89} 90 return ans; 91} 92 93 int main () {94 freopen ("data. in "," r ", stdin); 95 96 Init (); 97 int t, a, B, CAS = 0; 98 scanf (" % d ", & T ); 99 while (t --) {100 scanf ("% d", & A, & B); 101 printf ("case % d: % d \ n ", + + cas, do (B)-Do (A-1); 102} 103 return 0; 104}

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.