[Digital DP] HDU 3967 Zero's numberd

Source: Internet
Author: User

For two numbers I and K, divide them into two parts: Number, N and M. If (n + M) % K = 0, this is a method.

For example, 333 can be divided into 3, 33, 33, and 3. For (333,3), it is equal to 2.

A, B, and K are given, which is (~ B, k) How many methods are there?

Idea: For a number, pay attention to the leading zero and enumerate the points.

DP [22] [22] [22] [22] [2], representing the I-bit, the Shard is fd, and the remainder mod. For K remainder, whether there is a leading zero


Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "stack" # include "algorithm" # include "iostream" using namespace STD; __int64 DP [22] [22] [22] [22] [2], ten [22]; // I bit, the Shard is fd, the remainder mod, for K remainder, whether there is a leading zero int num [22], CNT ;__ int64 DFS (INT site, int FD, int mod, int K, int ff, int F) {If (Site = 0) return MoD? 0: 1; if (! F & DP [site] [FD] [mod] [k] [ff]! =-1) return DP [site] [FD] [mod] [k] [ff]; int Len = f? Num [site]: 9; _ int64 ans = 0; For (INT I = 0; I <= Len; I ++) {If (site> FD) {If (FF = 0 & Site = FD + 1 & I = 0) continue; // The leading 0 cannot be before the shard, that is, the first half cannot be 0 ans + = DFS (site-1, FD, (I * Ten [site-FD] + mod) % K, K, FF | I! = 0, F & I = Len);} else ans + = DFS (site-1, FD, (I * Ten [site] + mod) % K, K, FF | I! = 0, F & I = Len);} If (! F) DP [site] [FD] [mod] [k] [ff] = ans; return ans; }__ int64 solve (_ int64 X, int K) {CNT = 0; If (x <10) return 0; _ int64 ans = 0; while (x) {num [++ CNT] = x % 10; x/= 10;} For (INT I = 1; I <CNT; I ++) // after the division of enumeration points is ans + = DFS (CNT, I, 0, k, 0, 1); Return ans;} int main () {_ int64 a, B; int K; memset (DP,-1, sizeof (DP )); ten [1] = 1; for (INT I = 2; I <= 18; I ++) Ten [I] = ten [I-1] * 10; while (scanf ("% i64d % i64d % d", & A, & B, & K )! =-1) {printf ("% i64d \ n", solve (B, k)-solve (A-1, k);} return 0 ;}


[Digital DP] HDU 3967 Zero's numberd

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.